From 20722f26d19505d2d4e00b7ddfb136d7f7d09d16 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 14 Mar 2017 11:37:06 +0100 Subject: [PATCH] TASK: Refactor code base * As method does not belong to class renaming, but is a general method, it was moved to a new trait. Relates: #4 --- .../Sniffs/ExtendedPhpCsSupportTrait.php | 69 +++++++++++++++++++ .../ClassnameCheckerTrait.php | 41 +---------- .../InstantiationWithMakeInstanceSniff.php | 1 + 3 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php diff --git a/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php b/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php new file mode 100644 index 0000000..2368d4d --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php @@ -0,0 +1,69 @@ + + * + * 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_Tokens as Tokens; + +/** + * Provide common uses for all sniffs. + */ +trait ExtendedPhpCsSupportTrait +{ + /** + * Check whether current stackPtr is a function call. + * + * Code taken from PEAR_Sniffs_Functions_FunctionCallSignatureSniff for reuse. + * + * @param PhpCsFile $phpcsFile + * @param int $stackPtr + * + * @return bool + */ + protected function isFunctionCall(PhpCsFile $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + // Find the next non-empty token. + $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); + + if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { + // Not a function call. + return false; + } + + if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { + // Not a function call. + return false; + } + + // Find the previous non-empty token. + $search = Tokens::$emptyTokens; + $search[] = T_BITWISE_AND; + $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true); + if ($tokens[$previous]['code'] === T_FUNCTION) { + // It's a function definition, not a function call. + return false; + } + + return true; + } +} diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php index e709022..5537b0f 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php @@ -24,7 +24,7 @@ use PHP_CodeSniffer_File as PhpcsFile; use PHP_CodeSniffer_Tokens as Tokens; /** - * Provide common uses for all sniffs. + * Provide common uses for all sniffs, regarding class name checks. */ trait ClassnameCheckerTrait { @@ -109,43 +109,4 @@ trait ClassnameCheckerTrait { return $classname; } - - /** - * Check whether current stackPtr is a function call. - * - * Code taken from PEAR_Sniffs_Functions_FunctionCallSignatureSniff for reuse. - * - * @param PhpCsFile $phpcsFile - * @param int $stackPtr - * - * @return bool - */ - protected function isFunctionCall(PhpCsFile $phpcsFile, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - - // Find the next non-empty token. - $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); - - if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { - // Not a function call. - return false; - } - - if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { - // Not a function call. - return false; - } - - // Find the previous non-empty token. - $search = Tokens::$emptyTokens; - $search[] = T_BITWISE_AND; - $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true); - if ($tokens[$previous]['code'] === T_FUNCTION) { - // It's a function definition, not a function call. - return false; - } - - return true; - } } diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php index 0638b50..94bb175 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php @@ -27,6 +27,7 @@ use PHP_CodeSniffer_Tokens as Tokens; class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithMakeInstanceSniff implements PHP_CodeSniffer_Sniff { use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; + use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; /** * Original token content for reuse accross methods.