TASK: Migrate RemovedClassFeature

* Also migrate RemovedClassFeature to new structure with
  AbstractYamlRemovedUsage

Relates: #71
This commit is contained in:
Daniel Siepmann 2017-04-27 14:05:05 +02:00
parent bcbd1f96bc
commit b9bdd5dd3b
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
5 changed files with 140 additions and 26 deletions

View file

@ -0,0 +1,103 @@
<?php
namespace Typo3Update\Feature;
/*
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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<string>
*/
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';
}
}

View file

@ -21,12 +21,18 @@ namespace Typo3Update\Feature;
*/ */
use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer_Sniff as PhpCsSniff;
/** /**
* See "Features" in documentation. * See "Features" in documentation.
*/ */
interface FeatureInterface interface FeatureInterface
{ {
/**
* @var PhpCsSniff $sniff
*/
public function __construct(PhpCsSniff $sniff);
/** /**
* Process like a PHPCS Sniff. * Process like a PHPCS Sniff.
* *

View file

@ -20,37 +20,42 @@ namespace Typo3Update\Feature;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
use PHP_CodeSniffer as PhpCs;
use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer_Sniff as PhpCsSniff; use Typo3Update\Options;
/** class RemovedClassFeature extends AbstractYamlRemovedUsage
* This feature will add fixable errors for old legacy classnames.
*
* Can be attached to sniffs returning classnames.
*/
class RemovedClassFeature implements FeatureInterface
{ {
/**
* Process like a PHPCS Sniff.
*
* @param PhpCsFile $phpcsFile
* @param int $classnamePosition
* @param string $classname
*
* @return void
*/
public function process(PhpCsFile $phpcsFile, $classnamePosition, $classname) public function process(PhpCsFile $phpcsFile, $classnamePosition, $classname)
{ {
if ($this->isClassnameRemoved($classname) === false) { if (! $this->configured->isRemoved($classname)) {
return; return;
} }
$this->addWarning(
$phpcsFile->addError( $phpcsFile,
'Removed classes are not allowed; found "%s", use "%s" instead',
$classnamePosition, $classnamePosition,
'removedClassname', $this->configured->getRemoved($classname)
[$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();
}
} }

View file

@ -104,7 +104,7 @@ class Options
{ {
return static::getOptionFileNames( return static::getOptionFileNames(
'removedClassConfigFiles', 'removedClassConfigFiles',
__DIR__ . '/../Configuration/Removed/Classes/*.yaml' __DIR__ . '/Configuration/Removed/Classes/*.yaml'
); );
} }

View file

@ -25,9 +25,9 @@
"column": 15, "column": 15,
"fixable": false, "fixable": false,
"line": 32, "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, "severity": 5,
"source": "Typo3Update.LegacyClassnames.DocComment.TYPO3_CMS_Backend_Template_MediumDocumentTemplate", "source": "Typo3Update.Classname.PhpDocComment.RemovedClass.TYPO3_CMS_Backend_Template_MediumDocumentTemplate",
"type": "WARNING" "type": "WARNING"
}, },
{ {