From 2118d86879c15d1cadbffefe809beb2d4f671db6 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 28 Mar 2017 10:31:30 +0200 Subject: [PATCH] WIP|FEATURE: First working check for deprecated functions * Provide a generic sniff with configuration (property) containing all deprecated calls. * Check function calls and provide warning with information. Relates: #33 --- .../Deprecated/AbstractFunctionCall.php | 95 +++++++++++++++++++ .../Deprecated/GenericFunctionCallSniff.php | 60 ++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 src/Standards/Typo3Update/Sniffs/Deprecated/AbstractFunctionCall.php create mode 100644 src/Standards/Typo3Update/Sniffs/Deprecated/GenericFunctionCallSniff.php diff --git a/src/Standards/Typo3Update/Sniffs/Deprecated/AbstractFunctionCall.php b/src/Standards/Typo3Update/Sniffs/Deprecated/AbstractFunctionCall.php new file mode 100644 index 0000000..30fb56a --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/Deprecated/AbstractFunctionCall.php @@ -0,0 +1,95 @@ + + * + * 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; + +/** + * + */ +abstract class AbstractFunctionCall implements PhpCsSniff +{ + use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; + + abstract protected function getFunctionNames(); + abstract protected function getOldFunctionCall($functionName); + abstract protected function getNewFunctionCall($functionName); + abstract protected function getDocsUrl($functionName); + + /** + * Returns the token types that this sniff is interested in. + * + * @return array + */ + public function register() + { + return Tokens::$functionNameTokens; + } + + /** + * 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->isFunctionCall($phpcsFile, $stackPtr)) { + return; + } + + $tokens = $phpcsFile->getTokens(); + $token = $tokens[$stackPtr]; + + if (in_array($token['content'], $this->getFunctionNames()) === false) { + return; + } + + $this->addWarning($phpcsFile, $stackPtr); + } + + protected function addWarning(PhpCsFile $phpcsFile, $tokenPosition) + { + $tokens = $phpcsFile->getTokens(); + $token = $tokens[$tokenPosition]; + $functionCall = $token['content']; + + $phpcsFile->addWarning( + 'Legacy function calls are not allowed; found %s. %s. See: %s', + $tokenPosition, + 'legacyFunctioncall', + [ + $this->getOldFunctionCall($functionCall), + $this->getNewFunctionCall($functionCall), + $this->getDocsUrl($functionCall), + ] + ); + } +} diff --git a/src/Standards/Typo3Update/Sniffs/Deprecated/GenericFunctionCallSniff.php b/src/Standards/Typo3Update/Sniffs/Deprecated/GenericFunctionCallSniff.php new file mode 100644 index 0000000..e801ea4 --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/Deprecated/GenericFunctionCallSniff.php @@ -0,0 +1,60 @@ + + * + * 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\Sniffs\Deprecated\AbstractFunctionCall; + +/** + */ +class Typo3Update_Sniffs_Deprecated_GenericFunctionCallSniff extends AbstractFunctionCall +{ + protected $deprecatedFunctions = [ + 'loadTCA' => [ + 'oldFunctionCall' => '\TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA()', + 'newFunctionCall' => null, + 'docsUrl' => 'https://docs.typo3.org/typo3cms/extensions/core/Changelog/7.0/Breaking-61785-LoadTcaFunctionRemoved.html', + ], + ]; + + protected function getFunctionNames() + { + return array_keys($this->deprecatedFunctions); + } + + protected function getOldFunctionCall($functionName) + { + return $this->deprecatedFunctions[$functionName]['oldFunctionCall']; + } + + protected function getNewFunctionCall($functionName) + { + $newCall = $this->deprecatedFunctions[$functionName]['newFunctionCall']; + if ($newCall !== null) { + return $newCall; + } + return 'There is no replacement, just remove call'; + } + + protected function getDocsUrl($functionName) + { + return $this->deprecatedFunctions[$functionName]['docsUrl']; + } +}