From b3023f94ff205a1a71cce8425bb39bab43f78ab2 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 14 Mar 2017 08:32:21 +0100 Subject: [PATCH 1/4] FEATURE: Migrate inline comments for IDEs * Check all inline comments matching the necessary pattern. * Check the contained class name. * Migrate class name. Resolves: #3 --- Readme.rst | 2 + .../LegacyClassnames/InlineCommentSniff.php | 85 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php diff --git a/Readme.rst b/Readme.rst index 228c3b6..b17e026 100644 --- a/Readme.rst +++ b/Readme.rst @@ -51,6 +51,8 @@ new ones like ``\TYPO3\Extbase\...``. This is done for: - Typehints in methods and function like injects. +- Inline comments for IDEs, e.g. ``/* @var $configurationManager + Tx_Extbase_Configuration_ConfigurationManager */`` What does it look like? ======================= diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php new file mode 100644 index 0000000..5cca543 --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php @@ -0,0 +1,85 @@ + + * + * 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. + */ + +/** + * Migrate PHP inline comments, e.g. for IDEs. + */ +class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff implements PHP_CodeSniffer_Sniff +{ + use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; + + /** + * Original token content for reuse accross methods. + * @var string + */ + protected $originalTokenContent = ''; + + /** + * Returns the token types that this sniff is interested in. + * + * @return array + */ + public function register() + { + return [ + T_COMMENT, + ]; + } + + /** + * Processes the tokens that this sniff is interested in. + * + * @param PHP_CodeSniffer_File $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(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + if (substr($tokens[$stackPtr]['content'], 0, 9) !== '/* @var $') { + return; + } + + $commentParts = explode(' ', $tokens [$stackPtr ]['content']); + if (count($commentParts) !== 5) { + return; + } + + $this->originalTokenContent = $tokens[$stackPtr]['content']; + $this->addFixableError($phpcsFile, $stackPtr, $commentParts[3]); + } + + /** + * As token contains more then just class name, we have to build new content ourself. + * + * @param string $classname + * @return string + */ + public function getTokenForReplacement($classname) + { + $token = explode(' ', $this->originalTokenContent); + $token[3] = $classname; + + return implode(' ', $token); + } +} From c3358654faa994f3ea39f594750109ab28a37b6a Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 16 Mar 2017 11:33:50 +0100 Subject: [PATCH 2/4] BUGFIX: Fix visibility of method Relates: #3 --- .../Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php index 5cca543..5d51081 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php @@ -75,7 +75,7 @@ class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff implements PHP_Code * @param string $classname * @return string */ - public function getTokenForReplacement($classname) + protected function getTokenForReplacement($classname) { $token = explode(' ', $this->originalTokenContent); $token[3] = $classname; From 6361db2eaf0208944b2f1bdc7bc2794a60d32831 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 16 Mar 2017 12:01:01 +0100 Subject: [PATCH 3/4] TASK: Use regex instead of whitespace char * To be more bulletproof. Relates: #3 --- .../Sniffs/LegacyClassnames/InlineCommentSniff.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php index 5d51081..38f8ae8 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php @@ -56,11 +56,11 @@ class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff implements PHP_Code public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - if (substr($tokens[$stackPtr]['content'], 0, 9) !== '/* @var $') { + if (preg_match('/\/\*\s+@var\s+\$/', $tokens[$stackPtr]['content']) !== 1) { return; } - $commentParts = explode(' ', $tokens [$stackPtr ]['content']); + $commentParts = preg_split('/\s+/', $tokens[$stackPtr]['content']); if (count($commentParts) !== 5) { return; } @@ -77,7 +77,7 @@ class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff implements PHP_Code */ protected function getTokenForReplacement($classname) { - $token = explode(' ', $this->originalTokenContent); + $token = preg_split('/\s+/', $this->originalTokenContent); $token[3] = $classname; return implode(' ', $token); From e2bae45829e6e1bd3201642a04a702b499839a9c Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 16 Mar 2017 13:44:03 +0100 Subject: [PATCH 4/4] FEATURE: Respect class $var and $var class * Respect both orders. * Replace regexes with array checks. Relates: #3 --- .../LegacyClassnames/InlineCommentSniff.php | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php index 38f8ae8..c6e8303 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php @@ -56,17 +56,30 @@ class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff implements PHP_Code public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - if (preg_match('/\/\*\s+@var\s+\$/', $tokens[$stackPtr]['content']) !== 1) { - return; - } - - $commentParts = preg_split('/\s+/', $tokens[$stackPtr]['content']); - if (count($commentParts) !== 5) { - return; - } - $this->originalTokenContent = $tokens[$stackPtr]['content']; - $this->addFixableError($phpcsFile, $stackPtr, $commentParts[3]); + $commentParts = preg_split('/\s+/', $this->originalTokenContent); + + if (count($commentParts) !== 5 || $commentParts[1] !== '@var' || ($commentParts[2][0] !== '$' && $commentParts[3][0] !== '$')) { + return; + } + + $this->addFixableError($phpcsFile, $stackPtr, $commentParts[$this->getClassnamePosition($commentParts)]); + } + + /** + * As Classname can be found as first or second argument of @var, we have + * to check where it is. + * + * @param array $commentParts + * @return int + */ + protected function getClassnamePosition(array $commentParts) + { + if ($commentParts[3][0] === '$') { + return 2; + } + + return 3; } /** @@ -78,7 +91,7 @@ class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff implements PHP_Code protected function getTokenForReplacement($classname) { $token = preg_split('/\s+/', $this->originalTokenContent); - $token[3] = $classname; + $token[$this->getClassnamePosition($token)] = $classname; return implode(' ', $token); }