Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Use more PHP 7.4 language features #160

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Linter/Configuration/YamlConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public function __construct(FileLocatorInterface $locator, YamlParser $yamlParse
* Loads a resource.
*
* @param mixed $resource The resource
* @param string $type The resource type
* @param string|null $type The resource type
*
* @return array
*
* @psalm-suppress MethodSignatureMismatch
*/
public function load($resource, $type = null): array
public function load(mixed $resource, ?string $type = null): array
{
try {
/** @var string $path */
Expand All @@ -67,13 +67,13 @@ public function load($resource, $type = null): array
* Returns true if this class supports the given resource.
*
* @param mixed $resource A resource
* @param string $type The resource type
* @param string|null $type The resource type
*
* @return bool true if this class supports the given resource, false otherwise
*
* @psalm-suppress MethodSignatureMismatch
*/
public function supports($resource, $type = null): bool
public function supports(mixed $resource, string $type = null): bool
{
return is_string($resource)
&& in_array(pathinfo($resource, PATHINFO_EXTENSION), ['yml', 'yaml']);
Expand Down
31 changes: 5 additions & 26 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 All @@ -33,14 +30,6 @@ public function __construct(TokenizerInterface $tokenizer, ParserInterface $pars
$this->sniffLocator = $sniffLocator;
}

/**
* @param string $filename
* @param Report $report
* @param LinterConfiguration $configuration
* @param LinterLoggerInterface $logger
*
* @return File
*/
public function lintFile(
string $filename,
Report $report,
Expand Down Expand Up @@ -69,12 +58,7 @@ public function lintFile(
}

/**
* @param TokenInterface[] $tokens
* @param File $file
* @param LinterConfiguration $configuration
* @param LinterLoggerInterface $logger
*
* @return File
* @psalm-param TokenInterface[] $tokens
*/
private function lintTokenStream(
array $tokens,
Expand All @@ -98,12 +82,7 @@ private function lintTokenStream(
}

/**
* @param Statement[] $statements
* @param File $file
* @param LinterConfiguration $configuration
* @param LinterLoggerInterface $logger
*
* @return File
* @psalm-param Statement[] $statements
*/
private function lintSyntaxTree(
array $statements,
Expand Down
3 changes: 1 addition & 2 deletions src/Linter/LinterConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
class LinterConfiguration implements ConfigurationInterface
{

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

public function setConfiguration(array $configuration): void
{
Expand Down
9 changes: 0 additions & 9 deletions src/Linter/LinterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@

interface LinterInterface
{

/**
* @param string $filename
* @param Report $report
* @param LinterConfiguration $configuration
* @param LinterLoggerInterface $logger
*
* @return File
*/
public function lintFile(
string $filename,
Report $report,
Expand Down
23 changes: 4 additions & 19 deletions src/Linter/Report/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,16 @@
class File
{

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

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

/**
* Constructs a new file report.
*
* @param string $filename The filename.
*/
public function __construct(string $filename)
{
$this->filename = $filename;
}

/**
* Gets the filename.
*
* @return string The filename.
*/
public function getFilename(): string
{
return $this->filename;
Expand Down Expand Up @@ -61,9 +50,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 +64,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
9 changes: 1 addition & 8 deletions src/Linter/ReportPrinter/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@
* Interface definition for code linting report printers.
*
* @package Helmich\TypoScriptLint
* @subpcakage Linter\ReportPrinter
* @subpackage Linter\ReportPrinter
*/
interface Printer
{

/**
* Writes a report.
*
* @param Report $report
*
* @return void
*/
public function writeReport(Report $report): void;
}
6 changes: 0 additions & 6 deletions src/Linter/Sniff/AbstractSyntaxTreeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
abstract class AbstractSyntaxTreeSniff implements SyntaxTreeSniffInterface
{

/**
* @param array $parameters
*/
public function __construct(array $parameters)
{
}
Expand All @@ -44,8 +41,5 @@ public function sniff(array $statements, File $file, LinterConfiguration $config
}
}

/**
* @return SniffVisitor
*/
abstract protected function buildVisitor(): SniffVisitor;
}
3 changes: 0 additions & 3 deletions src/Linter/Sniff/ConfigNoCacheSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class ConfigNoCacheSniff extends AbstractSyntaxTreeSniff
{
/**
* @return SniffVisitor
*/
protected function buildVisitor(): SniffVisitor
{
return new ConfigNoCacheVisitor();
Expand Down
7 changes: 2 additions & 5 deletions src/Linter/Sniff/DeadCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
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
*/
public function __construct(array $parameters)
{
}
Expand Down Expand Up @@ -51,7 +48,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
3 changes: 0 additions & 3 deletions src/Linter/Sniff/DuplicateAssignmentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class DuplicateAssignmentSniff extends AbstractSyntaxTreeSniff
{
/**
* @return SniffVisitor
*/
protected function buildVisitor(): SniffVisitor
{
return new DuplicateAssignmentVisitor();
Expand Down
Loading
Loading