Skip to content

Commit df6e2cb

Browse files
committed
improve readability and use PHP 8 features
With the help of rector and ☕
1 parent 50b7123 commit df6e2cb

16 files changed

+53
-85
lines changed

src/Classes/CodeQuality/Check/ComponentVariablesCheck.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ protected function extractUsedVariables(): array
3636
$this->component->rootNode,
3737
ObjectAccessorNode::class
3838
);
39-
return array_map(function (ObjectAccessorNode $node) {
40-
return $node->getObjectPath();
41-
}, $usedVariables);
39+
return array_map(fn(ObjectAccessorNode $node) => $node->getObjectPath(), $usedVariables);
4240
}
4341
}

src/Classes/CodeQuality/Check/DocumentationFixtureCheck.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ public function check(): array
3535
return $issues;
3636
}
3737

38-
$fixtureFiles = array_map(function ($fixtureFile) use ($directory, $name) {
39-
return sprintf($fixtureFile, $directory, $name);
40-
}, $this->fixtureFiles);
38+
$fixtureFiles = array_map(fn($fixtureFile) => sprintf($fixtureFile, $directory, $name), $this->fixtureFiles);
4139
$fixtureFile = array_filter($fixtureFiles, 'file_exists');
4240
$fixtureFile = reset($fixtureFile);
4341

src/Classes/CodeQuality/Check/ParamTypeNamespaceCheck.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function check(): array
1515
$issues = [];
1616
foreach ($this->component->paramNodes as $paramNode) {
1717
$arguments = $paramNode->getArguments();
18-
if (substr((string) $arguments['type'], 0, 1) === '\\') {
18+
if (str_starts_with((string) $arguments['type'], '\\')) {
1919
$issues[] = $this->issue('Type is specified with leading backslash for parameter %s: %s', [
2020
$arguments['name'],
2121
$arguments['type']

src/Classes/CodeQuality/Check/ViewHelpersCheck.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function check(): array
1616
foreach ($this->configuration['renderer']['viewHelperRestrictions'] as $restriction) {
1717
$pattern = $this->createViewHelperPattern($restriction['viewHelperName']);
1818
foreach ($usedViewHelpers as $tagName) {
19-
if (preg_match($pattern, $tagName)) {
19+
if (preg_match($pattern, (string) $tagName)) {
2020
$issues[] = $this->issue($restriction['message'], [
2121
$tagName
2222
])->setSeverity($restriction['severity']);
@@ -30,7 +30,7 @@ public function check(): array
3030
protected function createViewHelperPattern(string $viewHelperName): string
3131
{
3232
$pattern = preg_quote($viewHelperName, '#');
33-
if (substr($viewHelperName, -1, 1) !== '.') {
33+
if (!str_ends_with($viewHelperName, '.')) {
3434
$pattern .= '$';
3535
}
3636
return '#^' . $pattern . '#';
@@ -42,9 +42,7 @@ protected function extractUsedViewHelpers(): array
4242
$this->component->rootNode,
4343
ViewHelperNode::class
4444
);
45-
$introspectedViewHelpers = array_filter($usedViewHelpers, function (ViewHelperNode $node) {
46-
return $node->getUninitializedViewHelper() instanceof IntrospectionViewHelper;
47-
});
45+
$introspectedViewHelpers = array_filter($usedViewHelpers, fn(ViewHelperNode $node) => $node->getUninitializedViewHelper() instanceof IntrospectionViewHelper);
4846
return array_map(function (ViewHelperNode $node) {
4947
/** @var IntrospectionViewHelper */
5048
$introspectionViewHelper = $node->getUninitializedViewHelper();

src/Classes/CodeQuality/Issue/Issue.php

+12-15
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@
55

66
class Issue implements IssueInterface
77
{
8-
protected $checkName = '';
9-
protected $description;
10-
protected $data;
11-
12-
protected $categories = [];
13-
protected $severity = IssueInterface::SEVERITY_MAJOR;
14-
15-
protected $file = '';
16-
protected $line = null;
17-
protected $column = null;
18-
19-
public function __construct(string $description, array $data, string $file, int $line = null, int $column = null)
20-
{
21-
$this->description = $description;
22-
$this->data = $data;
8+
protected string $checkName = '';
9+
10+
protected array $categories = [];
11+
protected string $severity = IssueInterface::SEVERITY_MAJOR;
12+
13+
public function __construct(
14+
protected string $description,
15+
protected array $data,
16+
protected string $file,
17+
protected ?int $line = null,
18+
protected ?int $column = null,
19+
) {
2320
$this->setLocation($file, $line, $column);
2421
}
2522

src/Classes/CodeQuality/Issue/IssueInterface.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
interface IssueInterface
77
{
8-
const SEVERITY_INFO = 'info';
9-
const SEVERITY_MINOR = 'minor';
10-
const SEVERITY_MAJOR = 'major';
11-
const SEVERITY_CRITICAL = 'critical';
12-
const SEVERITY_BLOCKER = 'blocker';
8+
public const SEVERITY_INFO = 'info';
9+
public const SEVERITY_MINOR = 'minor';
10+
public const SEVERITY_MAJOR = 'major';
11+
public const SEVERITY_CRITICAL = 'critical';
12+
public const SEVERITY_BLOCKER = 'blocker';
1313

1414
/**
1515
* Ordered list of severities based on their... severity
1616
*/
17-
const SEVERITIES = [
17+
public const SEVERITIES = [
1818
self::SEVERITY_INFO,
1919
self::SEVERITY_MINOR,
2020
self::SEVERITY_MAJOR,

src/Classes/Command/LintCommand.php

+6-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
use Sitegeist\FluidComponentsLinter\CodeQuality\Issue\IssueInterface;
77
use Sitegeist\FluidComponentsLinter\CodeQuality\Output\GroupedOutput;
88
use Sitegeist\FluidComponentsLinter\CodeQuality\Output\JsonOutput;
9-
use Sitegeist\FluidComponentsLinter\Configuration\LintConfiguration;
10-
use Sitegeist\FluidComponentsLinter\Exception\ConfigurationException;
119
use Sitegeist\FluidComponentsLinter\Service\CodeQualityService;
1210
use Sitegeist\FluidComponentsLinter\Service\ComponentService;
1311
use Sitegeist\FluidComponentsLinter\Service\ConfigurationService;
14-
use Symfony\Component\Config\Definition\Processor;
12+
use Symfony\Component\Console\Attribute\AsCommand;
1513
use Symfony\Component\Console\Command\Command;
1614
use Symfony\Component\Console\Input\InputArgument;
1715
use Symfony\Component\Console\Input\InputInterface;
@@ -20,19 +18,16 @@
2018

2119
class LintCommand extends Command
2220
{
23-
2421
/**
2522
* Define severities which will lead to an exit status 0
26-
*
27-
* @var array
2823
*/
29-
protected $fatalSeverities = [
24+
protected array $fatalSeverities = [
3025
IssueInterface::SEVERITY_BLOCKER,
3126
IssueInterface::SEVERITY_CRITICAL,
3227
IssueInterface::SEVERITY_MAJOR
3328
];
3429

35-
protected function configure()
30+
protected function configure(): void
3631
{
3732
$this
3833
->setName('lint')
@@ -85,7 +80,7 @@ protected function configure()
8580
);
8681
}
8782

88-
protected function execute(InputInterface $input, OutputInterface $output)
83+
protected function execute(InputInterface $input, OutputInterface $output): int
8984
{
9085
try {
9186
$configurationService = new ConfigurationService;
@@ -121,9 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
121116

122117
$skipSeverities = $this->determineSeveritiesToSkip($input->getOption('severity'));
123118
if (!empty($skipSeverities)) {
124-
$issues = array_filter($issues, function (IssueInterface $issue) use ($skipSeverities) {
125-
return !in_array($issue->getSeverity(), $skipSeverities);
126-
});
119+
$issues = array_filter($issues, fn(IssueInterface $issue) => !in_array($issue->getSeverity(), $skipSeverities));
127120
}
128121

129122
if ($input->getOption('json')) {
@@ -154,7 +147,7 @@ protected function determineExitStatus(array $issues): int
154147
return 0;
155148
}
156149

157-
protected function determineSeveritiesToSkip(string $minSeverity)
150+
protected function determineSeveritiesToSkip(string $minSeverity): array
158151
{
159152
$skipSeverities = [];
160153
foreach (IssueInterface::SEVERITIES as $severity) {

src/Classes/Configuration/LintConfiguration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class LintConfiguration implements ConfigurationInterface
1111
{
12-
public function getConfigTreeBuilder()
12+
public function getConfigTreeBuilder(): TreeBuilder
1313
{
1414
$treeBuilder = new TreeBuilder('fclint');
1515
/** @var TreeNode */

src/Classes/Fluid/ViewHelper/ViewHelperResolver.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
namespace Sitegeist\FluidComponentsLinter\Fluid\ViewHelper;
66

7-
use TYPO3Fluid\Fluid\Core\Parser\Exception as ParserException;
87
use Sitegeist\FluidComponentsLinter\ViewHelpers\IntrospectionViewHelper;
8+
use TYPO3Fluid\Fluid\Core\Parser\Exception as ParserException;
9+
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;
910

1011
class ViewHelperResolver extends \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperResolver
1112
{
12-
public function createViewHelperInstanceFromClassName($viewHelperClassName)
13+
public function createViewHelperInstanceFromClassName($viewHelperClassName): ViewHelperInterface
1314
{
1415
$parts = explode('\\', $viewHelperClassName);
1516
$methodIdentifier = array_pop($parts);
@@ -25,7 +26,7 @@ public function resolveViewHelperClassName($namespaceIdentifier, $methodIdentifi
2526
{
2627
try {
2728
return parent::resolveViewHelperClassName($namespaceIdentifier, $methodIdentifier);
28-
} catch (ParserException $e) {
29+
} catch (ParserException) {
2930
// Redirect missing ViewHelpers to introspection placeholder
3031
return sprintf(
3132
'%s\\%s\\%s',

src/Classes/Service/CodeQualityService.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,17 @@
99
use Sitegeist\FluidComponentsLinter\CodeQuality\Issue\IssueInterface;
1010
use Sitegeist\FluidComponentsLinter\Exception\ComponentStructureException;
1111
use Sitegeist\FluidComponentsLinter\Fluid\ViewHelper\ViewHelperResolver;
12+
use TYPO3Fluid\Fluid\Core\Parser\Exception;
1213
use TYPO3Fluid\Fluid\View\TemplateView;
1314

1415
class CodeQualityService
1516
{
16-
/** @var array */
17-
protected $configuration;
17+
protected ?TemplateView $view = null;
1818

19-
/** @var string[] */
20-
protected $checks;
21-
22-
/** @var TemplateView */
23-
protected $view;
24-
25-
public function __construct(array $configuration, array $checks)
26-
{
27-
$this->configuration = $configuration;
19+
public function __construct(
20+
protected array $configuration,
21+
protected array $checks, // @var string[]
22+
) {
2823
$this->initializeChecks($checks);
2924
$this->initializeView();
3025
}
@@ -60,7 +55,7 @@ public function validateComponent(string $path): array
6055
file_get_contents($path),
6156
$path
6257
);
63-
} catch (\TYPO3Fluid\Fluid\Core\Parser\Exception $e) {
58+
} catch (Exception $e) {
6459
preg_match('#in template .+, line ([0-9]+) at character ([0-9]+).#', $e->getMessage(), $matches);
6560
$issue = $this->blocker($e->getMessage(), $path, (int) $matches[1], (int) $matches[2]);
6661
return [$issue];

src/Classes/Service/ComponentService.php

+5-17
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ class ComponentService
88
/**
99
* Collection of paths that have already been scanned for components;
1010
* this prevents infinite loops caused by circular symlinks
11-
*
12-
* @var array
1311
*/
14-
protected $scannedPaths;
12+
protected array $scannedPaths = [];
1513

1614
/**
1715
* Finds all components in the specified paths
@@ -22,10 +20,10 @@ class ComponentService
2220
*/
2321
public function findComponentsInPaths(array $paths, string $ext): array
2422
{
25-
$components = $this->scannedPaths = [];
23+
$components = [];
2624
foreach ($paths as $path) {
2725
if (!is_dir($path)) {
28-
if (file_exists($path) && substr($path, - strlen($ext)) == $ext) {
26+
if (file_exists($path) && str_ends_with((string) $path, $ext)) {
2927
$components[] = $path;
3028
}
3129
continue;
@@ -42,36 +40,26 @@ public function findComponentsInPaths(array $paths, string $ext): array
4240
/**
4341
* Removes all items from the provided array of component paths
4442
* that match the provided ignore list
45-
*
46-
* @param array $components
47-
* @param array $ignoreList
48-
* @return array
4943
*/
5044
public function removeComponentsFromIgnoreList(array $components, array $ignoreList): array
5145
{
5246
if (empty($ignoreList)) {
5347
return $components;
5448
}
5549

56-
$ignorePattern = $this->buildPattern($ignoreList);
50+
$ignorePattern = static::buildPattern($ignoreList);
5751
if (!$ignorePattern) {
5852
throw new \Exception(sprintf(
5953
'Invalid ignore pattern provided: %s',
6054
print_r($ignoreList, true)
6155
), 1601484307);
6256
}
6357

64-
return array_filter($components, function ($path) use ($ignorePattern) {
65-
return !preg_match($ignorePattern, $path);
66-
});
58+
return array_filter($components, fn($path) => !preg_match($ignorePattern, (string) $path));
6759
}
6860

6961
/**
7062
* Searches recursively for component files in a directory
71-
*
72-
* @param string $path
73-
* @param string $ext
74-
* @return array
7563
*/
7664
protected function scanForComponents(string $path, string $ext): array
7765
{

src/Classes/Service/FluidService.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ public function generateNodeExceptionPreview(NodeInterface $node): string
4848
if ($uninitializedViewHelper instanceof IntrospectionViewHelper) {
4949
return $uninitializedViewHelper->getViewhelperTag();
5050
} else {
51-
return get_class($uninitializedViewHelper);
51+
return $uninitializedViewHelper::class;
5252
}
5353
} elseif ($node instanceof TextNode) {
5454
return trim($node->getText());
5555
} elseif ($node instanceof ObjectAccessorNode) {
5656
return '{' . $node->getObjectPath() . '}';
5757
} else {
58-
return get_class($node);
58+
return $node::class;
5959
}
6060
}
6161

src/Classes/ViewHelpers/ComponentViewHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class ComponentViewHelper extends AbstractViewHelper
1111
{
12-
public function initializeArguments()
12+
public function initializeArguments(): void
1313
{
1414
$this->registerArgument('description', 'string', 'Description of the component');
1515
}

src/Classes/ViewHelpers/IntrospectionViewHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function getViewhelperTagName(): string
3030
return sprintf('%s:%s', $this->namespaceIdentifier, $this->methodIdentifier);
3131
}
3232

33-
public function validateAdditionalArguments(array $arguments)
33+
public function validateAdditionalArguments(array $arguments): void
3434
{
3535
// Allow all arguments
3636
}

src/Classes/ViewHelpers/ParamViewHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class ParamViewHelper extends AbstractViewHelper
1212
{
13-
public function initializeArguments()
13+
public function initializeArguments(): void
1414
{
1515
$this->registerArgument('name', 'string', 'Parameter name', true);
1616
$this->registerArgument('type', 'string', 'Parameter type', true);

src/FcLint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
$autoloadLocations = [
1010
$GLOBALS['_composer_autoload_path'] ?? null,
1111
dirname(__DIR__) . '/vendor/autoload.php',
12-
dirname(dirname(dirname(__DIR__))) . '/autoload.php'
12+
dirname(__DIR__, 3) . '/autoload.php'
1313
];
1414
$autoloadLocations = array_filter(array_filter($autoloadLocations), 'file_exists');
1515

0 commit comments

Comments
 (0)