-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDoNotUseImplicitVariableInConfigurationFilesRule.php
76 lines (62 loc) · 2.15 KB
/
DoNotUseImplicitVariableInConfigurationFilesRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace Ssch\Typo3PhpstanRules\Rules;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use Ssch\Typo3PhpstanRules\FileResolver;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @implements Rule<Variable>
*/
final class DoNotUseImplicitVariableInConfigurationFilesRule implements Rule, DocumentedRuleInterface
{
/**
* @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 getNodeType(): string
{
return Variable::class;
}
public function processNode(Node $node, Scope $scope): array
{
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];
}
}