123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\ControlStructure;
- use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
- use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer
- */
- final class YodaStyleFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param array<string, mixed> $extraConfig
- *
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null, array $extraConfig = []): void
- {
- $this->fixer->configure(['equal' => true, 'identical' => true] + $extraConfig);
- $this->doTest($expected, $input);
- }
- /**
- * Test with the inverse config.
- *
- * @param array<string, mixed> $extraConfig
- *
- * @dataProvider provideFixCases
- */
- public function testFixInverse(string $expected, ?string $input = null, array $extraConfig = []): void
- {
- $this->fixer->configure(['equal' => false, 'identical' => false] + $extraConfig);
- if (null === $input) {
- $this->doTest($expected);
- } else {
- $this->doTest($input, $expected);
- }
- }
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php $a = 1 + ($b + $c) === true ? 1 : 2;',
- null,
- ['always_move_variable' => true],
- ];
- yield [
- '<?php $a = true === ($b + $c) ? 1 : 2;',
- '<?php $a = ($b + $c) === true ? 1 : 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php
- if ((1 === $a) === 1) {
- return;
- }',
- '<?php
- if (($a === 1) === 1) {
- return;
- }',
- ['always_move_variable' => false],
- ];
- yield [
- '<?php
- if (true === (1 !== $foo[0])) {
- return;
- }',
- '<?php
- if (($foo[0] !== 1) === true) {
- return;
- }',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return 1 !== $a [$b];',
- '<?php return $a [$b] !== 1;',
- ];
- yield [
- '<?= 1 === $a ? 5 : 7;',
- '<?= $a === 1 ? 5 : 7;',
- ];
- yield [
- '<?php print 1 === 1343;',
- ];
- yield [
- '<?php
- echo 3 === $a ? 2 : 4;
- ',
- '<?php
- echo $a === 3 ? 2 : 4;
- ',
- ];
- yield [
- '<?php 1 === foo($a) ? 1 : 2;',
- '<?php foo($a) === 1 ? 1 : 2;',
- ];
- yield [
- '<?php 1 === $a::$a ? 1 : 2;',
- '<?php $a::$a === 1 ? 1 : 2;',
- ];
- yield [
- '<?php 1 === (bool) $a ? 8 : 7;',
- '<?php (bool) $a === 1 ? 8 : 7;',
- ];
- yield [
- '<?php 1 === new $a ? 1 : 2;',
- '<?php new $a === 1 ? 1 : 2;',
- ];
- yield [
- '<?php 1 === "a".$a ? 5 : 6;',
- '<?php "a".$a === 1 ? 5 : 6;',
- ];
- yield [
- '<?php 1 === __DIR__.$a ? 5 : 6;',
- '<?php __DIR__.$a === 1 ? 5 : 6;',
- ];
- yield [
- '<?php 1 === $a.$b ? 5 : 6;',
- '<?php $a.$b === 1 ? 5 : 6;',
- ];
- yield [
- '<?php echo 1 === (object) $a ? 8 : 7;',
- '<?php echo (object) $a === 1 ? 8 : 7;',
- ];
- yield [
- '<?php echo 1 === (int) $a ? 8 : 7;',
- '<?php echo (int) $a === 1 ? 8 : 7;',
- ];
- yield [
- '<?php echo 1 === (float) $a ? 8 : 7;',
- '<?php echo (float) $a === 1 ? 8 : 7;',
- ];
- yield [
- '<?php echo 1 === (string) $a ? 8 : 7;',
- '<?php echo (string) $a === 1 ? 8 : 7;',
- ];
- yield [
- '<?php echo 1 === (array) $a ? 8 : 7;',
- '<?php echo (array) $a === 1 ? 8 : 7;',
- ];
- yield [
- '<?php echo 1 === (bool) $a ? 8 : 7;',
- '<?php echo (bool) $a === 1 ? 8 : 7;',
- ];
- yield [
- '<?php
- if ($a = true === $obj instanceof A) {
- echo \'A\';
- }',
- '<?php
- if ($a = $obj instanceof A === true) {
- echo \'A\';
- }',
- ];
- yield [
- '<?php echo 1 === !!$a ? 8 : 7;',
- '<?php echo !!$a === 1 ? 8 : 7;',
- ];
- yield [
- '<?php $a = 1 === new b ? 1 : 2;',
- '<?php $a = new b === 1 ? 1 : 2;',
- ];
- yield [
- '<?php $a = 1 === empty($a) ? 1 : 2;',
- '<?php $a = empty($a) === 1 ? 1 : 2;',
- ];
- yield [
- '<?php $b = 1 === clone $a ? 5 : 9;',
- '<?php $b = clone $a === 1 ? 5 : 9;',
- ];
- yield [
- '<?php while(1 === $a ? 1 : 2){};',
- '<?php while($a === 1 ? 1 : 2){};',
- ];
- yield [
- '<?php switch(1 === $a){
- case true: echo 1;
- };',
- '<?php switch($a === 1){
- case true: echo 1;
- };',
- ];
- yield [
- '<?php echo 1 === $a ? 1 : 2;',
- '<?php echo $a === 1 ? 1 : 2;',
- ];
- // Don't fix cases.
- yield ['<?php $a = 1 === 1;'];
- yield ['<?php $b = $b === $c;'];
- yield ['<?php $c = $$b === $$c;'];
- yield ['<?php $d = count($this->array[$var]) === $a;'];
- yield ['<?php $e = $a === count($this->array[$var]);'];
- yield ['<?php $f = ($a123 & self::MY_BITMASK) === $a;'];
- yield ['<?php $g = $a === ($a456 & self::MY_BITMASK);'];
- yield ['<?php $h = $this->getStuff() === $myVariable;'];
- yield ['<?php $i = $myVariable === $this->getStuff();'];
- yield ['<?php $j = 2 * $myVar % 3 === $a;'];
- yield ['<?php return $k === 2 * $myVar % 3;'];
- yield ['<?php $l = $c > 2;'];
- yield ['<?php return $this->myObject1->{$index}+$b === "";'];
- yield ['<?php return $m[2]+1 == 2;'];
- yield ['<?php return $foo === $bar[$baz][1];'];
- yield ['<?php $a = $b[$key]["1"] === $c["2"];'];
- yield ['<?php return $foo->$a === $foo->$b->$c;'];
- yield ['<?php return $x === 2 - 1;'];
- yield ['<?php return $x === 2-1;'];
- // https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/693
- yield ['<?php return array(2) == $o;'];
- yield ['<?php return $p == array(2);'];
- yield ['<?php return $p == array("2");'];
- yield ['<?php return $p == array(TWO);'];
- yield ['<?php return $p == array(array());'];
- yield ['<?php return $p == [[]];'];
- yield ['<?php return array($q) == $a;'];
- yield ['<?php return $r == array($a);'];
- yield ['<?php $s = ((array(2))) == $a;'];
- yield ['<?php $t = $a == ((array(2)));'];
- yield ['<?php list($a) = $c === array(1) ? $b : $d;'];
- yield ['<?php $b = 7 === list($a) = [7];'];
- yield ['<?php $a = function(){} === array(0);'];
- yield ['<?php $z = $n == list($a) = $b;'];
- yield ['<?php return $n == list($a) = $b;'];
- // Fix cases.
- yield 'Array destruct by ternary.' => [
- '<?php list($a) = 11 === $c ? $b : $d;',
- '<?php list($a) = $c === 11 ? $b : $d;',
- ];
- yield 'Less spacing.' => [
- '<?php $z=2==$a;$b=$c>1&&$c<=10;',
- '<?php $z=$a==2;$b=$c>1&&$c<=10;',
- ];
- yield 'Comments.' => [
- '<?php $z = /**/ /**/2/**/ /**/
- # aa
- /**/==/**/$a/***/;',
- '<?php $z = /**/ /**/$a/**/ /**/
- # aa
- /**/==/**/2/***/;',
- ];
- yield [
- '<?php return 2 == ($a)?>',
- ];
- yield [
- '<?php return ($a) == 2?>',
- ];
- yield [
- '<?php return 2 == ($a)?>',
- '<?php return ($a) == 2?>',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php $a = ($c === ((null === $b)));',
- '<?php $a = ($c === (($b === null)));',
- ];
- yield [
- '<?php return null == $a[2];',
- '<?php return $a[2] == null;',
- ];
- yield [
- '<?php return "" === $this->myArray[$index];',
- '<?php return $this->myArray[$index] === "";',
- ];
- yield [
- '<?php return "" === $this->myArray[$index]->/*1*//*2*//*3*/a;',
- '<?php return $this->myArray[$index]->/*1*//*2*//*3*/a === "";',
- ];
- yield [
- '<?php return "" === $this->myArray[$index]->a;',
- '<?php return $this->myArray[$index]->a === "";',
- ];
- yield [
- '<?php return "" === $this->myObject2-> {$index};',
- '<?php return $this->myObject2-> {$index} === "";',
- ];
- yield [
- '<?php return "" === $this->myObject3->{$index}->a;',
- '<?php return $this->myObject3->{$index}->a === "";',
- ];
- yield [
- '<?php return "" === $this->myObject4->{$index}->{$index}->a;',
- '<?php return $this->myObject4->{$index}->{$index}->a === "";',
- ];
- yield [
- '<?php return "" === $this->myObject4->$index->a;',
- '<?php return $this->myObject4->$index->a === "";',
- ];
- yield [
- '<?php return self::MY_CONST === self::$myVariable;',
- '<?php return self::$myVariable === self::MY_CONST;',
- ];
- yield [
- '<?php return \A\B\C::MY_CONST === \A\B\C::$myVariable;',
- '<?php return \A\B\C::$myVariable === \A\B\C::MY_CONST;',
- ];
- yield [
- '<?php $a = 1 == $$a?>',
- '<?php $a = $$a == 1?>',
- ];
- yield 'Nested case' => [
- '<?php return null === $a[0 === $b ? $c : $d];',
- '<?php return $a[$b === 0 ? $c : $d] === null;',
- ];
- yield [
- '<?php return null === $this->{null === $a ? "a" : "b"};',
- '<?php return $this->{$a === null ? "a" : "b"} === null;',
- ];
- yield 'Complex code sample.' => [
- '<?php
- if ($a == $b) {
- return null === $b ? (null === $a ? 0 : 0 === $a->b) : 0 === $b->a;
- } else {
- if ($c === (null === $b)) {
- return false === $d;
- }
- }',
- '<?php
- if ($a == $b) {
- return $b === null ? ($a === null ? 0 : $a->b === 0) : $b->a === 0;
- } else {
- if ($c === ($b === null)) {
- return $d === false;
- }
- }',
- ];
- yield [
- '<?php $b = list($a) = 7 === [7];', // makes no sense, but valid PHP syntax
- '<?php $b = list($a) = [7] === 7;',
- ];
- yield [
- '<?php $a = 1 === function(){};',
- '<?php $a = function(){} === 1;',
- ];
- yield [
- '<?php
- $z#1
- #2
- =
- #3
- 1#4
- #5
- ===#6
- #7
- $a#8
- #9
- ;#10',
- '<?php
- $z#1
- #2
- =
- #3
- $a#4
- #5
- ===#6
- #7
- 1#8
- #9
- ;#10',
- ];
- yield [
- '<?php $i = 2 === $this/*a*//*b*//*c*//*d*//*e*//*f*/->getStuff();',
- '<?php $i = $this/*a*//*b*//*c*//*d*//*e*//*f*/->getStuff() === 2;',
- ];
- yield [
- '<?php return "" === $this->myObject5->{$index}->/*1*//*2*/b;',
- '<?php return $this->myObject5->{$index}->/*1*//*2*/b === "";',
- ];
- yield [
- '<?php
- function hello() {}
- 1 === $a ? b() : c();
- ',
- '<?php
- function hello() {}
- $a === 1 ? b() : c();
- ',
- ];
- yield [
- '<?php
- class A{}
- 1 === $a ? b() : c();
- ',
- '<?php
- class A{}
- $a === 1 ? b() : c();
- ',
- ];
- yield [
- '<?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;
- }',
- ];
- yield [
- '<?php false === $a = array();',
- ];
- yield [
- '<?php $e = count($this->array[$var]) === $a;',
- '<?php $e = $a === count($this->array[$var]);',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php $i = $this->getStuff() === $myVariable;',
- '<?php $i = $myVariable === $this->getStuff();',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php $g = ($a789 & self::MY_BITMASK) === $a;',
- null,
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar + 2 === $k;',
- '<?php return $k === $myVar + 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar . $b === $k;',
- '<?php return $k === $myVar . $b;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar - 2 === $k;',
- '<?php return $k === $myVar - 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar * 2 === $k;',
- '<?php return $k === $myVar * 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar / 2 === $k;',
- '<?php return $k === $myVar / 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar % 2 === $k;',
- '<?php return $k === $myVar % 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar ** 2 === $k;',
- '<?php return $k === $myVar ** 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar < 2 === $k;',
- '<?php return $k === $myVar < 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar > 2 === $k;',
- '<?php return $k === $myVar > 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar <= 2 === $k;',
- '<?php return $k === $myVar <= 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar >= 2 === $k;',
- '<?php return $k === $myVar >= 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar . 2 === $k;',
- '<?php return $k === $myVar . 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar << 2 === $k;',
- '<?php return $k === $myVar << 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar >> 2 === $k;',
- '<?php return $k === $myVar >> 2;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return !$myVar === $k;',
- '<?php return $k === !$myVar;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return $myVar instanceof Foo === $k;',
- '<?php return $k === $myVar instanceof Foo;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return (bool) $myVar === $k;',
- '<?php return $k === (bool) $myVar;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return (int) $myVar === $k;',
- '<?php return $k === (int) $myVar;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return (float) $myVar === $k;',
- '<?php return $k === (float) $myVar;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return (string) $myVar === $k;',
- '<?php return $k === (string) $myVar;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return (array) $myVar === $k;',
- '<?php return $k === (array) $myVar;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php return (object) $myVar === $k;',
- '<?php return $k === (object) $myVar;',
- ['always_move_variable' => true],
- ];
- yield [
- '<?php $a = null === foo();',
- '<?php $a = foo() === null;',
- ];
- yield [
- '<?php $a = \'foo\' === foo();',
- '<?php $a = foo() === \'foo\';',
- ];
- yield [
- '<?php $a = "foo" === foo();',
- '<?php $a = foo() === "foo";',
- ];
- yield [
- '<?php $a = 1 === foo();',
- '<?php $a = foo() === 1;',
- ];
- yield [
- '<?php $a = 1.2 === foo();',
- '<?php $a = foo() === 1.2;',
- ];
- yield [
- '<?php $a = true === foo();',
- '<?php $a = foo() === true;',
- ];
- yield [
- '<?php $a = false === foo();',
- '<?php $a = foo() === false;',
- ];
- yield [
- '<?php $a = -1 === reset($foo);',
- '<?php $a = reset($foo) === -1;',
- ];
- yield [
- '<?php $a = - 1 === reset($foo);',
- '<?php $a = reset($foo) === - 1;',
- ];
- yield [
- '<?php $a = -/* bar */1 === reset($foo);',
- '<?php $a = reset($foo) === -/* bar */1;',
- ];
- yield [
- '<?php return array() === $array;',
- '<?php return $array === array();',
- ];
- yield [
- '<?php return [] === $array;',
- '<?php return $array === [];',
- ];
- yield [
- '<?php return array(/* foo */) === $array;',
- '<?php return $array === array(/* foo */);',
- ];
- yield [
- '<?php return [
- // 1
- ] === $array;',
- '<?php return $array === [
- // 1
- ];',
- ];
- yield [
- '<?php $a = $b = null === $c;',
- '<?php $a = $b = $c === null;',
- ];
- $template = '<?php $a = ($b + $c) %s 1 === true ? 1 : 2;';
- $operators = ['||', '&&'];
- foreach ($operators as $operator) {
- yield [
- \sprintf($template, $operator),
- null,
- ['always_move_variable' => true],
- ];
- }
- $assignmentOperators = ['=', '**=', '*=', '|=', '+=', '-=', '^=', '<<=', '>>=', '&=', '.=', '/=', '%=', '??='];
- $logicalOperators = ['xor', 'or', 'and', '||', '&&', '??'];
- foreach ([...$assignmentOperators, ...$logicalOperators] as $operator) {
- yield [
- \sprintf('<?php $a %s 4 === $b ? 2 : 3;', $operator),
- \sprintf('<?php $a %s $b === 4 ? 2 : 3;', $operator),
- ];
- }
- foreach ($assignmentOperators as $operator) {
- yield [
- \sprintf('<?php 1 === $x %s 2;', $operator),
- ];
- }
- yield ['<?php $a = $b + 1 <=> $d;'];
- yield [
- '<?php $a = new class(10) extends SomeClass implements SomeInterface {} === $a;/**/',
- ];
- yield [
- '<?php $a = $b ?? 1 ?? 2 == $d;',
- '<?php $a = $b ?? 1 ?? $d == 2;',
- ];
- yield [
- '<?php $a = 1 === new class(10) extends SomeClass implements SomeInterface {};/**/',
- '<?php $a = new class(10) extends SomeClass implements SomeInterface {} === 1;/**/',
- ];
- yield [
- '<?php
- function a() {
- for ($i = 1; $i <= 3; $i++) {
- echo yield 1 === $i ? 1 : 2;
- }
- }
- ',
- '<?php
- function a() {
- for ($i = 1; $i <= 3; $i++) {
- echo yield $i === 1 ? 1 : 2;
- }
- }
- ',
- ];
- yield [
- '<?php function test() {return yield 1 !== $a [$b];};',
- '<?php function test() {return yield $a [$b] !== 1;};',
- ];
- yield [
- '<?php function test() {return yield 1 === $a;};',
- '<?php function test() {return yield $a === 1;};',
- ];
- yield [
- '<?php
- $a = 1;
- switch ($a) {
- case 1 === $a:
- echo 123;
- break;
- }
- ',
- '<?php
- $a = 1;
- switch ($a) {
- case $a === 1:
- echo 123;
- break;
- }
- ',
- ];
- yield 'require' => [
- '<?php require 1 === $var ? "A.php" : "B.php";',
- '<?php require $var === 1 ? "A.php" : "B.php";',
- ];
- yield 'require_once' => [
- '<?php require_once 1 === $var ? "A.php" : "B.php";',
- '<?php require_once $var === 1 ? "A.php" : "B.php";',
- ];
- yield 'include' => [
- '<?php include 1 === $var ? "A.php" : "B.php";',
- '<?php include $var === 1 ? "A.php" : "B.php";',
- ];
- yield 'include_once' => [
- '<?php include_once 1 === $var ? "A.php" : "B.php";',
- '<?php include_once $var === 1 ? "A.php" : "B.php";',
- ];
- yield 'yield from' => [
- '<?php function test() {return yield from 1 === $a ? $c : $d;};',
- '<?php function test() {return yield from $a === 1 ? $c : $d;};',
- ];
- yield [
- '<?php if (1_000 === $b);',
- '<?php if ($b === 1_000);',
- ];
- yield [
- '<?php fn() => $c === array(1) ? $b : $d;',
- null,
- [
- 'less_and_greater' => false,
- ],
- ];
- }
- /**
- * @dataProvider provideLessGreaterCases
- */
- public function testFixLessGreater(string $expected, string $input): void
- {
- $this->fixer->configure(['less_and_greater' => true]);
- $this->doTest($expected, $input);
- }
- /**
- * Test with the inverse config.
- *
- * @dataProvider provideLessGreaterCases
- */
- public function testFixLessGreaterInverse(string $expected, string $input): void
- {
- $this->fixer->configure(['less_and_greater' => false]);
- $this->doTest($input, $expected);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideLessGreaterCases(): iterable
- {
- yield [
- '<?php $a = 3 <= $b;',
- '<?php $a = $b >= 3;',
- ];
- yield [
- '<?php $a = 3 > $b;',
- '<?php $a = $b < 3;',
- ];
- yield [
- '<?php $a = (3 > $b) || $d;',
- '<?php $a = ($b < 3) || $d;',
- ];
- }
- public function testComplexConfiguration(): void
- {
- $this->fixer->configure([
- 'equal' => null,
- 'identical' => true,
- 'less_and_greater' => false,
- ]);
- $this->doTest(
- '<?php
- $a = 1 === $b;
- $b = $c != 1;
- $c = $c > 3;
- ',
- '<?php
- $a = $b === 1;
- $b = $c != 1;
- $c = $c > 3;
- '
- );
- }
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideInvalidConfigCases
- */
- public function testInvalidConfig(array $config, string $expectedMessage): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches("#^\\[{$this->fixer->getName()}\\] {$expectedMessage}$#");
- $this->fixer->configure($config);
- }
- public static function provideInvalidConfigCases(): iterable
- {
- yield [['equal' => 2], 'Invalid configuration: The option "equal" with value 2 is expected to be of type "bool" or "null", but is of type "(int|integer)"\.'];
- yield [['_invalid_' => true], 'Invalid configuration: The option "_invalid_" does not exist\. Defined options are: "always_move_variable", "equal", "identical", "less_and_greater"\.'];
- }
- public function testDefinition(): void
- {
- self::assertInstanceOf(FixerDefinitionInterface::class, $this->fixer->getDefinition());
- }
- /**
- * @dataProvider providePHP71Cases
- */
- public function testPHP71(string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['equal' => true, 'identical' => true]);
- $this->doTest($expected, $input);
- }
- /**
- * Test with the inverse config.
- *
- * @dataProvider providePHP71Cases
- */
- public function testPHP71Inverse(string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['equal' => false, 'identical' => false]);
- if (null === $input) {
- $this->doTest($expected);
- } else {
- $this->doTest($input, $expected);
- }
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function providePHP71Cases(): iterable
- {
- // no fix cases
- yield ['<?php list("a" => $a, "b" => $b, "c" => $c) = $c === array(1) ? $b : $d;'];
- yield ['<?php list(list("x" => $x1, "y" => $y1), list("x" => $x2, "y" => $y2)) = $points;'];
- yield ['<?php list("first" => list($x1, $y1), "second" => list($x2, $y2)) = $points;'];
- yield ['<?php [$a, $b, $c] = [1, 2, 3];'];
- yield ['<?php ["a" => $a, "b" => $b, "c" => $c] = $a[0];'];
- yield ['<?php $b = 7 === [$a] = [7];']; // makes no sense, but valid PHP syntax
- yield ['<?php [$a] = $c === array(1) ? $b : $d;'];
- yield ['<?php $z = $n == [$a] = $b;'];
- yield ['<?php return $n == [$a] = $b;'];
- // fix cases
- yield [
- '<?php list("a" => $a, "b" => $b, "c" => $c) = 1 === $c ? $b : $d;',
- '<?php list("a" => $a, "b" => $b, "c" => $c) = $c === 1 ? $b : $d;',
- ];
- yield [
- '<?php list("a" => $a, "b" => $b, "c" => $c) = A::B === $c ? $b : $d;',
- '<?php list("a" => $a, "b" => $b, "c" => $c) = $c === A::B ? $b : $d;',
- ];
- yield [
- '<?php list( (2 === $c ? "a" : "b") => $b) = ["a" => 7 === $c ? 5 : 1, "b" => 7];',
- '<?php list( ($c === 2 ? "a" : "b") => $b) = ["a" => $c === 7 ? 5 : 1, "b" => 7];',
- ];
- yield [
- '<?php [ (ABC::A === $c ? "a" : "b") => $b] = ["a" => 7 === $c ? 5 : 1, "b" => 7];',
- '<?php [ ($c === ABC::A ? "a" : "b") => $b] = ["a" => $c === 7 ? 5 : 1, "b" => 7];',
- ];
- yield 'Array destruct by ternary.' => [
- '<?php [$a] = 11 === $c ? $b : $d;',
- '<?php [$a] = $c === 11 ? $b : $d;',
- ];
- yield [
- '<?php $b = [$a] = 7 === [7];', // makes no sense, but valid PHP syntax
- '<?php $b = [$a] = [7] === 7;',
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideWithConfigCases
- */
- public function testWithConfig(array $config, string $expected): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected);
- }
- public static function provideWithConfigCases(): iterable
- {
- yield [
- [
- 'identical' => false,
- ],
- '<?php
- $a = [1, 2, 3];
- while (2 !== $b = array_pop($c));
- ',
- ];
- yield [
- [
- '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\'];
- }
- }',
- ];
- }
- /**
- * @dataProvider provideFixPrePHP80Cases
- *
- * @requires PHP <8.0
- */
- public function testFixPrePHP80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixPrePHP80Cases(): iterable
- {
- yield [
- '<?php return \A/*5*/\/*6*/B\/*7*/C::MY_CONST === \A/*1*//*1*//*1*//*1*//*1*/\/*2*/B/*3*/\C/*4*/::$myVariable;',
- '<?php return \A/*1*//*1*//*1*//*1*//*1*/\/*2*/B/*3*/\C/*4*/::$myVariable === \A/*5*/\/*6*/B\/*7*/C::MY_CONST;',
- ];
- yield [
- '<?php return A\/**//**//**/B/*a*//*a*//*a*//*a*/::MY_CONST === B\C::$myVariable;',
- '<?php return B\C::$myVariable === A\/**//**//**/B/*a*//*a*//*a*//*a*/::MY_CONST;',
- ];
- yield ['<?php return $foo === $bar[$baz]{1};'];
- yield ['<?php return $foo->$a[1] === $bar[$baz]{1}->$a[1][2][3]->$d[$z]{1};'];
- yield ['<?php return $m->a{2}+1 == 2;'];
- yield ['<?php return $m{2}+1 == 2;'];
- yield [
- '<?php echo 1 === (unset) $a ? 8 : 7;',
- '<?php echo (unset) $a === 1 ? 8 : 7;',
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, ?string $input = null, array $config = []): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- public static function provideFix80Cases(): iterable
- {
- yield [
- '<?php
- if ($a = true === $obj instanceof (foo())) {
- echo 1;
- }',
- '<?php
- if ($a = $obj instanceof (foo()) === true) {
- echo 1;
- }',
- ];
- yield [
- '<?php $i = $this?->getStuff() === $myVariable;',
- '<?php $i = $myVariable === $this?->getStuff();',
- ['equal' => true, 'identical' => true, 'always_move_variable' => true],
- ];
- yield [
- '<?php 42 === $a->b[5]?->c;',
- '<?php $a->b[5]?->c === 42;',
- ];
- yield [
- '<?php return $this->myObject1?->{$index}+$b === "";',
- null,
- ['equal' => true, 'identical' => true],
- ];
- yield [
- '<?php new Foo(bar: 1 === $var);',
- '<?php new Foo(bar: $var === 1);',
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield 'does not make a lot of sense but is valid syntax, do not break 1' => [
- '<?php $b = strlen( ... ) === $a;',
- ];
- yield 'does not make a lot of sense but is valid syntax, do not break 2' => [
- '<?php $b = $a === strlen( ... );',
- ];
- }
- }
|