WIP|FEATURE: Check for removed php classes.

* Add feature to existing code base and logic, see #72 .
* Add first removed classes for TYPO3 7.0 as example.
* Adjust first test to make sure basic implementation works.

Relates: #41
This commit is contained in:
Daniel Siepmann 2017-04-25 09:22:50 +02:00
parent 01af1eaf4b
commit f972533e04
8 changed files with 404 additions and 5 deletions

View file

@ -0,0 +1,14 @@
Typo3Update\Feature\RemovedClassFeature:
- Typo3Update_Sniffs_Classname_InheritanceSniff
- Typo3Update_Sniffs_Classname_InlineCommentSniff
- Typo3Update_Sniffs_Classname_InstanceofSniff
- Typo3Update_Sniffs_Classname_InstantiationWithMakeInstanceSniff
- Typo3Update_Sniffs_Classname_InstantiationWithNewSniff
- Typo3Update_Sniffs_Classname_InstantiationWithObjectManagerSniff
- Typo3Update_Sniffs_Classname_IsACallSniff
- Typo3Update_Sniffs_Classname_MissingVendorForPluginsAndModulesSniff
- Typo3Update_Sniffs_Classname_PhpDocCommentSniff
- Typo3Update_Sniffs_Classname_StaticCallSniff
- Typo3Update_Sniffs_Classname_TypeHintCatchExceptionSniff
- Typo3Update_Sniffs_Classname_TypeHintSniff
- Typo3Update_Sniffs_Classname_UseSniff

View file

@ -0,0 +1,11 @@
# Breaking changes in 7.0: https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Index.html#breaking-changes
'7.0':
\TYPO3\CMS\Backend\Template\MediumDocumentTemplate:
replacement: 'Use \TYPO3\CMS\Backend\Template\DocumentTemplate instead'
docsUrl: 'https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-61782-DeprecatedDocumentTemplateClassesRemoved.html'
\TYPO3\CMS\Backend\Template\SmallDocumentTemplate:
replacement: 'Use \TYPO3\CMS\Backend\Template\DocumentTemplate instead'
docsUrl: 'https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-61782-DeprecatedDocumentTemplateClassesRemoved.html'
\TYPO3\CMS\Backend\Template\StandardDocumentTemplate:
replacement: 'Use \TYPO3\CMS\Backend\Template\DocumentTemplate instead'
docsUrl: 'https://docs.typo3.org/typo3cms/extensions/core/7.6/Changelog/7.0/Breaking-61782-DeprecatedDocumentTemplateClassesRemoved.html'

View file

@ -0,0 +1,294 @@
<?php
namespace Typo3Update\Sniffs\Removed;
/*
* 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_File as PhpCsFile;
use PHP_CodeSniffer_Sniff as PhpCsSniff;
use PHP_CodeSniffer_Tokens as Tokens;
use Symfony\Component\Yaml\Yaml;
use Typo3Update\Options;
/**
* 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
{
use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait;
/**
* Configuration to define removed code.
*
* @var array
*/
protected $configured = [];
/**
* Constant for the current sniff instance.
* @var array
*/
protected $removed = [];
public function __construct()
{
if ($this->configured === []) {
foreach ($this->getRemovedConfigFiles() as $file) {
$this->configured = array_merge(
$this->configured,
$this->prepareStructure(Yaml::parse(file_get_contents((string) $file)))
);
}
}
}
/**
* Return file names containing removed configurations.
*
* @return array<string>
*/
abstract protected function getRemovedConfigFiles();
/**
* Prepares structure from config for later usage.
*
* @param array $typo3Versions
* @return array
*/
protected function prepareStructure(array $typo3Versions)
{
$newStructure = [];
foreach ($typo3Versions as $typo3Version => $removals) {
foreach ($removals as $removed => $config) {
// Split static methods and methods.
$split = preg_split('/::|->/', $removed);
$newStructure[$removed] = $config;
$newStructure[$removed]['static'] = strpos($removed, '::') !== false;
$newStructure[$removed]['fqcn'] = null;
$newStructure[$removed]['class'] = null;
$newStructure[$removed]['name'] = $split[0];
$newStructure[$removed]['version_removed'] = $typo3Version;
// If split contains two parts, it's a class
if (isset($split[1])) {
$newStructure[$removed]['fqcn'] = $split[0];
$newStructure[$removed]['class'] = array_slice(
explode('\\', $newStructure[$removed]['fqcn']),
-1
)[0];
$newStructure[$removed]['name'] = $split[1];
}
};
}
return $newStructure;
}
/**
* Processes the tokens that this sniff is interested in.
*
* This is the default implementation, as most of the time next T_STRING is
* the class name. This way only the register method has to be registered
* in default cases.
*
* @param PhpCsFile $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(PhpCsFile $phpcsFile, $stackPtr)
{
if (!$this->isRemoved($phpcsFile, $stackPtr)) {
return;
}
$this->addMessage($phpcsFile, $stackPtr);
}
/**
* Check whether the current token is removed.
*
* @param PhpCsFile $phpcsFile
* @param int $stackPtr
* @return bool
*/
protected function isRemoved(PhpCsFile $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$staticPosition = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true, null, true);
$name = $tokens[$stackPtr]['content'];
$isStatic = false;
$class = false;
if ($staticPosition !== false) {
$isStatic = $tokens[$staticPosition]['code'] === T_DOUBLE_COLON;
}
if ($isStatic) {
$class = $phpcsFile->findPrevious(T_STRING, $staticPosition, null, false, null, true);
if ($class !== false) {
$class = $tokens[$class]['content'];
}
}
$this->removed = $this->getMatchingRemoved($name, $class, $isStatic);
return $this->removed !== [];
}
/**
* Returns all matching removed functions for given arguments.
*
* @param string $name
* @param string $className The last part of the class name, splitted by namespaces.
* @param bool $isStatic
*
* @return array
*/
protected function getMatchingRemoved($name, $className, $isStatic)
{
// We will not match any static calls, without the class name, at least for now.
if ($isStatic === true && $className === false) {
return [];
}
return array_filter(
$this->configured,
function ($config) use ($name, $isStatic, $className) {
return $name === $config['name']
&& $isStatic === $config['static']
&& (
$className === $config['class']
|| $className === false
)
;
}
);
}
/**
* Add message for the given token position.
*
* Default is a warning, non fixable. Just overwrite in concrete sniff, if
* something different suites better.
*
* @param PhpCsFile $phpcsFile
* @param int $tokenPosition
*
* @return void
*/
protected function addMessage(PhpCsFile $phpcsFile, $tokenPosition)
{
foreach ($this->removed as $constant) {
$phpcsFile->addWarning(
'Legacy calls are not allowed; found %s. Removed in %s. %s. See: %s',
$tokenPosition,
$this->getIdentifier($constant),
[
$this->getOldUsage($constant),
$this->getRemovedVersion($constant),
$this->getReplacement($constant),
$this->getDocsUrl($constant),
]
);
}
}
/**
* Identifier for configuring this specific error / warning through PHPCS.
*
* @param array $config
*
* @return string
*/
protected function getIdentifier(array $config)
{
$name = $config['name'];
if ($config['class']) {
$name = $config['class'] . '.' . $name;
}
return $name;
}
/**
* The original call, to allow user to check matches.
*
* As we match the name, that can be provided by multiple classes, you
* should provide an example, so users can check that this is the legacy
* one.
*
* @param array $config
*
* @return string
*/
abstract protected function getOldUsage(array $config);
/**
* Returns TYPO3 version when the breaking change happened.
*
* To let user decide whether this is important for him.
*
* @param array $config
*
* @return string
*/
protected function getRemovedVersion(array $config)
{
return $config['version_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';
}
/**
* Allow user to lookup the official docs related to this deprecation / breaking change.
*
* @param array $config The converted structure for a single constant.
*
* @return string
*/
protected function getDocsUrl(array $config)
{
return $config['docsUrl'];
}
}

View file

@ -0,0 +1,56 @@
<?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;
/**
* 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)
{
if ($this->isClassnameRemoved($classname) === false) {
return;
}
$phpcsFile->addError(
'Removed classes are not allowed; found "%s", use "%s" instead',
$classnamePosition,
'removedClassname',
[$classname]
);
}
}

View file

@ -95,6 +95,19 @@ class Options
);
}
/**
* Returns an array of absolute file names containing removed class configurations.
*
* @return array<string>
*/
public static function getRemovedClassConfigFiles()
{
return static::getOptionFileNames(
'removedClassConfigFiles',
__DIR__ . '/../Configuration/Removed/Classes/*.yaml'
);
}
/**
* Get the option by optionName, if not defined, use default.
*

View file

@ -1,6 +1,6 @@
--- tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/PhpDocCommentSniff/InputFileForIssues.php
+++ PHP_CodeSniffer
@@ -22,19 +22,19 @@
@@ -22,20 +22,20 @@
class InputFileForIssues
{
/**
@ -13,6 +13,7 @@
/**
- * @param t3lib_div
+ * @param \TYPO3\CMS\Core\Utility\GeneralUtility
* @param \TYPO3\CMS\Backend\Template\MediumDocumentTemplate
*
- * @return Tx_Extbase_Configuration_Configurationmanager
+ * @return \TYPO3\CMS\Extbase\Configuration\ConfigurationManager

View file

@ -21,10 +21,19 @@
"source": "Typo3Update.Classname.PhpDocComment.legacyClassname",
"type": "ERROR"
},
{
"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",
"severity": 5,
"source": "Typo3Update.LegacyClassnames.DocComment.TYPO3_CMS_Backend_Template_MediumDocumentTemplate",
"type": "WARNING"
},
{
"column": 16,
"fixable": true,
"line": 33,
"line": 34,
"message": "Legacy classes are not allowed; found \"Tx_Extbase_Configuration_Configurationmanager\", use \"TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager\" instead",
"severity": 5,
"source": "Typo3Update.Classname.PhpDocComment.legacyClassname",
@ -33,19 +42,19 @@
{
"column": 18,
"fixable": true,
"line": 37,
"line": 38,
"message": "Legacy classes are not allowed; found \"t3lib_div\", use \"TYPO3\\CMS\\Core\\Utility\\GeneralUtility\" instead",
"severity": 5,
"source": "Typo3Update.Classname.PhpDocComment.legacyClassname",
"type": "ERROR"
}
],
"warnings": 0
"warnings": 1
}
},
"totals": {
"errors": 4,
"fixable": 4,
"warnings": 0
"warnings": 1
}
}

View file

@ -29,6 +29,7 @@ class InputFileForIssues
/**
* @param t3lib_div
* @param \TYPO3\CMS\Backend\Template\MediumDocumentTemplate
*
* @return Tx_Extbase_Configuration_Configurationmanager
*/