123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?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\Alias;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Alias\ArrayPushFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\ArrayPushFixer>
- */
- final class ArrayPushFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- yield 'minimal' => [
- '<?php $a[] =$b;',
- '<?php array_push($a,$b);',
- ];
- yield 'simple' => [
- '<?php $a[] = $b;',
- '<?php array_push($a, $b);',
- ];
- yield 'simple, spaces' => [
- '<?php $a [] =$b ;',
- '<?php array_push( $a , $b );',
- ];
- yield 'simple 25x times' => [
- '<?php '.str_repeat('$a[] = $b;', 25),
- '<?php '.str_repeat('array_push($a, $b);', 25),
- ];
- yield 'simple namespaced' => [
- '<?php $a[] = $b1;',
- '<?php \array_push($a, $b1);',
- ];
- yield '; before' => [
- '<?php ; $a1[] = $b2 ?>',
- '<?php ; array_push($a1, $b2) ?>',
- ];
- yield ') before' => [
- '<?php
- if ($c) $a[] = $b;
- while (--$c > 0) $a[] = $c;
- ',
- '<?php
- if ($c) array_push($a, $b);
- while (--$c > 0) array_push($a, $c);
- ',
- ];
- yield '} before' => [
- '<?php $b3 = []; { $a = 1; } $b5[] = 1;',
- '<?php $b3 = []; { $a = 1; } \array_push($b5, 1);',
- ];
- yield '{ before' => [
- '<?php { $a[] = $b8; }',
- '<?php { array_push($a, $b8); }',
- ];
- yield 'comments and PHPDoc' => [
- '<?php /* */ $a2[] = $b3 /** */;',
- '<?php /* */ array_push($a2, $b3) /** */;',
- ];
- yield [
- '<?php $a4[1][] = $b6[2];',
- '<?php array_push($a4[1], $b6[2]);',
- ];
- yield 'case insensitive and precedence' => [
- '<?php
- $a[] = $b--;
- $a[] = ++$b;
- $a[] = !$b;
- $a[] = $b + $c;
- $a[] = 1 ** $c / 2 || !b && c(1,2,3) ^ $a[1];
- ',
- '<?php
- array_push($a, $b--);
- ARRAY_push($a, ++$b);
- array_PUSH($a, !$b);
- ARRAY_PUSH($a, $b + $c);
- \array_push($a, 1 ** $c / 2 || !b && c(1,2,3) ^ $a[1]);
- ',
- ];
- yield 'simple traditional array' => [
- '<?php $a[] = array($b, $c);',
- '<?php array_push($a, array($b, $c));',
- ];
- yield 'simple short array' => [
- '<?php $a[] = [$b];',
- '<?php array_push($a, [$b]);',
- ];
- yield 'multiple element short array' => [
- '<?php $a[] = [[], [], $b, $c];',
- '<?php array_push($a, [[], [], $b, $c]);',
- ];
- yield 'second argument wrapped in `(` `)`' => [
- '<?php $a::$c[] = ($b);',
- '<?php array_push($a::$c, ($b));',
- ];
- yield [
- '<?php $a::$c[] = $b;',
- '<?php array_push($a::$c, $b);',
- ];
- yield [
- '<?php $a[foo(1,2,3)][] = $b[foo(1,2,3)];',
- '<?php array_push($a[foo(1,2,3)], $b[foo(1,2,3)]);',
- ];
- yield [
- '<?php \A\B::$foo[] = 1;',
- '<?php array_push(\A\B::$foo, 1);',
- ];
- yield [
- '<?php static::$foo[] = 1;',
- '<?php array_push(static::$foo, 1);',
- ];
- yield [
- '<?php namespace\A::$foo[] = 1;',
- '<?php array_push(namespace\A::$foo, 1);',
- ];
- yield [
- '<?php foo()->bar[] = 1;',
- '<?php array_push(foo()->bar, 1);',
- ];
- yield [
- '<?php foo()[] = 1;',
- '<?php array_push(foo(), 1);',
- ];
- yield [
- '<?php $a->$c[] = $b;',
- '<?php array_push($a->$c, $b);',
- ];
- yield [
- '<?php $a->$c[1]->$d[$a--]->$a[7][] = $b;',
- '<?php array_push($a->$c[1]->$d[$a--]->$a[7], $b);',
- ];
- yield 'push multiple' => [
- '<?php array_push($a6, $b9, $c);',
- ];
- yield 'push multiple II' => [
- '<?php ; array_push($a6a, $b9->$a(1,2), $c);',
- ];
- yield 'push multiple short' => [
- '<?php array_push($a6, [$b,$c], []);',
- ];
- yield 'returns number of elements in the array I' => [
- '<?php foo(array_push($a7, $b10)); // ;array_push($a, $b);',
- ];
- yield 'returns number of elements in the array II' => [
- '<?php $a = 3 * array_push($a8, $b11);',
- ];
- yield 'returns number of elements in the array III' => [
- '<?php $a = foo($z, array_push($a9, $b12));',
- ];
- yield 'returns number of elements in the array IV' => [
- '<?php $z = array_push($a00, $b13);',
- ];
- yield 'function declare in different namespace' => [
- '<?php namespace Foo; function array_push($a11, $b14){};',
- ];
- yield 'overridden detect I' => [
- '<?php namespace Foo; array_push(1, $a15);',
- ];
- yield 'overridden detect II' => [
- '<?php namespace Foo; array_push($a + 1, $a16);',
- ];
- yield 'different namespace and not a function call' => [
- '<?php
- A\array_push($a, $b17);
- A::array_push($a, $b18);
- $a->array_push($a, $b19);
- ',
- ];
- yield 'open echo' => [
- '<?= array_push($a, $b20) ?> <?= array_push($a, $b20); ?>',
- ];
- yield 'ellipsis' => [
- '<?php array_push($a, ...$b21);',
- ];
- $precedenceCases = [
- '$b and $c',
- '$b or $c',
- '$b xor $c',
- ];
- foreach ($precedenceCases as $precedenceCase) {
- yield [
- \sprintf(
- '<?php array_push($a, %s);',
- $precedenceCase
- ),
- ];
- }
- yield [
- '<?php
- while (foo()) $a[] = $b;
- foreach (foo() as $C) $a[] = $b;
- if (foo()) $a[] = $b;
- if ($b) {} elseif (foo()) $a[] = $b;
- ',
- '<?php
- while (foo()) array_push($a, $b);
- foreach (foo() as $C) array_push($a, $b);
- if (foo()) array_push($a, $b);
- if ($b) {} elseif (foo()) array_push($a, $b);
- ',
- ];
- }
- /**
- * @dataProvider provideFixPre80Cases
- *
- * @requires PHP <8.0
- */
- public function testFixPre80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixPre80Cases(): iterable
- {
- yield [
- '<?php $a5{1*3}[2+1][] = $b4{2+1};',
- '<?php array_push($a5{1*3}[2+1], $b4{2+1});',
- ];
- yield [
- '<?php $a5{1*3}[2+1][] = $b7{2+1};',
- '<?php array_push($a5{1*3}[2+1], $b7{2+1});',
- ];
- }
- /**
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string}>
- */
- public static function provideFix80Cases(): iterable
- {
- yield [
- '<?php array_push($b?->c[2], $b19);',
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string, string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield 'simple 8.1' => [
- '<?php
- $a[] = $b;
- $a = array_push(...);
- ',
- '<?php
- array_push($a, $b);
- $a = array_push(...);
- ',
- ];
- }
- /**
- * @dataProvider provideFixPre84Cases
- *
- * @requires PHP <8.4
- */
- public function testFixPre84(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixPre84Cases(): iterable
- {
- yield [
- '<?php $a->$c[1]->$d{$a--}->$a[7][] = $b;',
- '<?php array_push($a->$c[1]->$d{$a--}->$a[7], $b);',
- ];
- }
- }
|