Legoktm has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/362171 )
Change subject: Add sniff to verify type-casts use the short form (bool, int) ...................................................................... Add sniff to verify type-casts use the short form (bool, int) Bug: T145162 Change-Id: Ieabdfb31d490e946971332ae0dd060b842e94aa3 --- A MediaWiki/Sniffs/AlternativeSyntax/ShortCastSyntaxSniff.php A MediaWiki/Tests/files/AlternativeSyntax/short_cast.php A MediaWiki/Tests/files/AlternativeSyntax/short_cast.php.expect A MediaWiki/Tests/files/AlternativeSyntax/short_cast.php.fixed 4 files changed, 84 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer refs/changes/71/362171/1 diff --git a/MediaWiki/Sniffs/AlternativeSyntax/ShortCastSyntaxSniff.php b/MediaWiki/Sniffs/AlternativeSyntax/ShortCastSyntaxSniff.php new file mode 100644 index 0000000..222defc --- /dev/null +++ b/MediaWiki/Sniffs/AlternativeSyntax/ShortCastSyntaxSniff.php @@ -0,0 +1,65 @@ +<?php +/** + * 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. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +namespace MediaWiki\Sniffs\AlternativeSyntax; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +class ShortCastSyntaxSniff implements Sniff { + /** + * @return array + */ + public function register() { + return [ + T_BOOL_CAST, + T_INT_CAST, + ]; + } + + /** + * @param File $phpcsFile File object + * @param int $stackPtr Position of error + */ + public function process( File $phpcsFile, $stackPtr ) { + $token = $phpcsFile->getTokens()[$stackPtr]; + + if ( $token['code'] === T_BOOL_CAST && $token['content'] !== '(bool)' ) { + $fix = $phpcsFile->addFixableWarning( + 'Type-cast should use short "bool" form', + $stackPtr, + 'NotShortBoolForm' + ); + if ( $fix === true ) { + $phpcsFile->fixer->replaceToken( $stackPtr, '(bool)' ); + } + } elseif ( $token['code'] === T_INT_CAST && $token['content'] !== '(int)' ) { + $fix = $phpcsFile->addFixableWarning( + 'Type-cast should use short "int" form', + $stackPtr, + 'NotShortIntForm' + ); + if ( $fix === true ) { + $phpcsFile->fixer->replaceToken( $stackPtr, '(int)' ); + } + } + } + +} diff --git a/MediaWiki/Tests/files/AlternativeSyntax/short_cast.php b/MediaWiki/Tests/files/AlternativeSyntax/short_cast.php new file mode 100644 index 0000000..42c7308 --- /dev/null +++ b/MediaWiki/Tests/files/AlternativeSyntax/short_cast.php @@ -0,0 +1,7 @@ +<?php + +(bool)$a; +(boolean)$a; + +(int)$a; +(integer)$a; diff --git a/MediaWiki/Tests/files/AlternativeSyntax/short_cast.php.expect b/MediaWiki/Tests/files/AlternativeSyntax/short_cast.php.expect new file mode 100644 index 0000000..e9d916b --- /dev/null +++ b/MediaWiki/Tests/files/AlternativeSyntax/short_cast.php.expect @@ -0,0 +1,5 @@ + 4 | WARNING | [x] Type-cast should use short "bool" form + | | (MediaWiki.AlternativeSyntax.ShortCastSyntax.NotShortBoolForm) + 7 | WARNING | [x] Type-cast should use short "int" form + | | (MediaWiki.AlternativeSyntax.ShortCastSyntax.NotShortIntForm) +PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY diff --git a/MediaWiki/Tests/files/AlternativeSyntax/short_cast.php.fixed b/MediaWiki/Tests/files/AlternativeSyntax/short_cast.php.fixed new file mode 100644 index 0000000..e2049c3 --- /dev/null +++ b/MediaWiki/Tests/files/AlternativeSyntax/short_cast.php.fixed @@ -0,0 +1,7 @@ +<?php + +(bool)$a; +(bool)$a; + +(int)$a; +(int)$a; -- To view, visit https://gerrit.wikimedia.org/r/362171 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ieabdfb31d490e946971332ae0dd060b842e94aa3 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/tools/codesniffer Gerrit-Branch: master Gerrit-Owner: Legoktm <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
