Browse Source

Merge branch '1.12'

Conflicts:
	src/Fixer/Operator/TernaryOperatorSpacesFixer.php
Dariusz Ruminski 8 years ago
parent
commit
3d74083600

+ 13 - 3
src/Fixer/Operator/TernaryOperatorSpacesFixer.php

@@ -87,15 +87,25 @@ final class TernaryOperatorSpacesFixer extends AbstractFixer
         return 'Standardize spaces around ternary operator.';
     }
 
+    /**
+     * @param Tokens $tokens
+     * @param int    $index
+     * @param bool   $after
+     */
     private function ensureWhitespaceExistence(Tokens $tokens, $index, $after)
     {
-        $indexChange = $after ? 0 : 1;
-        $token = $tokens[$index];
+        if ($tokens[$index]->isWhitespace()) {
+            if (
+                false === strpos($tokens[$index]->getContent(), "\n")
+                && !$tokens[$index - 1]->isComment()
+            ) {
+                $tokens[$index]->setContent(' ');
+            }
 
-        if ($token->isWhitespace()) {
             return;
         }
 
+        $indexChange = $after ? 0 : 1;
         $tokens->insertAt($index + $indexChange, new Token(array(T_WHITESPACE, ' ')));
     }
 }

+ 18 - 1
tests/Fixer/Operator/TernaryOperatorSpacesFixerTest.php

@@ -32,6 +32,10 @@ final class TernaryOperatorSpacesFixerTest extends AbstractFixerTestCase
     public function provideCases()
     {
         return array(
+            array(
+                '<?php $a = $a ? 1 : 0;',
+                '<?php $a = $a  ? 1 : 0;',
+            ),
             array(
                 '<?php $val = (1===1) ? true : false;',
                 '<?php $val = (1===1)?true:false;',
@@ -42,8 +46,11 @@ final class TernaryOperatorSpacesFixerTest extends AbstractFixerTestCase
             ),
             array(
                 '<?php
-$a = $b  ? 2 : 3;
+$a = $b ? 2 : ($bc ? 2 : 3);
 $a = $bc ? 2 : 3;',
+                '<?php
+$a = $b   ?   2  :    ($bc?2:3);
+$a = $bc?2:3;',
             ),
             array(
                 '<?php $config = $config ?: new Config();',
@@ -69,6 +76,16 @@ $a = $b
             ),
             array(
                 '<?php
+$a = $b  //
+    ? $c  /**/
+    : $d;',
+                '<?php
+$a = $b  //
+    ?$c  /**/
+    :$d;',
+            ),
+            array(
+                '<?php
 $a = ($b
     ? $c
     : ($d