From 4ac34503d1c09a0f2a3b85f9343092473f773d38 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 9 May 2017 14:48:44 +0200 Subject: [PATCH 1/3] FEATURE: Check removed globals * Add feature, tests and configuration. * Refactor to follow DRY, move prepareStructure to abstract. Resolves: #77 --- .../Configuration/Removed/Globals/7.0.yaml | 8 +++ src/Standards/Typo3Update/Options.php | 13 ++++ .../Sniffs/ExtendedPhpCsSupportTrait.php | 22 +++++++ .../Sniffs/Removed/AbstractGenericUsage.php | 21 ++++++ .../Sniffs/Removed/GenericGlobalSniff.php | 66 +++++++++++++++++++ .../Removed/GenericGlobalSniff/Expected.json | 33 ++++++++++ .../GenericGlobalSniff/InputFileForIssues.php | 27 ++++++++ .../Sniffs/Removed/GenericGlobalSniffTest.php | 28 ++++++++ 8 files changed, 218 insertions(+) create mode 100644 src/Standards/Typo3Update/Configuration/Removed/Globals/7.0.yaml create mode 100644 src/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff.php create mode 100644 tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/Expected.json create mode 100644 tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/InputFileForIssues.php create mode 100644 tests/Sniffs/Removed/GenericGlobalSniffTest.php diff --git a/src/Standards/Typo3Update/Configuration/Removed/Globals/7.0.yaml b/src/Standards/Typo3Update/Configuration/Removed/Globals/7.0.yaml new file mode 100644 index 0000000..90a5d12 --- /dev/null +++ b/src/Standards/Typo3Update/Configuration/Removed/Globals/7.0.yaml @@ -0,0 +1,8 @@ +# Breaking changes in 7.0: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Index.html#breaking-changes +'7.0': + typo3CacheManager: + replacement: 'Use GeneralUtility::makeInstance() instead' + docsUrl: 'https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-62416-DeprecatedCodeRemovalInCoreSysext.html#removed-php-classes' + typo3CacheFactory: + replacement: 'Use GeneralUtility::makeInstance() instead' + docsUrl: 'https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-62416-DeprecatedCodeRemovalInCoreSysext.html#removed-php-classes' diff --git a/src/Standards/Typo3Update/Options.php b/src/Standards/Typo3Update/Options.php index f5a4777..ea7954d 100644 --- a/src/Standards/Typo3Update/Options.php +++ b/src/Standards/Typo3Update/Options.php @@ -121,6 +121,19 @@ class Options ); } + /** + * Returns an array of absolute file names containing removed globals configurations. + * + * @return array + */ + public static function getRemovedGlobalConfigFiles() + { + return static::getOptionFileNames( + 'removedGlobalConfigFiles', + __DIR__ . '/Configuration/Removed/Globals/*.yaml' + ); + } + /** * Get the option by optionName, if not defined, use default. * diff --git a/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php b/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php index 2368d4d..ad4385e 100644 --- a/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php +++ b/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php @@ -66,4 +66,26 @@ trait ExtendedPhpCsSupportTrait return true; } + + /** + * Check whether variable at $stackPtr is global. + * + * @param PhpCsFile $phpcsFile + * @param int $stackPtr + * + * @return bool + */ + protected function isGlobalVariable(PhpCsFile $phpcsFile, $stackPtr) + { + $position = $phpcsFile->findPrevious(T_GLOBAL, $stackPtr, null, false, null, true); + if ($position !== false) { + return true; + } + + if ($phpcsFile->getTokens()[$stackPtr]['content'] === '$GLOBALS') { + return true; + } + + return false; + } } diff --git a/src/Standards/Typo3Update/Sniffs/Removed/AbstractGenericUsage.php b/src/Standards/Typo3Update/Sniffs/Removed/AbstractGenericUsage.php index 557dcb5..e20d8e0 100644 --- a/src/Standards/Typo3Update/Sniffs/Removed/AbstractGenericUsage.php +++ b/src/Standards/Typo3Update/Sniffs/Removed/AbstractGenericUsage.php @@ -33,6 +33,27 @@ abstract class AbstractGenericUsage extends BaseAbstractYamlRemovedUsage impleme */ abstract protected function findRemoved(PhpCsFile $phpcsFile, $stackPtr); + /** + * @param array $typo3Versions + * @return array + */ + protected function prepareStructure(array $typo3Versions) + { + $newStructure = []; + + foreach ($typo3Versions as $typo3Version => $removals) { + foreach ($removals as $removed => $config) { + $config['name'] = $removed; + $config['identifier'] = $removed; + $config['oldUsage'] = $removed; + $config['versionRemoved'] = $typo3Version; + $newStructure[$removed] = $config; + } + } + + return $newStructure; + } + /** * @param PhpCsFile $phpcsFile * @param int $stackPtr diff --git a/src/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff.php b/src/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff.php new file mode 100644 index 0000000..1aabb4c --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff.php @@ -0,0 +1,66 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use PHP_CodeSniffer_File as PhpCsFile; +use Typo3Update\Options; +use Typo3Update\Sniffs\Removed\AbstractGenericUsage; +use Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; + +class Typo3Update_Sniffs_Removed_GenericGlobalSniff extends AbstractGenericUsage +{ + use ExtendedPhpCsSupportTrait; + + public function register() + { + return [T_VARIABLE]; + } + + /** + * @param PhpCsFile $phpcsFile + * @param int $stackPtr + * @return array + */ + protected function findRemoved(PhpCsFile $phpcsFile, $stackPtr) + { + if ($this->isGlobalVariable($phpcsFile, $stackPtr) === false) { + return []; + } + + $variableName = substr($phpcsFile->getTokens()[$stackPtr]['content'], 1); + if ($variableName === 'GLOBALS') { + $variableName = trim( + $phpcsFile->getTokens()[$phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr)]['content'], + '\'"' + ); + } + + if ($this->configured->isRemoved($variableName)) { + return [$this->configured->getRemoved($variableName)]; + } + + return []; + } + + protected function getRemovedConfigFiles() + { + return Options::getRemovedGlobalConfigFiles(); + } +} diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/Expected.json b/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/Expected.json new file mode 100644 index 0000000..4e2c2b9 --- /dev/null +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/Expected.json @@ -0,0 +1,33 @@ +{ + "files": { + "InputFileForIssues.php": { + "errors": 0, + "messages": [ + { + "column": 12, + "fixable": false, + "line": 24, + "message": "Calls to removed code are not allowed; found typo3CacheFactory. Removed in 7.0. Use GeneralUtility::makeInstance() instead. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-62416-DeprecatedCodeRemovalInCoreSysext.html#removed-php-classes", + "severity": 5, + "source": "Typo3Update.Removed.GenericGlobal.typo3CacheFactory", + "type": "WARNING" + }, + { + "column": 13, + "fixable": false, + "line": 25, + "message": "Calls to removed code are not allowed; found typo3CacheManager. Removed in 7.0. Use GeneralUtility::makeInstance() instead. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-62416-DeprecatedCodeRemovalInCoreSysext.html#removed-php-classes", + "severity": 5, + "source": "Typo3Update.Removed.GenericGlobal.typo3CacheManager", + "type": "WARNING" + } + ], + "warnings": 2 + } + }, + "totals": { + "errors": 0, + "fixable": 0, + "warnings": 2 + } +} diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/InputFileForIssues.php b/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/InputFileForIssues.php new file mode 100644 index 0000000..d63cfd4 --- /dev/null +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/InputFileForIssues.php @@ -0,0 +1,27 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +function name() +{ + global $typo3CacheFactory; + $typo3CacheManager = $GLOBALS['typo3CacheManager']; + $typo3CacheManager->something(); +} diff --git a/tests/Sniffs/Removed/GenericGlobalSniffTest.php b/tests/Sniffs/Removed/GenericGlobalSniffTest.php new file mode 100644 index 0000000..b19e40d --- /dev/null +++ b/tests/Sniffs/Removed/GenericGlobalSniffTest.php @@ -0,0 +1,28 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use Typo3Update\Tests\SniffsTest; + +class GenericGlobalSniffTest extends SniffsTest +{ +} From 219103561e4abe76d0a99bc9b8737a1fff9bb9d9 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 9 May 2017 15:03:35 +0200 Subject: [PATCH 2/3] TASK: Update docs * To reflect new feature. Relates: #77 --- Documentation/source/configuration.rst | 22 ++++++++++++++++++++++ Documentation/source/features.rst | 3 +++ 2 files changed, 25 insertions(+) diff --git a/Documentation/source/configuration.rst b/Documentation/source/configuration.rst index 5cc0c0e..a4efe38 100644 --- a/Documentation/source/configuration.rst +++ b/Documentation/source/configuration.rst @@ -187,6 +187,28 @@ Using ``runtime-set``: --runtime-set removedClassConfigFiles "/Some/Absolute/Path/*.yaml" +.. _configuration-removedGlobalConfigFiles: + +removedGlobalConfigFiles +^^^^^^^^^^^^^^^^^^^^^^^^ + +Configure where to look for configuration files defining the removed globals. Default is +``Configuration/Removed/Globals/*.yaml`` inside the standard itself. We already try to deliver as +much as possible. Globing is used, so placeholders like ``*`` are possible, see +https://secure.php.net/manual/en/function.glob.php + +Using :file:`ruleset.xml`: + +.. code:: xml + + + +Using ``runtime-set``: + +.. code:: bash + + --runtime-set removedGlobalConfigFiles "/Some/Absolute/Path/*.yaml" + .. _configuration-removedTypoScriptConfigFiles: removedTypoScriptConfigFiles diff --git a/Documentation/source/features.rst b/Documentation/source/features.rst index 7576335..69cd6ca 100644 --- a/Documentation/source/features.rst +++ b/Documentation/source/features.rst @@ -106,6 +106,9 @@ functions. For configuration options see :ref:`configuration-removedConstantConf Check for usage of *removed PHP classes*. The classes are configured in same way as removed functions. For configuration options see :ref:`configuration-removedClassConfigFiles`. +Check for usage of *removed PHP globals*. The globals are configured in same way as removed +functions. For configuration options see :ref:`configuration-removedGlobalConfigFiles`. + Check for usage of *removed TypoScript*. The TypoScript objects are configured in same way as removed functions. For configuration options see :ref:`configuration-removedTypoScriptConfigFiles`. This will check whether you are using already removed TypoScript parts, supported are: From 7aafc28dbfc884d8fe10ab6df5b5bf78a96e7891 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 9 May 2017 15:55:43 +0200 Subject: [PATCH 3/3] TASK: Fix broken test Relates: #77 --- .../Typo3Update/Sniffs/Removed/GenericGlobalSniff/Expected.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/Expected.json b/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/Expected.json index 4e2c2b9..c94aa82 100644 --- a/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/Expected.json +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Removed/GenericGlobalSniff/Expected.json @@ -13,7 +13,7 @@ "type": "WARNING" }, { - "column": 13, + "column": 26, "fixable": false, "line": 25, "message": "Calls to removed code are not allowed; found typo3CacheManager. Removed in 7.0. Use GeneralUtility::makeInstance() instead. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-62416-DeprecatedCodeRemovalInCoreSysext.html#removed-php-classes",