jenkins-bot has submitted this change and it was merged.
Change subject: Sniff to check assignment in while & if
......................................................................
Sniff to check assignment in while & if
Fail: if ( $a = 0 )
Fail: if ( $a *= foo() )
Fail: if ( $a += foo() )
Fail: while ( $a = foo() )
Pass: if ( $a == 0 )
Pass: if ( $a === 0 )
Pass: if ( $a === array( 1 => 0 ) )
Pass: while ( $a < 0 )
Bug: T92744
Change-Id: I4c8cc00f779d6b7e823f5db562af3d826e09fb6c
---
A MediaWiki/Sniffs/ControlStructures/AssignmentInControlStructuresSniff.php
A
MediaWiki/Tests/files/ControlStructures/assignment_in_control_structures_fail.php
A
MediaWiki/Tests/files/ControlStructures/assignment_in_control_structures_pass.php
3 files changed, 82 insertions(+), 0 deletions(-)
Approvals:
Legoktm: Looks good to me, approved
jenkins-bot: Verified
diff --git
a/MediaWiki/Sniffs/ControlStructures/AssignmentInControlStructuresSniff.php
b/MediaWiki/Sniffs/ControlStructures/AssignmentInControlStructuresSniff.php
new file mode 100644
index 0000000..21786e8
--- /dev/null
+++ b/MediaWiki/Sniffs/ControlStructures/AssignmentInControlStructuresSniff.php
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Sniff to suppress the use of:
+ * Fail: if ( $a = 0 )
+ * Fail: if ( $a *= foo() )
+ * Fail: if ( $a += foo() )
+ * Fail: while ( $a = foo() )
+ * Pass: if ( $a == 0 )
+ * Pass: if ( $a === 0 )
+ * Pass: if ( $a === array( 1 => 0 ) )
+ * Pass: while ( $a < 0 )
+ */
+// @codingStandardsIgnoreStart
+class MediaWiki_Sniffs_ControlStructures_AssignmentInControlStructuresSniff
+ implements PHP_CodeSniffer_Sniff {
+ // @codingStandardsIgnoreEnd
+ public function register() {
+ return array(
+ T_IF,
+ T_WHILE,
+ T_ELSEIF,
+ );
+ }
+ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
+ $tokens = $phpcsFile->getTokens();
+ $token = $tokens[$stackPtr];
+
+ $next = $token['parenthesis_opener'] + 1;
+ $end = $token['parenthesis_closer'];
+ while ( $next < $end ) {
+ $code = $tokens[$next]['code'];
+ // Check if any assignment operator was used. Allow
T_DOUBLE_ARROW as that can
+ // be used in an array like `if ( $foo === array( 'foo'
=> 'bar' ) )`
+ if ( in_array( $code,
PHP_CodeSniffer_Tokens::$assignmentTokens, true )
+ && $code !== T_DOUBLE_ARROW ) {
+ $error = 'Assignment expression not allowed
within "%s".';
+ $phpcsFile->addError(
+ $error,
+ $stackPtr,
+ 'AssignmentInControlStructures',
+ $token['content']
+ );
+ break;
+ }
+ $next++;
+ }
+ }
+}
diff --git
a/MediaWiki/Tests/files/ControlStructures/assignment_in_control_structures_fail.php
b/MediaWiki/Tests/files/ControlStructures/assignment_in_control_structures_fail.php
new file mode 100644
index 0000000..e5a836b
--- /dev/null
+++
b/MediaWiki/Tests/files/ControlStructures/assignment_in_control_structures_fail.php
@@ -0,0 +1,17 @@
+<?php
+
+if ( $foo = 1 ) {
+
+} elseif ( $foo += 1 ) {
+
+} elseif ( $foo *= $bar ) {
+
+} elseif ( $foo > 1 || $bar < 1 || $foobar = 1 ) {
+
+} elseif ( $bar = $foo->foo() ) {
+
+}
+
+while ( $foo = foo() ) {
+
+}
diff --git
a/MediaWiki/Tests/files/ControlStructures/assignment_in_control_structures_pass.php
b/MediaWiki/Tests/files/ControlStructures/assignment_in_control_structures_pass.php
new file mode 100644
index 0000000..b9228c7
--- /dev/null
+++
b/MediaWiki/Tests/files/ControlStructures/assignment_in_control_structures_pass.php
@@ -0,0 +1,17 @@
+<?php
+
+if ( $foo == 1 ) {
+
+} elseif ( $foo === 1 ) {
+
+} elseif ( $foo === array( 1 => 0 ) ) {
+
+} elseif ( $foo > 1 || $bar < 1 ) {
+
+} elseif ( $foo <= 1 ) {
+
+}
+
+while ( $foo !== 1 ) {
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/237902
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I4c8cc00f779d6b7e823f5db562af3d826e09fb6c
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: TasneemLo <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Polybuildr <[email protected]>
Gerrit-Reviewer: TasneemLo <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits