Skip to content

Commit f2cdd7a

Browse files
committed
PHP8 - Allow trailing comma in parameter list support
1 parent e52e854 commit f2cdd7a

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

tests/Fixer/ClassNotation/NoPhp4ConstructorFixerTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,4 +1134,45 @@ function Class1($foo)
11341134

11351135
$this->doTest($expected, $input);
11361136
}
1137+
1138+
/**
1139+
* @param string $expected
1140+
* @param null|string $input
1141+
*
1142+
* @dataProvider provideFixPhp80Cases
1143+
* @requires PHP 8.0
1144+
*/
1145+
public function testFixPhp80($expected, $input = null)
1146+
{
1147+
$this->doTest($expected, $input);
1148+
}
1149+
1150+
public function provideFixPhp80Cases()
1151+
{
1152+
yield [
1153+
<<<'EOF'
1154+
<?php
1155+
1156+
class Foo
1157+
{
1158+
public function __construct($bar,)
1159+
{
1160+
var_dump(1);
1161+
}
1162+
}
1163+
EOF
1164+
,
1165+
<<<'EOF'
1166+
<?php
1167+
1168+
class Foo
1169+
{
1170+
public function Foo($bar,)
1171+
{
1172+
var_dump(1);
1173+
}
1174+
}
1175+
EOF
1176+
];
1177+
}
11371178
}

tests/Fixer/ClassNotation/SelfAccessorFixerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,24 @@ public function provideFix71Cases()
205205
],
206206
];
207207
}
208+
209+
/**
210+
* @param string $expected
211+
* @param null|string $input
212+
*
213+
* @dataProvider provideFixPhp80Cases
214+
* @requires PHP 8.0
215+
*/
216+
public function testFixPhp80($expected, $input = null)
217+
{
218+
$this->doTest($expected, $input);
219+
}
220+
221+
public function provideFixPhp80Cases()
222+
{
223+
yield [
224+
'<?php interface Foo { public function bar(self $foo, self $bar,): self; }',
225+
'<?php interface Foo { public function bar(Foo $foo, Foo $bar,): Foo; }',
226+
];
227+
}
208228
}

tests/Fixer/FunctionNotation/FunctionDeclarationFixerTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ public function provideFix74Cases()
441441
'<?php fn &( $a ) => null;',
442442
self::$configurationClosureSpacingNone,
443443
],
444+
[
445+
'<?php fn&($a,$b) => null;',
446+
'<?php fn &( $a,$b ) => null;',
447+
self::$configurationClosureSpacingNone,
448+
],
444449
[
445450
'<?php $b = static fn ($a) => $a;',
446451
'<?php $b = static fn( $a ) => $a;',
@@ -452,4 +457,45 @@ public function provideFix74Cases()
452457
],
453458
];
454459
}
460+
461+
/**
462+
* @param string $expected
463+
* @param null|string $input
464+
*
465+
* @dataProvider provideFixPhp80Cases
466+
* @requires PHP 8.0
467+
*/
468+
public function testFixPhp80($expected, $input = null, array $configuration = [])
469+
{
470+
$this->fixer->configure($configuration);
471+
472+
$this->doTest($expected, $input);
473+
}
474+
475+
public function provideFixPhp80Cases()
476+
{
477+
yield [
478+
'<?php function ($i,) {};',
479+
'<?php function( $i, ) {};',
480+
];
481+
482+
yield [
483+
'<?php
484+
$b = static function ($a,$b,) {
485+
echo $a;
486+
};
487+
',
488+
'<?php
489+
$b = static function( $a,$b, ) {
490+
echo $a;
491+
};
492+
',
493+
];
494+
495+
yield [
496+
'<?php fn&($a,$b,) => null;',
497+
'<?php fn &( $a,$b, ) => null;',
498+
self::$configurationClosureSpacingNone,
499+
];
500+
}
455501
}

tests/Tokenizer/Analyzer/FunctionsAnalyzerTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,4 +713,47 @@ public function methodOne() {
713713
24,
714714
];
715715
}
716+
717+
/**
718+
* @param string $code
719+
* @param int $methodIndex
720+
* @param array $expected
721+
*
722+
* @dataProvider provideFunctionsWithArgumentsPhp80Cases
723+
* @requires PHP 8.0
724+
*/
725+
public function testFunctionArgumentInfoPhp80($code, $methodIndex, $expected)
726+
{
727+
$tokens = Tokens::fromCode($code);
728+
$analyzer = new FunctionsAnalyzer();
729+
730+
static::assertSame(serialize($expected), serialize($analyzer->getFunctionArguments($tokens, $methodIndex)));
731+
}
732+
733+
public function provideFunctionsWithArgumentsPhp80Cases()
734+
{
735+
yield ['<?php function($aa,){};', 1, [
736+
'$aa' => new ArgumentAnalysis(
737+
'$aa',
738+
3,
739+
null,
740+
null
741+
),
742+
]];
743+
744+
yield ['<?php fn($a, $bc ,) => null;', 1, [
745+
'$a' => new ArgumentAnalysis(
746+
'$a',
747+
3,
748+
null,
749+
null
750+
),
751+
'$bc' => new ArgumentAnalysis(
752+
'$bc',
753+
6,
754+
null,
755+
null
756+
),
757+
]];
758+
}
716759
}

tests/Tokenizer/TokensAnalyzerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,22 @@ public function provideIsLambda80Cases()
664664
};',
665665
[6 => true],
666666
],
667+
[
668+
'<?php
669+
$c = 4; //
670+
$a = function(
671+
$a,
672+
$b,
673+
) use (
674+
$c,
675+
) {
676+
echo $a + $b + $c;
677+
};
678+
679+
680+
$a(1,2);',
681+
[14 => true],
682+
],
667683
];
668684
}
669685

0 commit comments

Comments
 (0)