Skip to content

Commit e362435

Browse files
kubawerloskeradus
authored andcommitted
TypeAlternationTransformer - fix for "array" type in type alternation
1 parent c2f6aa3 commit e362435

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/Tokenizer/Transformer/TypeAlternationTransformer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
use PhpCsFixer\Tokenizer\Tokens;
1919

2020
/**
21-
* Transform `|` operator into CT::T_TYPE_ALTERNATION in `} catch (ExceptionType1 | ExceptionType2 $e) {`.
21+
* Transform `|` operator into CT::T_TYPE_ALTERNATION in `function foo(Type1 | Type2 $x) {`
22+
* or `} catch (ExceptionType1 | ExceptionType2 $e) {`.
2223
*
2324
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
2425
*
@@ -31,7 +32,7 @@ final class TypeAlternationTransformer extends AbstractTransformer
3132
*/
3233
public function getPriority()
3334
{
34-
// needs to run after TypeColonTransformer
35+
// needs to run after ArrayTypehintTransformer and TypeColonTransformer
3536
return -15;
3637
}
3738

@@ -54,7 +55,7 @@ public function process(Tokens $tokens, Token $token, $index)
5455

5556
$prevIndex = $tokens->getPrevMeaningfulToken($index);
5657

57-
if (!$tokens[$prevIndex]->isGivenKind(T_STRING)) {
58+
if (!$tokens[$prevIndex]->isGivenKind([T_STRING, CT::T_ARRAY_TYPEHINT])) {
5859
return;
5960
}
6061

tests/Fixer/ClassNotation/VisibilityRequiredFixerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,5 +849,18 @@ public function provideFix80Cases()
849849
yield [
850850
'<?php class Foo { private int | /* or empty */ null $foo; }',
851851
];
852+
853+
yield [
854+
'<?php class Foo { private array|null $foo; }',
855+
];
856+
857+
yield [
858+
'<?php class Foo { private null|array $foo; }',
859+
];
860+
861+
yield [
862+
'<?php class Foo { public static null|array $foo; }',
863+
'<?php class Foo { static null|array $foo; }',
864+
];
852865
}
853866
}

tests/Tokenizer/Transformer/TypeAlternationTransformerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,29 @@ class Foo {
226226
35 => CT::T_TYPE_ALTERNATION,
227227
],
228228
];
229+
230+
yield 'array as first element of types' => [
231+
'<?php function foo(array|bool|null $foo) {}',
232+
[
233+
6 => CT::T_TYPE_ALTERNATION,
234+
8 => CT::T_TYPE_ALTERNATION,
235+
],
236+
];
237+
238+
yield 'array as middle element of types' => [
239+
'<?php function foo(null|array|bool $foo) {}',
240+
[
241+
6 => CT::T_TYPE_ALTERNATION,
242+
8 => CT::T_TYPE_ALTERNATION,
243+
],
244+
];
245+
246+
yield 'array as last element of types' => [
247+
'<?php function foo(null|bool|array $foo) {}',
248+
[
249+
6 => CT::T_TYPE_ALTERNATION,
250+
8 => CT::T_TYPE_ALTERNATION,
251+
],
252+
];
229253
}
230254
}

0 commit comments

Comments
 (0)