diff --git a/src/Standards/Typo3Update/AbstractYamlRemovedUsage.php b/src/Standards/Typo3Update/AbstractYamlRemovedUsage.php new file mode 100644 index 0000000..84aee44 --- /dev/null +++ b/src/Standards/Typo3Update/AbstractYamlRemovedUsage.php @@ -0,0 +1,72 @@ + + * + * 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\RemovedByYamlConfiguration; + +abstract class AbstractYamlRemovedUsage +{ + protected $configured; + + public function __construct() + { + $this->configured = new RemovedByYamlConfiguration( + $this->getRemovedConfigFiles(), + $this->getPrepareStructureCallback() + ); + } + + protected function getPrepareStructureCallback() + { + return function (array $typo3Versions) { + return call_user_func_array([$this, 'prepareStructure'], [$typo3Versions]); + }; + } + + abstract protected function prepareStructure(array $typo3Versions); + + 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'], + ] + ); + } + + 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/AbstractYamlRemovedUsage.php b/src/Standards/Typo3Update/Feature/AbstractYamlRemovedUsage.php index d5463d6..2a1fb9f 100644 --- a/src/Standards/Typo3Update/Feature/AbstractYamlRemovedUsage.php +++ b/src/Standards/Typo3Update/Feature/AbstractYamlRemovedUsage.php @@ -20,91 +20,13 @@ 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\RemovedByYamlConfiguration; +use Typo3Update\AbstractYamlRemovedUsage as BaseAbstractYamlRemovedUsage; -/** - * 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 +abstract class AbstractYamlRemovedUsage extends BaseAbstractYamlRemovedUsage implements FeatureInterface { - /** - * @var array - */ - protected $configured; - - /** - * @var PhpCsSniff - */ - protected $sniff; - public function __construct(PhpCsSniff $sniff) { - $this->sniff = $sniff; - $this->configured = new RemovedByYamlConfiguration( - $this->getRemovedConfigFiles(), - $this->getPrepateStructure() - ); - } - - protected function getPrepateStructure() - { - return function (array $typo3Versions) { - return call_user_func_array([$this, 'prepareStructure'], [$typo3Versions]); - }; - } - - /** - * 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'; + parent::__construct(); } } diff --git a/src/Standards/Typo3Update/Sniffs/Removed/AbstractGenericUsage.php b/src/Standards/Typo3Update/Sniffs/Removed/AbstractGenericUsage.php index ba2ba40..b7ab259 100644 --- a/src/Standards/Typo3Update/Sniffs/Removed/AbstractGenericUsage.php +++ b/src/Standards/Typo3Update/Sniffs/Removed/AbstractGenericUsage.php @@ -22,83 +22,16 @@ namespace Typo3Update\Sniffs\Removed; use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_Sniff as PhpCsSniff; -use Typo3Update\RemovedByYamlConfiguration; +use Typo3Update\AbstractYamlRemovedUsage as BaseAbstractYamlRemovedUsage; -/** - * 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 AbstractGenericUsage implements PhpCsSniff +abstract class AbstractGenericUsage extends BaseAbstractYamlRemovedUsage implements PhpCsSniff { - protected $configured; - - public function __construct() - { - $this->configured = new RemovedByYamlConfiguration( - $this->getRemovedConfigFiles(), - $this->getPrepateStructure() - ); - } - - protected function getPrepateStructure() - { - return function (array $typo3Versions) { - return call_user_func_array([$this, 'prepareStructure'], [$typo3Versions]); - }; - } - - /** - * 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(); - abstract protected function findRemoved(PhpCsFile $phpcsFile, $stackPtr); public function process(PhpCsFile $phpcsFile, $stackPtr) { foreach ($this->findRemoved($phpcsFile, $stackPtr) as $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'], - ] - ); + $this->addWarning($phpcsFile, $stackPtr, $removed); } } - - /** - * 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'; - } }