From b9bdd5dd3b89b4661994f4123622db14c88e2fb0 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 27 Apr 2017 14:05:05 +0200 Subject: [PATCH] TASK: Migrate RemovedClassFeature * Also migrate RemovedClassFeature to new structure with AbstractYamlRemovedUsage Relates: #71 --- .../Feature/AbstractYamlRemovedUsage.php | 103 ++++++++++++++++++ .../Typo3Update/Feature/FeatureInterface.php | 6 + .../Feature/RemovedClassFeature.php | 51 +++++---- src/Standards/Typo3Update/Options.php | 2 +- .../PhpDocCommentSniff/Expected.json | 4 +- 5 files changed, 140 insertions(+), 26 deletions(-) create mode 100644 src/Standards/Typo3Update/Feature/AbstractYamlRemovedUsage.php diff --git a/src/Standards/Typo3Update/Feature/AbstractYamlRemovedUsage.php b/src/Standards/Typo3Update/Feature/AbstractYamlRemovedUsage.php new file mode 100644 index 0000000..9395d44 --- /dev/null +++ b/src/Standards/Typo3Update/Feature/AbstractYamlRemovedUsage.php @@ -0,0 +1,103 @@ + + * + * 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 as PhpCs; +use PHP_CodeSniffer_File as PhpCsFile; +use PHP_CodeSniffer_Sniff as PhpCsSniff; +use Typo3Update\RemovedByYamlConfiguration; + +/** + * Contains common functionality for removed code like constants or functions. + * + * Removed parts are configured using YAML-Files, for examples see + * src/Standards/Typo3Update/Configuration/Removed/Constants/7.0.yaml Also + * check out the configuration options in Readme.rst. + */ +abstract class AbstractYamlRemovedUsage implements FeatureInterface +{ + /** + * @var array + */ + protected $configured; + + /** + * @var PhpCsSniff + */ + protected $sniff; + + public function __construct(PhpCsSniff $sniff) + { + $this->sniff = $sniff; + $this->configured = new RemovedByYamlConfiguration( + $this->getRemovedConfigFiles(), + \Closure::fromCallable([$this, 'prepareStructure']) + ); + } + + /** + * Prepares structure from config for later usage. + * + * @param array $typo3Versions + * @return array + */ + abstract protected function prepareStructure(array $typo3Versions); + + /** + * Return file names containing removed configurations. + * + * @return array + */ + abstract protected function getRemovedConfigFiles(); + + protected function addWarning(PhpCsFile $phpcsFile, $stackPtr, array $removed) + { + $phpcsFile->addWarning( + 'Calls to removed code are not allowed; found %s. Removed in %s. %s. See: %s', + $stackPtr, + $removed['identifier'], + [ + $removed['oldUsage'], + $removed['versionRemoved'], + $this->getReplacement($removed), + $removed['docsUrl'], + ] + ); + } + + /** + * The new call, or information how to migrate. + * + * To provide feedback for user to ease migration. + * + * @param array $config + * + * @return string + */ + protected function getReplacement(array $config) + { + $newCall = $config['replacement']; + if ($newCall !== null) { + return $newCall; + } + return 'There is no replacement, just remove call'; + } +} diff --git a/src/Standards/Typo3Update/Feature/FeatureInterface.php b/src/Standards/Typo3Update/Feature/FeatureInterface.php index 39de10c..ac8b7a9 100644 --- a/src/Standards/Typo3Update/Feature/FeatureInterface.php +++ b/src/Standards/Typo3Update/Feature/FeatureInterface.php @@ -21,12 +21,18 @@ namespace Typo3Update\Feature; */ use PHP_CodeSniffer_File as PhpCsFile; +use PHP_CodeSniffer_Sniff as PhpCsSniff; /** * See "Features" in documentation. */ interface FeatureInterface { + /** + * @var PhpCsSniff $sniff + */ + public function __construct(PhpCsSniff $sniff); + /** * Process like a PHPCS Sniff. * diff --git a/src/Standards/Typo3Update/Feature/RemovedClassFeature.php b/src/Standards/Typo3Update/Feature/RemovedClassFeature.php index d81c34d..ffc61df 100644 --- a/src/Standards/Typo3Update/Feature/RemovedClassFeature.php +++ b/src/Standards/Typo3Update/Feature/RemovedClassFeature.php @@ -20,37 +20,42 @@ namespace Typo3Update\Feature; * 02110-1301, USA. */ -use PHP_CodeSniffer as PhpCs; use PHP_CodeSniffer_File as PhpCsFile; -use PHP_CodeSniffer_Sniff as PhpCsSniff; +use Typo3Update\Options; -/** - * This feature will add fixable errors for old legacy classnames. - * - * Can be attached to sniffs returning classnames. - */ -class RemovedClassFeature implements FeatureInterface +class RemovedClassFeature extends AbstractYamlRemovedUsage { - /** - * Process like a PHPCS Sniff. - * - * @param PhpCsFile $phpcsFile - * @param int $classnamePosition - * @param string $classname - * - * @return void - */ public function process(PhpCsFile $phpcsFile, $classnamePosition, $classname) { - if ($this->isClassnameRemoved($classname) === false) { + if (! $this->configured->isRemoved($classname)) { return; } - - $phpcsFile->addError( - 'Removed classes are not allowed; found "%s", use "%s" instead', + $this->addWarning( + $phpcsFile, $classnamePosition, - 'removedClassname', - [$classname] + $this->configured->getRemoved($classname) ); } + + protected function prepareStructure(array $typo3Versions) + { + $newStructure = []; + foreach ($typo3Versions as $typo3Version => $removals) { + foreach ($removals as $removed => $config) { + $config['name'] = $removed; + $config['identifier'] = 'RemovedClass.' . str_replace('\\', '_', ltrim($removed, '\\')); + $config['versionRemoved'] = $typo3Version; + $config['oldUsage'] = $removed; + + $newStructure[$removed] = $config; + } + } + + return $newStructure; + } + + protected function getRemovedConfigFiles() + { + return Options::getRemovedClassConfigFiles(); + } } diff --git a/src/Standards/Typo3Update/Options.php b/src/Standards/Typo3Update/Options.php index 6db1bd2..62b8a78 100644 --- a/src/Standards/Typo3Update/Options.php +++ b/src/Standards/Typo3Update/Options.php @@ -104,7 +104,7 @@ class Options { return static::getOptionFileNames( 'removedClassConfigFiles', - __DIR__ . '/../Configuration/Removed/Classes/*.yaml' + __DIR__ . '/Configuration/Removed/Classes/*.yaml' ); } diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/PhpDocCommentSniff/Expected.json b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/PhpDocCommentSniff/Expected.json index c528d04..89158c7 100644 --- a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/PhpDocCommentSniff/Expected.json +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/PhpDocCommentSniff/Expected.json @@ -25,9 +25,9 @@ "column": 15, "fixable": false, "line": 32, - "message": "Legacy calls are not allowed; found \\TYPO3\\CMS\\Backend\\Template\\MediumDocumentTemplate. Removed in 7.0. Use \\TYPO3\\CMS\\Backend\\Template\\DocumentTemplate instead. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-61782-DeprecatedDocumentTemplateClassesRemoved.html", + "message": "Calls to removed code are not allowed; found \\TYPO3\\CMS\\Backend\\Template\\MediumDocumentTemplate. Removed in 7.0. Use \\TYPO3\\CMS\\Backend\\Template\\DocumentTemplate instead. See: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-61782-DeprecatedDocumentTemplateClassesRemoved.html", "severity": 5, - "source": "Typo3Update.LegacyClassnames.DocComment.TYPO3_CMS_Backend_Template_MediumDocumentTemplate", + "source": "Typo3Update.Classname.PhpDocComment.RemovedClass.TYPO3_CMS_Backend_Template_MediumDocumentTemplate", "type": "WARNING" }, {