Umherirrender has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/400240 )

Change subject: Handle alternative if in IfElseStructureSniff
......................................................................

Handle alternative if in IfElseStructureSniff

Use scope opener to look for colon (:) or curly bracket ({) as opener.

Bug: T178772
Change-Id: Ib851016c87dfbba4c2ddc310c67daa5ab4df2536
---
M MediaWiki/Sniffs/ControlStructures/IfElseStructureSniff.php
M MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php
M MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php.expect
M MediaWiki/Tests/files/ControlStructures/if_else_structure.php
M MediaWiki/Tests/files/ControlStructures/if_else_structure.php.expect
M MediaWiki/Tests/files/ControlStructures/if_else_structure.php.fixed
6 files changed, 75 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer 
refs/changes/40/400240/1

diff --git a/MediaWiki/Sniffs/ControlStructures/IfElseStructureSniff.php 
b/MediaWiki/Sniffs/ControlStructures/IfElseStructureSniff.php
index db710d3..4a593f3 100644
--- a/MediaWiki/Sniffs/ControlStructures/IfElseStructureSniff.php
+++ b/MediaWiki/Sniffs/ControlStructures/IfElseStructureSniff.php
@@ -32,10 +32,14 @@
                $tokens = $phpcsFile->getTokens();
                $prevToken = $tokens[$stackPtr - 1];
                $nextToken = $tokens[$stackPtr + 1];
+               $scopeOpener = $tokens[$stackPtr]['scope_opener'];
+               $isAlternativeIfSyntax = $tokens[$scopeOpener]['code'] === 
T_COLON;
 
                // single space expected before else and elseif structure
-               if ( $prevToken['code'] !== T_WHITESPACE
-                       || $prevToken['content'] !== " " ) {
+               if ( !$isAlternativeIfSyntax &&
+                       ( $prevToken['code'] !== T_WHITESPACE
+                       || $prevToken['content'] !== " " )
+               ) {
                        $fix = $phpcsFile->addFixableWarning(
                                'Single space expected before "%s"',
                                $stackPtr + 1,
@@ -59,8 +63,10 @@
                        return;
                }
                // single space expected after else structure
-               if ( $nextToken['code'] !== T_WHITESPACE
-                       || $nextToken['content'] !== " " ) {
+               if ( !$isAlternativeIfSyntax &&
+                       ( $nextToken['code'] !== T_WHITESPACE
+                       || $nextToken['content'] !== " " )
+               ) {
                        $fix = $phpcsFile->addFixableWarning(
                                'Single space expected after "%s"',
                                $stackPtr + 1,
@@ -79,5 +85,23 @@
                                }
                        }
                }
+               // no space expected after else structure, when it is 
alternative syntax
+               if ( $isAlternativeIfSyntax
+                       && $nextToken['code'] === T_WHITESPACE
+               ) {
+                       $fix = $phpcsFile->addFixableWarning(
+                               'No space expected after "%s"',
+                               $stackPtr + 1,
+                               'SpaceAfterElse',
+                               [ $tokens[$stackPtr]['content'] ]
+                       );
+                       if ( $fix === true ) {
+                               // Replace all after whitespace with no space
+                               $phpcsFile->fixer->replaceToken( $stackPtr + 1, 
'' );
+                               for ( $i = 2; $tokens[$stackPtr + $i]['code'] 
=== T_WHITESPACE; $i++ ) {
+                                       $phpcsFile->fixer->replaceToken( 
$stackPtr + $i, '' );
+                               }
+                       }
+               }
        }
 }
diff --git a/MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php 
b/MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php
index a957cfa..5b22aeb 100644
--- a/MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php
+++ b/MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php
@@ -21,6 +21,10 @@
 
        if ( $x < 2 ):
                echo $x;
+       elseif ( $x > 10 ):
+               echo $i;
+       else:
+               echo $x . $i;
        endif;
 
        switch ( $x ):
diff --git 
a/MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php.expect 
b/MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php.expect
index c036217..0613a00 100644
--- a/MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php.expect
+++ b/MediaWiki/Tests/files/AlternativeSyntax/alternative_syntax.php.expect
@@ -7,11 +7,11 @@
  18 | WARNING | Alternative syntax such as "endforeach" should not be
     |         | used
     |         | 
(MediaWiki.AlternativeSyntax.AlternativeSyntax.AlternativeSyntax)
- 24 | WARNING | Alternative syntax such as "endif" should not be used
+ 28 | WARNING | Alternative syntax such as "endif" should not be used
     |         | 
(MediaWiki.AlternativeSyntax.AlternativeSyntax.AlternativeSyntax)
- 30 | WARNING | Alternative syntax such as "endswitch" should not be
+ 34 | WARNING | Alternative syntax such as "endswitch" should not be
     |         | used
     |         | 
(MediaWiki.AlternativeSyntax.AlternativeSyntax.AlternativeSyntax)
- 34 | WARNING | Alternative syntax such as "endwhile" should not be
+ 38 | WARNING | Alternative syntax such as "endwhile" should not be
     |         | used
     |         | 
(MediaWiki.AlternativeSyntax.AlternativeSyntax.AlternativeSyntax)
\ No newline at end of file
diff --git a/MediaWiki/Tests/files/ControlStructures/if_else_structure.php 
b/MediaWiki/Tests/files/ControlStructures/if_else_structure.php
index 10bc02d..0826431 100644
--- a/MediaWiki/Tests/files/ControlStructures/if_else_structure.php
+++ b/MediaWiki/Tests/files/ControlStructures/if_else_structure.php
@@ -12,9 +12,16 @@
        elseif ( $a ) {
                # code...
        }
-       else {
+       else{
                # code...
        }
+       if ( $a ) :
+               # code...
+       elseif ( $a ) :
+               # code...
+       else :
+               # code...
+       endif;
 }
 
 /**
@@ -30,4 +37,11 @@
        } else {
                # code...
        }
+       if ( $a ):
+               # code...
+       elseif ( $a ):
+               # code...
+       else:
+               # code...
+       endif;
 }
diff --git 
a/MediaWiki/Tests/files/ControlStructures/if_else_structure.php.expect 
b/MediaWiki/Tests/files/ControlStructures/if_else_structure.php.expect
index fda2e66..d71d29c 100644
--- a/MediaWiki/Tests/files/ControlStructures/if_else_structure.php.expect
+++ b/MediaWiki/Tests/files/ControlStructures/if_else_structure.php.expect
@@ -2,4 +2,14 @@
     |         |     
(MediaWiki.ControlStructures.IfElseStructure.SpaceBeforeElse)
  15 | WARNING | [x] Single space expected before "else"
     |         |     
(MediaWiki.ControlStructures.IfElseStructure.SpaceBeforeElse)
-PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
\ No newline at end of file
+ 15 | WARNING | [x] Single space expected after "else"
+    |         |     
(MediaWiki.ControlStructures.IfElseStructure.SpaceAfterElse)
+ 22 | WARNING | [x] No space expected after "else"
+    |         |     
(MediaWiki.ControlStructures.IfElseStructure.SpaceAfterElse)
+ 24 | WARNING | [ ] Alternative syntax such as "endif" should not be
+    |         |     used
+    |         |     
(MediaWiki.AlternativeSyntax.AlternativeSyntax.AlternativeSyntax)
+ 46 | WARNING | [ ] Alternative syntax such as "endif" should not be
+    |         |     used
+    |         |     
(MediaWiki.AlternativeSyntax.AlternativeSyntax.AlternativeSyntax)
+PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
\ No newline at end of file
diff --git 
a/MediaWiki/Tests/files/ControlStructures/if_else_structure.php.fixed 
b/MediaWiki/Tests/files/ControlStructures/if_else_structure.php.fixed
index dfaeb30..94f97d8 100644
--- a/MediaWiki/Tests/files/ControlStructures/if_else_structure.php.fixed
+++ b/MediaWiki/Tests/files/ControlStructures/if_else_structure.php.fixed
@@ -13,6 +13,13 @@
        } else {
                # code...
        }
+       if ( $a ) :
+               # code...
+       elseif ( $a ) :
+               # code...
+       else:
+               # code...
+       endif;
 }
 
 /**
@@ -28,4 +35,11 @@
        } else {
                # code...
        }
+       if ( $a ):
+               # code...
+       elseif ( $a ):
+               # code...
+       else:
+               # code...
+       endif;
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/400240
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib851016c87dfbba4c2ddc310c67daa5ab4df2536
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to