From df6e2cb18a7bb5b88be4dde61c9307b1f66eb698 Mon Sep 17 00:00:00 2001 From: Ulrich Mathes Date: Wed, 17 Jul 2024 14:29:11 +0200 Subject: [PATCH] improve readability and use PHP 8 features MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the help of rector and ☕ --- .../Check/ComponentVariablesCheck.php | 4 +-- .../Check/DocumentationFixtureCheck.php | 4 +-- .../Check/ParamTypeNamespaceCheck.php | 2 +- .../CodeQuality/Check/ViewHelpersCheck.php | 8 +++--- src/Classes/CodeQuality/Issue/Issue.php | 27 +++++++++---------- .../CodeQuality/Issue/IssueInterface.php | 12 ++++----- src/Classes/Command/LintCommand.php | 19 +++++-------- .../Configuration/LintConfiguration.php | 2 +- .../Fluid/ViewHelper/ViewHelperResolver.php | 7 ++--- src/Classes/Service/CodeQualityService.php | 19 +++++-------- src/Classes/Service/ComponentService.php | 22 ++++----------- src/Classes/Service/FluidService.php | 4 +-- .../ViewHelpers/ComponentViewHelper.php | 2 +- .../ViewHelpers/IntrospectionViewHelper.php | 2 +- src/Classes/ViewHelpers/ParamViewHelper.php | 2 +- src/FcLint.php | 2 +- 16 files changed, 53 insertions(+), 85 deletions(-) diff --git a/src/Classes/CodeQuality/Check/ComponentVariablesCheck.php b/src/Classes/CodeQuality/Check/ComponentVariablesCheck.php index 72b7a20..a153eca 100644 --- a/src/Classes/CodeQuality/Check/ComponentVariablesCheck.php +++ b/src/Classes/CodeQuality/Check/ComponentVariablesCheck.php @@ -36,8 +36,6 @@ protected function extractUsedVariables(): array $this->component->rootNode, ObjectAccessorNode::class ); - return array_map(function (ObjectAccessorNode $node) { - return $node->getObjectPath(); - }, $usedVariables); + return array_map(fn(ObjectAccessorNode $node) => $node->getObjectPath(), $usedVariables); } } diff --git a/src/Classes/CodeQuality/Check/DocumentationFixtureCheck.php b/src/Classes/CodeQuality/Check/DocumentationFixtureCheck.php index 87b404a..0bba4be 100644 --- a/src/Classes/CodeQuality/Check/DocumentationFixtureCheck.php +++ b/src/Classes/CodeQuality/Check/DocumentationFixtureCheck.php @@ -35,9 +35,7 @@ public function check(): array return $issues; } - $fixtureFiles = array_map(function ($fixtureFile) use ($directory, $name) { - return sprintf($fixtureFile, $directory, $name); - }, $this->fixtureFiles); + $fixtureFiles = array_map(fn($fixtureFile) => sprintf($fixtureFile, $directory, $name), $this->fixtureFiles); $fixtureFile = array_filter($fixtureFiles, 'file_exists'); $fixtureFile = reset($fixtureFile); diff --git a/src/Classes/CodeQuality/Check/ParamTypeNamespaceCheck.php b/src/Classes/CodeQuality/Check/ParamTypeNamespaceCheck.php index 63daaf0..f9bebfb 100644 --- a/src/Classes/CodeQuality/Check/ParamTypeNamespaceCheck.php +++ b/src/Classes/CodeQuality/Check/ParamTypeNamespaceCheck.php @@ -15,7 +15,7 @@ public function check(): array $issues = []; foreach ($this->component->paramNodes as $paramNode) { $arguments = $paramNode->getArguments(); - if (substr((string) $arguments['type'], 0, 1) === '\\') { + if (str_starts_with((string) $arguments['type'], '\\')) { $issues[] = $this->issue('Type is specified with leading backslash for parameter %s: %s', [ $arguments['name'], $arguments['type'] diff --git a/src/Classes/CodeQuality/Check/ViewHelpersCheck.php b/src/Classes/CodeQuality/Check/ViewHelpersCheck.php index cdcfb38..5c517cc 100644 --- a/src/Classes/CodeQuality/Check/ViewHelpersCheck.php +++ b/src/Classes/CodeQuality/Check/ViewHelpersCheck.php @@ -16,7 +16,7 @@ public function check(): array foreach ($this->configuration['renderer']['viewHelperRestrictions'] as $restriction) { $pattern = $this->createViewHelperPattern($restriction['viewHelperName']); foreach ($usedViewHelpers as $tagName) { - if (preg_match($pattern, $tagName)) { + if (preg_match($pattern, (string) $tagName)) { $issues[] = $this->issue($restriction['message'], [ $tagName ])->setSeverity($restriction['severity']); @@ -30,7 +30,7 @@ public function check(): array protected function createViewHelperPattern(string $viewHelperName): string { $pattern = preg_quote($viewHelperName, '#'); - if (substr($viewHelperName, -1, 1) !== '.') { + if (!str_ends_with($viewHelperName, '.')) { $pattern .= '$'; } return '#^' . $pattern . '#'; @@ -42,9 +42,7 @@ protected function extractUsedViewHelpers(): array $this->component->rootNode, ViewHelperNode::class ); - $introspectedViewHelpers = array_filter($usedViewHelpers, function (ViewHelperNode $node) { - return $node->getUninitializedViewHelper() instanceof IntrospectionViewHelper; - }); + $introspectedViewHelpers = array_filter($usedViewHelpers, fn(ViewHelperNode $node) => $node->getUninitializedViewHelper() instanceof IntrospectionViewHelper); return array_map(function (ViewHelperNode $node) { /** @var IntrospectionViewHelper */ $introspectionViewHelper = $node->getUninitializedViewHelper(); diff --git a/src/Classes/CodeQuality/Issue/Issue.php b/src/Classes/CodeQuality/Issue/Issue.php index 4f1673d..7908615 100644 --- a/src/Classes/CodeQuality/Issue/Issue.php +++ b/src/Classes/CodeQuality/Issue/Issue.php @@ -5,21 +5,18 @@ class Issue implements IssueInterface { - protected $checkName = ''; - protected $description; - protected $data; - - protected $categories = []; - protected $severity = IssueInterface::SEVERITY_MAJOR; - - protected $file = ''; - protected $line = null; - protected $column = null; - - public function __construct(string $description, array $data, string $file, int $line = null, int $column = null) - { - $this->description = $description; - $this->data = $data; + protected string $checkName = ''; + + protected array $categories = []; + protected string $severity = IssueInterface::SEVERITY_MAJOR; + + public function __construct( + protected string $description, + protected array $data, + protected string $file, + protected ?int $line = null, + protected ?int $column = null, + ) { $this->setLocation($file, $line, $column); } diff --git a/src/Classes/CodeQuality/Issue/IssueInterface.php b/src/Classes/CodeQuality/Issue/IssueInterface.php index 3a84cd2..4416cae 100644 --- a/src/Classes/CodeQuality/Issue/IssueInterface.php +++ b/src/Classes/CodeQuality/Issue/IssueInterface.php @@ -5,16 +5,16 @@ interface IssueInterface { - const SEVERITY_INFO = 'info'; - const SEVERITY_MINOR = 'minor'; - const SEVERITY_MAJOR = 'major'; - const SEVERITY_CRITICAL = 'critical'; - const SEVERITY_BLOCKER = 'blocker'; + public const SEVERITY_INFO = 'info'; + public const SEVERITY_MINOR = 'minor'; + public const SEVERITY_MAJOR = 'major'; + public const SEVERITY_CRITICAL = 'critical'; + public const SEVERITY_BLOCKER = 'blocker'; /** * Ordered list of severities based on their... severity */ - const SEVERITIES = [ + public const SEVERITIES = [ self::SEVERITY_INFO, self::SEVERITY_MINOR, self::SEVERITY_MAJOR, diff --git a/src/Classes/Command/LintCommand.php b/src/Classes/Command/LintCommand.php index 20994aa..eb7aa8e 100644 --- a/src/Classes/Command/LintCommand.php +++ b/src/Classes/Command/LintCommand.php @@ -6,12 +6,10 @@ use Sitegeist\FluidComponentsLinter\CodeQuality\Issue\IssueInterface; use Sitegeist\FluidComponentsLinter\CodeQuality\Output\GroupedOutput; use Sitegeist\FluidComponentsLinter\CodeQuality\Output\JsonOutput; -use Sitegeist\FluidComponentsLinter\Configuration\LintConfiguration; -use Sitegeist\FluidComponentsLinter\Exception\ConfigurationException; use Sitegeist\FluidComponentsLinter\Service\CodeQualityService; use Sitegeist\FluidComponentsLinter\Service\ComponentService; use Sitegeist\FluidComponentsLinter\Service\ConfigurationService; -use Symfony\Component\Config\Definition\Processor; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -20,19 +18,16 @@ class LintCommand extends Command { - /** * Define severities which will lead to an exit status 0 - * - * @var array */ - protected $fatalSeverities = [ + protected array $fatalSeverities = [ IssueInterface::SEVERITY_BLOCKER, IssueInterface::SEVERITY_CRITICAL, IssueInterface::SEVERITY_MAJOR ]; - protected function configure() + protected function configure(): void { $this ->setName('lint') @@ -85,7 +80,7 @@ protected function configure() ); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { try { $configurationService = new ConfigurationService; @@ -121,9 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $skipSeverities = $this->determineSeveritiesToSkip($input->getOption('severity')); if (!empty($skipSeverities)) { - $issues = array_filter($issues, function (IssueInterface $issue) use ($skipSeverities) { - return !in_array($issue->getSeverity(), $skipSeverities); - }); + $issues = array_filter($issues, fn(IssueInterface $issue) => !in_array($issue->getSeverity(), $skipSeverities)); } if ($input->getOption('json')) { @@ -154,7 +147,7 @@ protected function determineExitStatus(array $issues): int return 0; } - protected function determineSeveritiesToSkip(string $minSeverity) + protected function determineSeveritiesToSkip(string $minSeverity): array { $skipSeverities = []; foreach (IssueInterface::SEVERITIES as $severity) { diff --git a/src/Classes/Configuration/LintConfiguration.php b/src/Classes/Configuration/LintConfiguration.php index 4182173..71ba78f 100644 --- a/src/Classes/Configuration/LintConfiguration.php +++ b/src/Classes/Configuration/LintConfiguration.php @@ -9,7 +9,7 @@ class LintConfiguration implements ConfigurationInterface { - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('fclint'); /** @var TreeNode */ diff --git a/src/Classes/Fluid/ViewHelper/ViewHelperResolver.php b/src/Classes/Fluid/ViewHelper/ViewHelperResolver.php index 41dec23..0bfc74c 100644 --- a/src/Classes/Fluid/ViewHelper/ViewHelperResolver.php +++ b/src/Classes/Fluid/ViewHelper/ViewHelperResolver.php @@ -4,12 +4,13 @@ namespace Sitegeist\FluidComponentsLinter\Fluid\ViewHelper; -use TYPO3Fluid\Fluid\Core\Parser\Exception as ParserException; use Sitegeist\FluidComponentsLinter\ViewHelpers\IntrospectionViewHelper; +use TYPO3Fluid\Fluid\Core\Parser\Exception as ParserException; +use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface; class ViewHelperResolver extends \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperResolver { - public function createViewHelperInstanceFromClassName($viewHelperClassName) + public function createViewHelperInstanceFromClassName($viewHelperClassName): ViewHelperInterface { $parts = explode('\\', $viewHelperClassName); $methodIdentifier = array_pop($parts); @@ -25,7 +26,7 @@ public function resolveViewHelperClassName($namespaceIdentifier, $methodIdentifi { try { return parent::resolveViewHelperClassName($namespaceIdentifier, $methodIdentifier); - } catch (ParserException $e) { + } catch (ParserException) { // Redirect missing ViewHelpers to introspection placeholder return sprintf( '%s\\%s\\%s', diff --git a/src/Classes/Service/CodeQualityService.php b/src/Classes/Service/CodeQualityService.php index c5e70ea..5443bb7 100644 --- a/src/Classes/Service/CodeQualityService.php +++ b/src/Classes/Service/CodeQualityService.php @@ -9,22 +9,17 @@ use Sitegeist\FluidComponentsLinter\CodeQuality\Issue\IssueInterface; use Sitegeist\FluidComponentsLinter\Exception\ComponentStructureException; use Sitegeist\FluidComponentsLinter\Fluid\ViewHelper\ViewHelperResolver; +use TYPO3Fluid\Fluid\Core\Parser\Exception; use TYPO3Fluid\Fluid\View\TemplateView; class CodeQualityService { - /** @var array */ - protected $configuration; + protected ?TemplateView $view = null; - /** @var string[] */ - protected $checks; - - /** @var TemplateView */ - protected $view; - - public function __construct(array $configuration, array $checks) - { - $this->configuration = $configuration; + public function __construct( + protected array $configuration, + protected array $checks, // @var string[] + ) { $this->initializeChecks($checks); $this->initializeView(); } @@ -60,7 +55,7 @@ public function validateComponent(string $path): array file_get_contents($path), $path ); - } catch (\TYPO3Fluid\Fluid\Core\Parser\Exception $e) { + } catch (Exception $e) { preg_match('#in template .+, line ([0-9]+) at character ([0-9]+).#', $e->getMessage(), $matches); $issue = $this->blocker($e->getMessage(), $path, (int) $matches[1], (int) $matches[2]); return [$issue]; diff --git a/src/Classes/Service/ComponentService.php b/src/Classes/Service/ComponentService.php index f5e8ef0..5dbd2df 100644 --- a/src/Classes/Service/ComponentService.php +++ b/src/Classes/Service/ComponentService.php @@ -8,10 +8,8 @@ class ComponentService /** * Collection of paths that have already been scanned for components; * this prevents infinite loops caused by circular symlinks - * - * @var array */ - protected $scannedPaths; + protected array $scannedPaths = []; /** * Finds all components in the specified paths @@ -22,10 +20,10 @@ class ComponentService */ public function findComponentsInPaths(array $paths, string $ext): array { - $components = $this->scannedPaths = []; + $components = []; foreach ($paths as $path) { if (!is_dir($path)) { - if (file_exists($path) && substr($path, - strlen($ext)) == $ext) { + if (file_exists($path) && str_ends_with((string) $path, $ext)) { $components[] = $path; } continue; @@ -42,10 +40,6 @@ public function findComponentsInPaths(array $paths, string $ext): array /** * Removes all items from the provided array of component paths * that match the provided ignore list - * - * @param array $components - * @param array $ignoreList - * @return array */ public function removeComponentsFromIgnoreList(array $components, array $ignoreList): array { @@ -53,7 +47,7 @@ public function removeComponentsFromIgnoreList(array $components, array $ignoreL return $components; } - $ignorePattern = $this->buildPattern($ignoreList); + $ignorePattern = static::buildPattern($ignoreList); if (!$ignorePattern) { throw new \Exception(sprintf( 'Invalid ignore pattern provided: %s', @@ -61,17 +55,11 @@ public function removeComponentsFromIgnoreList(array $components, array $ignoreL ), 1601484307); } - return array_filter($components, function ($path) use ($ignorePattern) { - return !preg_match($ignorePattern, $path); - }); + return array_filter($components, fn($path) => !preg_match($ignorePattern, (string) $path)); } /** * Searches recursively for component files in a directory - * - * @param string $path - * @param string $ext - * @return array */ protected function scanForComponents(string $path, string $ext): array { diff --git a/src/Classes/Service/FluidService.php b/src/Classes/Service/FluidService.php index 0ced9df..a9d38ad 100644 --- a/src/Classes/Service/FluidService.php +++ b/src/Classes/Service/FluidService.php @@ -48,14 +48,14 @@ public function generateNodeExceptionPreview(NodeInterface $node): string if ($uninitializedViewHelper instanceof IntrospectionViewHelper) { return $uninitializedViewHelper->getViewhelperTag(); } else { - return get_class($uninitializedViewHelper); + return $uninitializedViewHelper::class; } } elseif ($node instanceof TextNode) { return trim($node->getText()); } elseif ($node instanceof ObjectAccessorNode) { return '{' . $node->getObjectPath() . '}'; } else { - return get_class($node); + return $node::class; } } diff --git a/src/Classes/ViewHelpers/ComponentViewHelper.php b/src/Classes/ViewHelpers/ComponentViewHelper.php index 01398ff..0fbabf2 100644 --- a/src/Classes/ViewHelpers/ComponentViewHelper.php +++ b/src/Classes/ViewHelpers/ComponentViewHelper.php @@ -9,7 +9,7 @@ class ComponentViewHelper extends AbstractViewHelper { - public function initializeArguments() + public function initializeArguments(): void { $this->registerArgument('description', 'string', 'Description of the component'); } diff --git a/src/Classes/ViewHelpers/IntrospectionViewHelper.php b/src/Classes/ViewHelpers/IntrospectionViewHelper.php index 2092f28..268cfd7 100644 --- a/src/Classes/ViewHelpers/IntrospectionViewHelper.php +++ b/src/Classes/ViewHelpers/IntrospectionViewHelper.php @@ -30,7 +30,7 @@ public function getViewhelperTagName(): string return sprintf('%s:%s', $this->namespaceIdentifier, $this->methodIdentifier); } - public function validateAdditionalArguments(array $arguments) + public function validateAdditionalArguments(array $arguments): void { // Allow all arguments } diff --git a/src/Classes/ViewHelpers/ParamViewHelper.php b/src/Classes/ViewHelpers/ParamViewHelper.php index ba62216..c157894 100644 --- a/src/Classes/ViewHelpers/ParamViewHelper.php +++ b/src/Classes/ViewHelpers/ParamViewHelper.php @@ -10,7 +10,7 @@ class ParamViewHelper extends AbstractViewHelper { - public function initializeArguments() + public function initializeArguments(): void { $this->registerArgument('name', 'string', 'Parameter name', true); $this->registerArgument('type', 'string', 'Parameter type', true); diff --git a/src/FcLint.php b/src/FcLint.php index 8ab1c6d..c42ff77 100644 --- a/src/FcLint.php +++ b/src/FcLint.php @@ -9,7 +9,7 @@ $autoloadLocations = [ $GLOBALS['_composer_autoload_path'] ?? null, dirname(__DIR__) . '/vendor/autoload.php', - dirname(dirname(dirname(__DIR__))) . '/autoload.php' + dirname(__DIR__, 3) . '/autoload.php' ]; $autoloadLocations = array_filter(array_filter($autoloadLocations), 'file_exists');