Skip to content

Commit

Permalink
[TASK] Use more PHP 7.4 language features
Browse files Browse the repository at this point in the history
Also use more specific PHPDoc type annotations.
  • Loading branch information
oliverklee committed May 31, 2023
1 parent a3f48a3 commit 31b8d19
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 122 deletions.
8 changes: 3 additions & 5 deletions src/Linter/Configuration/ConfigurationLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
class ConfigurationLocator
{

/** @var LoaderInterface */
private $loader;
private LoaderInterface $loader;

/** @var Processor */
private $processor;
private Processor $processor;

/**
* Constructs a new configuration locator.
Expand Down Expand Up @@ -64,7 +62,7 @@ public function loadConfiguration(
$configs[] = $loadedConfig;
}

$configuration = $configuration ?? new LinterConfiguration();
$configuration ??= new LinterConfiguration();

$processedConfiguration = $this->processor->processConfiguration(
$configuration,
Expand Down
6 changes: 2 additions & 4 deletions src/Linter/Configuration/YamlConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
class YamlConfigurationLoader extends FileLoader
{

/** @var YamlParser */
private $yamlParser;
private YamlParser $yamlParser;

/** @var Filesystem */
private $filesystem;
private Filesystem $filesystem;

/**
* Constructs a new YAML-based configuration loader.
Expand Down
9 changes: 3 additions & 6 deletions src/Linter/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
class Linter implements LinterInterface
{

/** @var TokenizerInterface */
private $tokenizer;
private TokenizerInterface $tokenizer;

/** @var ParserInterface */
private $parser;
private ParserInterface $parser;

/** @var SniffLocator */
private $sniffLocator;
private SniffLocator $sniffLocator;

public function __construct(TokenizerInterface $tokenizer, ParserInterface $parser, SniffLocator $sniffLocator)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Linter/LinterConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
class LinterConfiguration implements ConfigurationInterface
{

/** @var array */
private $configuration = [];
private array $configuration = [];

/**
* @param mixed[] $configuration
*/
public function setConfiguration(array $configuration): void
{
$this->configuration = $configuration;
Expand Down
13 changes: 4 additions & 9 deletions src/Linter/Report/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
class File
{

/** @var string */
private $filename;
private string $filename;

/** @var Issue[] */
private $issues = [];
private array $issues = [];

/**
* Constructs a new file report.
Expand Down Expand Up @@ -61,9 +60,7 @@ public function getIssues(): array
{
usort(
$this->issues,
function (Issue $a, Issue $b): int {
return ($a->getLine() ?? 0) - ($b->getLine() ?? 0);
}
fn(Issue $a, Issue $b): int => ($a->getLine() ?? 0) - ($b->getLine() ?? 0)
);
return $this->issues;
}
Expand All @@ -77,9 +74,7 @@ function (Issue $a, Issue $b): int {
*/
public function getIssuesBySeverity(string $severity): array
{
return array_values(array_filter($this->getIssues(), function (Issue $i) use ($severity): bool {
return $i->getSeverity() === $severity;
}));
return array_values(array_filter($this->getIssues(), fn(Issue $i): bool => $i->getSeverity() === $severity));
}

/**
Expand Down
21 changes: 8 additions & 13 deletions src/Linter/Report/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@
class Issue
{

const SEVERITY_INFO = "info";
const SEVERITY_WARNING = "warning";
const SEVERITY_ERROR = "error";
public const SEVERITY_INFO = "info";
public const SEVERITY_WARNING = "warning";
public const SEVERITY_ERROR = "error";

/** @var int|null */
private $line;
private ?int $line = null;

/** @var int|null */
private $column;
private ?int $column = null;

/** @var string */
private $message;
private string $message;

/** @var string */
private $severity;
private string $severity;

/** @var string */
private $source;
private string $source;

/**
* Creates a new warning from a parse error.
Expand Down
2 changes: 1 addition & 1 deletion src/Linter/Report/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Report
{

/** @var File[] */
private $files = [];
private array $files = [];

/**
* Adds a sub-report for a specific file.
Expand Down
3 changes: 1 addition & 2 deletions src/Linter/ReportPrinter/CheckstyleReportPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
class CheckstyleReportPrinter implements Printer
{

/** @var OutputInterface */
private $output;
private OutputInterface $output;

/**
* Constructs a new checkstyle report printer.
Expand Down
3 changes: 1 addition & 2 deletions src/Linter/ReportPrinter/ConsoleReportPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
class ConsoleReportPrinter implements Printer
{

/** @var OutputInterface */
private $output;
private OutputInterface $output;

/**
* Constructs a new console report printer.
Expand Down
3 changes: 1 addition & 2 deletions src/Linter/ReportPrinter/GccReportPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
class GccReportPrinter implements Printer
{

/** @var OutputInterface */
private $output;
private OutputInterface $output;

/**
* Constructs a new GCC report printer.
Expand Down
4 changes: 2 additions & 2 deletions src/Linter/Sniff/DeadCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class DeadCodeSniff implements TokenStreamSniffInterface
{

const ANNOTATION_COMMENT = '/^\s*([a-z0-9]+=(.*?))(;\s*[a-z0-9]+=(.*?))*\s*$/';
public const ANNOTATION_COMMENT = '/^\s*([a-z0-9]+=(.*?))(;\s*[a-z0-9]+=(.*?))*\s*$/';

/**
* @param array $parameters
Expand Down Expand Up @@ -51,7 +51,7 @@ public function sniff(array $tokens, File $file, LinterConfiguration $configurat
0,
'Found commented code (' . $matches[0] . ').',
Issue::SEVERITY_INFO,
__CLASS__
self::class
));
} catch (\Exception $e) {
// pass
Expand Down
34 changes: 17 additions & 17 deletions src/Linter/Sniff/IndentationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,19 @@ class IndentationSniff implements TokenStreamSniffInterface
{
use TokenInspections;

/** @var bool */
private $useSpaces = true;
private bool $useSpaces = true;

/** @var int */
private $indentPerLevel = 4;
private int $indentPerLevel = 4;

/**
* Defines whether code inside conditions should be indented by one level.
*
* @var bool
*/
private $indentConditions = false;
private bool $indentConditions = false;

/**
* Track whether we are inside a condition.
*
* @var bool
*/
private $insideCondition = false;
private bool $insideCondition = false;

/**
* @param array $parameters
Expand Down Expand Up @@ -77,9 +71,12 @@ public function sniff(array $tokens, File $file, LinterConfiguration $configurat
$expectedIndentationCharacterCount
);

$tokensInLine = array_values(array_filter($tokensInLine, function (TokenInterface $token): bool {
return $token->getType() !== TokenInterface::TYPE_RIGHTVALUE_MULTILINE;
}));
$tokensInLine = array_values(
array_filter(
$tokensInLine,
fn(TokenInterface $token): bool => $token->getType() !== TokenInterface::TYPE_RIGHTVALUE_MULTILINE
)
);

// Skip empty lines and conditions inside conditions.
if ($this->isEmptyLine($tokensInLine) || ($this->insideCondition && self::isCondition($tokensInLine[0]))) {
Expand Down Expand Up @@ -112,9 +109,12 @@ public function sniff(array $tokens, File $file, LinterConfiguration $configurat
*/
private function isEmptyLine(array $tokensInLine): bool
{
$tokensInLine = array_values(array_filter($tokensInLine, function (TokenInterface $t): bool {
return $t->getType() !== TokenInterface::TYPE_EMPTY_LINE;
}));
$tokensInLine = array_values(
array_filter(
$tokensInLine,
fn(TokenInterface $t): bool => $t->getType() !== TokenInterface::TYPE_EMPTY_LINE
)
);

if (count($tokensInLine) === 0) {
return true;
Expand Down Expand Up @@ -199,6 +199,6 @@ private function createIssue(int $line, int $expectedLevel, string $actual): Iss

$expectedMessage = "Expected indent of {$indentCharacterCount} {$indentCharacterDescription}.";

return new Issue($line, strlen($actual), $expectedMessage, Issue::SEVERITY_WARNING, __CLASS__);
return new Issue($line, strlen($actual), $expectedMessage, Issue::SEVERITY_WARNING, self::class);
}
}
8 changes: 4 additions & 4 deletions src/Linter/Sniff/OperatorWhitespaceSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ public function sniff(array $tokens, File $file, LinterConfiguration $configurat
null,
'No whitespace after object accessor.',
Issue::SEVERITY_WARNING,
__CLASS__
self::class
));
} elseif (!self::isWhitespaceOfLength($tokensInLine[$i + 1], 1)) {
$file->addIssue(new Issue(
$tokensInLine[$i]->getLine(),
null,
'Accessor should be followed by single space.',
Issue::SEVERITY_WARNING,
__CLASS__
self::class
));
}

Expand All @@ -72,15 +72,15 @@ public function sniff(array $tokens, File $file, LinterConfiguration $configurat
null,
'No whitespace after operator.',
Issue::SEVERITY_WARNING,
__CLASS__
self::class
));
} elseif (!self::isWhitespaceOfLength($tokensInLine[$j + 1], 1)) {
$file->addIssue(new Issue(
$tokensInLine[$j]->getLine(),
null,
'Operator should be followed by single space.',
Issue::SEVERITY_WARNING,
__CLASS__
self::class
));
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/Linter/Sniff/RepeatingRValueSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
class RepeatingRValueSniff implements TokenStreamSniffInterface
{

const CONSTANT_EXPRESSION = ',\{\$[a-zA-Z0-9_\.]+\},';
public const CONSTANT_EXPRESSION = ',\{\$[a-zA-Z0-9_\.]+\},';

/** @var array<string, int> */
private $knownRightValues = [];
private array $knownRightValues = [];

/** @var string[] */
private $allowedRightValues = [];
private array $allowedRightValues = [];

/** @var int */
private $valueLengthThreshold = 8;
private int $valueLengthThreshold = 8;

/**
* @param array $parameters
Expand Down Expand Up @@ -75,7 +74,7 @@ public function sniff(array $tokens, File $file, LinterConfiguration $configurat
null,
'Duplicated value "' . $token->getValue() . '". Consider extracting it into a constant.',
Issue::SEVERITY_WARNING,
__CLASS__
self::class
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Linter/Sniff/SniffLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SniffLocator
{

/** @var SniffInterface[]|null */
private $sniffs = null;
private ?array $sniffs = null;

/**
* @param LinterConfiguration $configuration
Expand All @@ -29,11 +29,11 @@ private function loadSniffs(LinterConfiguration $configuration): array
foreach ($configuration->getSniffConfigurations() as $sniffConfiguration) {
if (!class_exists($sniffConfiguration['class'])) {
throw new Exception(
'Class "' . $sniffConfiguration['class'] . '" could not be loaded!', 1402948667
'Class "' . $sniffConfiguration['class'] . '" could not be loaded!', 1_402_948_667
);
}

$parameters = isset($sniffConfiguration['parameters']) ? $sniffConfiguration['parameters'] : [];
$parameters = $sniffConfiguration['parameters'] ?? [];

/** @var SniffInterface $sniff */
$sniff = new $sniffConfiguration['class']($parameters);
Expand Down
2 changes: 1 addition & 1 deletion src/Linter/Sniff/Visitor/ConfigNoCacheVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class ConfigNoCacheVisitor implements SniffVisitor
{
/** @var Issue[] */
private $issues = [];
private array $issues = [];

/**
* @return Issue[]
Expand Down
7 changes: 3 additions & 4 deletions src/Linter/Sniff/Visitor/DuplicateAssignmentVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ class DuplicateAssignmentVisitor implements SniffVisitor
{

/** @var Assignment[] */
private $assignments = [];
private array $assignments = [];

/** @var Issue[] */
private $issues = [];
private array $issues = [];

/** @var bool */
private $inCondition = false;
private bool $inCondition = false;

/**
* @return Issue[]
Expand Down
Loading

0 comments on commit 31b8d19

Please sign in to comment.