diff --git a/src/Rules/DoNotUseImplicitVariableInConfigurationFilesRule.php b/src/Rules/DoNotUseImplicitVariableInConfigurationFilesRule.php new file mode 100644 index 0000000..a65ebc0 --- /dev/null +++ b/src/Rules/DoNotUseImplicitVariableInConfigurationFilesRule.php @@ -0,0 +1,76 @@ +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]; + } +} diff --git a/src/Rules/DoNotUseMagicConstantFileAndDirInConfigurationFilesRule.php b/src/Rules/DoNotUseMagicConstantFileAndDirInConfigurationFilesRule.php index 345eb2c..57f5a7a 100644 --- a/src/Rules/DoNotUseMagicConstantFileAndDirInConfigurationFilesRule.php +++ b/src/Rules/DoNotUseMagicConstantFileAndDirInConfigurationFilesRule.php @@ -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;" ), ]); } diff --git a/src/Rules/MissingDefaultValueForTypedPropertyRule.php b/src/Rules/MissingDefaultValueForTypedPropertyRule.php index feb2b03..3f90a77 100644 --- a/src/Rules/MissingDefaultValueForTypedPropertyRule.php +++ b/src/Rules/MissingDefaultValueForTypedPropertyRule.php @@ -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 - ), - ]); + ), + + ] + ); } /** diff --git a/src/Rules/MissingVarAnnotationForPropertyInEntityClassRule.php b/src/Rules/MissingVarAnnotationForPropertyInEntityClassRule.php index 92bd4f3..52b8046 100644 --- a/src/Rules/MissingVarAnnotationForPropertyInEntityClassRule.php +++ b/src/Rules/MissingVarAnnotationForPropertyInEntityClassRule.php @@ -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 diff --git a/tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/DoNotUseImplicitVariableInConfigurationFilesRuleTest.php b/tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/DoNotUseImplicitVariableInConfigurationFilesRuleTest.php new file mode 100644 index 0000000..bfad050 --- /dev/null +++ b/tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/DoNotUseImplicitVariableInConfigurationFilesRuleTest.php @@ -0,0 +1,49 @@ + + */ +final class DoNotUseImplicitVariableInConfigurationFilesRuleTest extends AbstractServiceAwareRuleTestCase +{ + /** + * @dataProvider provideData() + * + * @param array $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' + ); + } +} diff --git a/tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/MyExtension/ext_localconf.php b/tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/MyExtension/ext_localconf.php new file mode 100644 index 0000000..e2e2e60 --- /dev/null +++ b/tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/MyExtension/ext_localconf.php @@ -0,0 +1,5 @@ +renderPreProcess'; diff --git a/tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/MySecondExtension/ext_localconf.php b/tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/MySecondExtension/ext_localconf.php new file mode 100644 index 0000000..50cce95 --- /dev/null +++ b/tests/Rules/DoNotUseImplicitVariableInConfigurationFilesRule/MySecondExtension/ext_localconf.php @@ -0,0 +1,3 @@ +