-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule to check for implicit variables
Resolves: #16
- Loading branch information
1 parent
7c6aa36
commit 573b888
Showing
8 changed files
with
153 additions
and
9 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/Rules/DoNotUseImplicitVariableInConfigurationFilesRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...VariableInConfigurationFilesRule/DoNotUseImplicitVariableInConfigurationFilesRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/MyExtension/ext_localconf.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
3 changes: 3 additions & 0 deletions
3
...ules/DoNotUseImplicitVariableInConfigurationFilesRule/MySecondExtension/ext_localconf.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
|
7 changes: 7 additions & 0 deletions
7
tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/config/configured_rule.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |