Skip to content

Commit

Permalink
Add rule to check for implicit variables
Browse files Browse the repository at this point in the history
Resolves: #16
  • Loading branch information
sabbelasichon committed Jan 14, 2022
1 parent 7c6aa36 commit 573b888
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 9 deletions.
76 changes: 76 additions & 0 deletions src/Rules/DoNotUseImplicitVariableInConfigurationFilesRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Ssch\Typo3PhpstanRules\Rules;

use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use Ssch\Typo3PhpstanRules\FileResolver;
use Symplify\PHPStanRules\Rules\AbstractSymplifyRule;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class DoNotUseImplicitVariableInConfigurationFilesRule extends AbstractSymplifyRule
{
/**
* @var string
*/
private const MESSAGE = 'Do not use implicit variable "$%s" in file "%s"';

/**
* @var string[]
*/
private const DISALLOWED_VARIABLES = ['_EXTKEY', '_EXTCONF'];

private FileResolver $fileResolver;

public function __construct(FileResolver $fileResolver)
{
$this->fileResolver = $fileResolver;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Do not use $_EXTKEY and $_EXTCONF in ext_localconf.php or ext_tables.php', [
new CodeSample(
<<<'CODE_SAMPLE'
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-preProcess'][$_EXTKEY] = \Prefix\MyExtension\PageRendererHooks::class . '->renderPreProcess';
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-preProcess']['my_extension_key'] = \Prefix\MyExtension\PageRendererHooks::class . '->renderPreProcess';
CODE_SAMPLE
),
]);
}

public function getNodeTypes(): array
{
return [Node\Expr\Variable::class];
}

public function process(Node $node, Scope $scope): array
{
if (! $node instanceof Variable) {
return [];
}

if (! $this->fileResolver->isConfigurationFile($scope)) {
return [];
}

if (! is_string($node->name)) {
return [];
}

if (! in_array($node->name, self::DISALLOWED_VARIABLES, true)) {
return [];
}

$message = sprintf(self::MESSAGE, $node->name, $scope->getFile());

return [$message];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getRuleDefinition(): RuleDefinition
__DIR__ . '/Resources/Private/Templates/Template.html;
CODE_SAMPLE
,
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::class . '::extPath(\'my_extension\') . \'/Resources/Private/Templates/Template.html;'
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::class . "::extPath('my_extension') . '/Resources/Private/Templates/Template.html;"
),
]);
}
Expand Down
18 changes: 11 additions & 7 deletions src/Rules/MissingDefaultValueForTypedPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@ public function __construct(EntityClassDetector $entityClassDetector)

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
return new RuleDefinition(
'Missing default value for property "property" in class "MissingDefaultValueForTypedProperty"',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class MissingDefaultValueForTypedProperty extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
protected string $property;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
,
<<<'CODE_SAMPLE'
final class MissingDefaultValueForTypedProperty extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
protected string $property = '';
}
CODE_SAMPLE
),
]);
),

]
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(EntityClassDetector $entityClassDetector)

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
return new RuleDefinition('Missing var annotation for property "property" in class "MissingDocblock"', [
new CodeSample(
<<<'CODE_SAMPLE'
final class MissingDocblock extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Ssch\Typo3PhpstanRules\Tests\Rules\DoNotUseMagicConstantFileAndDirInConfigurationFilesRule;

use Iterator;
use PHPStan\Rules\Rule;
use Ssch\Typo3PhpstanRules\Rules\DoNotUseImplicitVariableInConfigurationFilesRule;
use Symplify\PHPStanExtensions\Testing\AbstractServiceAwareRuleTestCase;

/**
* @extends AbstractServiceAwareRuleTestCase<DoNotUseImplicitVariableInConfigurationFilesRule>
*/
final class DoNotUseImplicitVariableInConfigurationFilesRuleTest extends AbstractServiceAwareRuleTestCase
{
/**
* @dataProvider provideData()
*
* @param array<string|int> $expectedErrorMessagesWithLines
*/
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void
{
$this->analyse([$filePath], $expectedErrorMessagesWithLines);
}

public function provideData(): Iterator
{
$invalidFile = __DIR__ . '/MyExtension/ext_localconf.php';

yield [
$invalidFile,
[
[sprintf('Do not use implicit variable "$_EXTCONF" in file "%s"', $invalidFile), 3],
[sprintf('Do not use implicit variable "$_EXTKEY" in file "%s"', $invalidFile), 5],
],
];

yield [__DIR__ . '/MySecondExtension/ext_localconf.php', []];
}

protected function getRule(): Rule
{
return $this->getRuleFromConfig(
DoNotUseImplicitVariableInConfigurationFilesRule::class,
__DIR__ . '/config/configured_rule.neon'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$foo = $_EXTCONF['foo'];

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-preProcess'][$_EXTKEY] = \Prefix\MyExtension\PageRendererHooks::class . '->renderPreProcess';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- ../../../config/included_services.neon

services:
-
class: Ssch\Typo3PhpstanRules\Rules\DoNotUseImplicitVariableInConfigurationFilesRule
tags: [phpstan.rules.rule]

0 comments on commit 573b888

Please sign in to comment.