Legoktm has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/371986 )
Change subject: Add sniff to ensure floats have a leading `0` if necessary ...................................................................... Add sniff to ensure floats have a leading `0` if necessary Bug: T170442 Change-Id: I0f8150dbe7155ba99a27a640b44dc347003ce72a --- A MediaWiki/Sniffs/AlternativeSyntax/LeadingZeroInFloatSniff.php A MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php A MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php.expect A MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php.fixed 4 files changed, 86 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer refs/changes/86/371986/1 diff --git a/MediaWiki/Sniffs/AlternativeSyntax/LeadingZeroInFloatSniff.php b/MediaWiki/Sniffs/AlternativeSyntax/LeadingZeroInFloatSniff.php new file mode 100644 index 0000000..d2ebf28 --- /dev/null +++ b/MediaWiki/Sniffs/AlternativeSyntax/LeadingZeroInFloatSniff.php @@ -0,0 +1,67 @@ +<?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 LeadingZeroInFloatSniff implements Sniff { + + /** + * T_DNUMBER is any floating point number + * + * @return array + */ + public function register() { + return [ + T_DNUMBER, + ]; + } + + /** + * If the float starts with a period, it needs + * a zero in front + * + * @param File $phpcsFile File + * @param int $stackPtr Location + */ + public function process( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + // var_dump($tokens[$stackPtr]); + $content = $tokens[$stackPtr]['content']; + if ( $content[0] == '.' ) { + // Starts with a ., needs a leading 0. + $fix = $phpcsFile->addFixableWarning( + 'Floats should have a leading 0', + $stackPtr, + 'Found' + ); + if ( $fix ) { + $phpcsFile->fixer->replaceToken( + $stackPtr, + // Put a zero in front of the existing content + '0' . $content + ); + } + } + } + +} diff --git a/MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php b/MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php new file mode 100644 index 0000000..5606628 --- /dev/null +++ b/MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php @@ -0,0 +1,7 @@ +<?php +$a = 0.75; +$b = .75; +$c = 1.75; +$d = -.75; +$e = -1.75; +$f = -0.75; diff --git a/MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php.expect b/MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php.expect new file mode 100644 index 0000000..801916e --- /dev/null +++ b/MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php.expect @@ -0,0 +1,5 @@ + 3 | WARNING | [x] Floats should have a leading 0 + | | (MediaWiki.AlternativeSyntax.LeadingZeroInFloat.Found) + 5 | WARNING | [x] Floats should have a leading 0 + | | (MediaWiki.AlternativeSyntax.LeadingZeroInFloat.Found) +PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY diff --git a/MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php.fixed b/MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php.fixed new file mode 100644 index 0000000..ce89322 --- /dev/null +++ b/MediaWiki/Tests/files/AlternativeSyntax/leading_zero_in_float.php.fixed @@ -0,0 +1,7 @@ +<?php +$a = 0.75; +$b = 0.75; +$c = 1.75; +$d = -0.75; +$e = -1.75; +$f = -0.75; -- To view, visit https://gerrit.wikimedia.org/r/371986 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0f8150dbe7155ba99a27a640b44dc347003ce72a Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/tools/codesniffer Gerrit-Branch: master Gerrit-Owner: Legoktm <lego...@member.fsf.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits