Skip to content

Commit

Permalink
improve readability and use PHP 8 features
Browse files Browse the repository at this point in the history
With the help of rector and ☕
  • Loading branch information
ulrichmathes committed Jul 17, 2024
1 parent 50b7123 commit df6e2cb
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 85 deletions.
4 changes: 1 addition & 3 deletions src/Classes/CodeQuality/Check/ComponentVariablesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 1 addition & 3 deletions src/Classes/CodeQuality/Check/DocumentationFixtureCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Classes/CodeQuality/Check/ParamTypeNamespaceCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
8 changes: 3 additions & 5 deletions src/Classes/CodeQuality/Check/ViewHelpersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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 . '#';
Expand All @@ -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();
Expand Down
27 changes: 12 additions & 15 deletions src/Classes/CodeQuality/Issue/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Classes/CodeQuality/Issue/IssueInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 6 additions & 13 deletions src/Classes/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/Configuration/LintConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class LintConfiguration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('fclint');
/** @var TreeNode */
Expand Down
7 changes: 4 additions & 3 deletions src/Classes/Fluid/ViewHelper/ViewHelperResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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',
Expand Down
19 changes: 7 additions & 12 deletions src/Classes/Service/CodeQualityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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];
Expand Down
22 changes: 5 additions & 17 deletions src/Classes/Service/ComponentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -42,36 +40,26 @@ 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
{
if (empty($ignoreList)) {
return $components;
}

$ignorePattern = $this->buildPattern($ignoreList);
$ignorePattern = static::buildPattern($ignoreList);
if (!$ignorePattern) {
throw new \Exception(sprintf(
'Invalid ignore pattern provided: %s',
print_r($ignoreList, true)
), 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
{
Expand Down
4 changes: 2 additions & 2 deletions src/Classes/Service/FluidService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Classes/ViewHelpers/ComponentViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ComponentViewHelper extends AbstractViewHelper
{
public function initializeArguments()
public function initializeArguments(): void
{
$this->registerArgument('description', 'string', 'Description of the component');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/ViewHelpers/IntrospectionViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/ViewHelpers/ParamViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/FcLint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit df6e2cb

Please sign in to comment.