Browse Source

YodaStyleFixer - do not un-Yoda if right side is assignment

SpacePossum 7 years ago
parent
commit
464d613afa

+ 5 - 0
src/Fixer/ControlStructure/YodaStyleFixer.php

@@ -348,6 +348,11 @@ final class YodaStyleFixer extends AbstractFixer implements ConfigurationDefinit
             }
 
             $right = $this->getRightSideCompareFixableInfo($tokens, $index);
+
+            if ($tokens[$tokens->getNextMeaningfulToken($right['end'])]->equals('=')) {
+                return null;
+            }
+
             $otherIsVar = $this->isVariable($tokens, $right['start'], $right['end']);
         }
 

+ 62 - 0
tests/Fixer/ControlStructure/YodaStyleFixerTest.php

@@ -390,6 +390,25 @@ $a#4
                 $a === 1 ? b() : c();
                 ',
             ],
+            [
+               '<?php
+                function foo() {
+                    foreach ($arr as $key => $value) {
+                        false !== uniqid() ? 1 : 2;
+                    }
+                    false !== uniqid() ? 1 : 2;
+                }',
+                '<?php
+                function foo() {
+                    foreach ($arr as $key => $value) {
+                        uniqid() !== false ? 1 : 2;
+                    }
+                    uniqid() !== false ? 1 : 2;
+                }',
+            ],
+            [
+                '<?php false === $a = array();',
+            ],
         ];
     }
 
@@ -679,4 +698,47 @@ function a() {
             ],
         ];
     }
+
+    /**
+     * @param array  $config
+     * @param string $expected
+     *
+     * @dataProvider provideFixWithConfigCases
+     */
+    public function testWithConfig(array $config, $expected)
+    {
+        $this->fixer->configure($config);
+        $this->doTest($expected);
+    }
+
+    public function provideFixWithConfigCases()
+    {
+        return [
+            [
+                [
+                    'identical' => false,
+                ],
+            '<?php
+$a = [1, 2, 3];
+while (2 !== $b = array_pop($c));
+',
+            ],
+            [
+                [
+                    'equal' => false,
+                    'identical' => false,
+                ],
+                '<?php
+                if ($revision->event == \'created\') {
+    foreach ($revision->getModified() as $col => $data) {
+        $model->$col = $data[\'new\'];
+    }
+} else {
+    foreach ($revision->getModified() as $col => $data) {
+        $model->$col = $data[\'old\'];
+    }
+}',
+            ],
+        ];
+    }
 }