Skip to content

Commit 2537d90

Browse files
committed
bug #4638 Ensure compatibility with PHP 7.4 (julienfalque)
This PR was merged into the 2.16 branch. Discussion ---------- Ensure compatibility with PHP 7.4 Same as previous PRs related to #4382 but for branch 2.16. Commits ------- fd4130a Ensure compatibility with PHP 7.4
2 parents 5f93e38 + fd4130a commit 2537d90

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed

src/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixer.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,19 @@ public function getDefinition()
5757
*/
5858
public function isCandidate(Tokens $tokens)
5959
{
60-
return \PHP_VERSION_ID >= 70100 && $tokens->isAllTokenKindsFound([T_FUNCTION, T_VARIABLE]);
60+
if (\PHP_VERSION_ID < 70100) {
61+
return false;
62+
}
63+
64+
if (!$tokens->isTokenKindFound(T_VARIABLE)) {
65+
return false;
66+
}
67+
68+
if (\PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_FN)) {
69+
return true;
70+
}
71+
72+
return $tokens->isTokenKindFound(T_FUNCTION);
6173
}
6274

6375
/**
@@ -89,10 +101,15 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
89101
{
90102
$functionsAnalyzer = new FunctionsAnalyzer();
91103

104+
$tokenKinds = [T_FUNCTION];
105+
if (\PHP_VERSION_ID >= 70400) {
106+
$tokenKinds[] = T_FN;
107+
}
108+
92109
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
93110
$token = $tokens[$index];
94111

95-
if (!$token->isGivenKind(T_FUNCTION)) {
112+
if (!$token->isGivenKind($tokenKinds)) {
96113
continue;
97114
}
98115

src/Fixer/Phpdoc/PhpdocLineSpanFixer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
2121
use PhpCsFixer\FixerDefinition\CodeSample;
2222
use PhpCsFixer\FixerDefinition\FixerDefinition;
23+
use PhpCsFixer\Tokenizer\CT;
2324
use PhpCsFixer\Tokenizer\Token;
2425
use PhpCsFixer\Tokenizer\Tokens;
2526
use PhpCsFixer\Tokenizer\TokensAnalyzer;
@@ -130,6 +131,9 @@ private function getDocBlockIndex(Tokens $tokens, $index)
130131
T_COMMENT,
131132
T_VAR,
132133
T_STATIC,
134+
T_STRING,
135+
T_NS_SEPARATOR,
136+
CT::T_NULLABLE_TYPE,
133137
]));
134138

135139
return $index;

tests/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixerTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,62 @@ public function provideInverseOnlyFixCases()
303303
'<?php function foo(? /*comment*/ string $param = null) {}',
304304
];
305305
}
306+
307+
/**
308+
* @param string $input
309+
* @param string $expected
310+
*
311+
* @dataProvider provideFixPhp74Cases
312+
* @requires PHP 7.4
313+
*/
314+
public function testFixPhp74($input, $expected)
315+
{
316+
$this->doTest($expected, $input);
317+
}
318+
319+
/**
320+
* @param string $input
321+
* @param string $expected
322+
*
323+
* @dataProvider provideFixPhp74Cases
324+
* @requires PHP 7.4
325+
*/
326+
public function testFixInversePhp74($expected, $input)
327+
{
328+
$this->fixer->configure(['use_nullable_type_declaration' => false]);
329+
330+
$this->doTest($expected, $input);
331+
}
332+
333+
public function provideFixPhp74Cases()
334+
{
335+
yield [
336+
'<?php $foo = fn (string $param = null) => null;',
337+
'<?php $foo = fn (?string $param = null) => null;',
338+
];
339+
yield [
340+
'<?php $foo = fn (string &$param = null) => null;',
341+
'<?php $foo = fn (?string &$param = null) => null;',
342+
];
343+
yield [
344+
'<?php $foo = fn (Baz $param = null) => null;',
345+
'<?php $foo = fn (?Baz $param = null) => null;',
346+
];
347+
yield [
348+
'<?php $foo = fn (Baz &$param = null) => null;',
349+
'<?php $foo = fn (?Baz &$param = null) => null;',
350+
];
351+
yield [
352+
'<?php $foo = fn (Baz & $param = null) => null;',
353+
'<?php $foo = fn (?Baz & $param = null) => null;',
354+
];
355+
yield [
356+
'<?php $foo = fn(array $a = null,
357+
array $b = null, array $c = null, array
358+
$d = null) => null;',
359+
'<?php $foo = fn(?array $a = null,
360+
?array $b = null, ?array $c = null, ?array
361+
$d = null) => null;',
362+
];
363+
}
306364
}

tests/Fixer/Phpdoc/PhpdocLineSpanFixerTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,46 @@ class Foo
429429
*
430430
*/
431431
private $foo;
432+
}',
433+
[
434+
'property' => 'single',
435+
],
436+
],
437+
];
438+
}
439+
440+
/**
441+
* @requires PHP 7.4
442+
* @dataProvider provideFixPhp74Cases
443+
*
444+
* @param string $expected
445+
* @param string $input
446+
*/
447+
public function testFixPhp74($expected, $input = null, array $config = [])
448+
{
449+
$this->fixer->configure($config);
450+
$this->doTest($expected, $input);
451+
}
452+
453+
public function provideFixPhp74Cases()
454+
{
455+
return [
456+
'It can handle properties with type declaration' => [
457+
'<?php
458+
459+
class Foo
460+
{
461+
/** */
462+
private ?string $foo;
463+
}',
464+
'<?php
465+
466+
class Foo
467+
{
468+
/**
469+
*
470+
*/
471+
private ?string $foo;
432472
}',
433473
[
434474
'property' => 'single',

0 commit comments

Comments
 (0)