Skip to content

Commit

Permalink
bug #5665 NullableTypeDeclarationForDefaultNullValueFixer - fix for n…
Browse files Browse the repository at this point in the history
…ullable with attribute (kubawerlos)

This PR was squashed before being merged into the 2.18 branch.

Discussion
----------

NullableTypeDeclarationForDefaultNullValueFixer - fix for nullable with attribute

Fixes #5533

Commits
-------

068aedb NullableTypeDeclarationForDefaultNullValueFixer - fix for nullable with attribute
  • Loading branch information
keradus committed Apr 30, 2021
2 parents 0869890 + 068aedb commit f2e555a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Tokenizer/Analyzer/ArgumentsAnalyzer.php
Expand Up @@ -134,6 +134,12 @@ public function getArgumentInfo(Tokens $tokens, $argumentStart, $argumentEnd)
continue;
}

if (\defined('T_ATTRIBUTE') && $token->isGivenKind(T_ATTRIBUTE)) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ATTRIBUTE, $index);

continue;
}

if ($sawName) {
$info['default'] .= $token->getContent();
} else {
Expand Down
Expand Up @@ -470,5 +470,23 @@ public function aaa(int | string $bar = null, $baz = 1) {}
public function bbb(int | null $bar = null, $baz = 1) {}
}',
];

yield 'attribute' => [
'<?php function foo(#[AnAttribute] string $param = null) {}',
'<?php function foo(#[AnAttribute] ?string $param = null) {}',
];

yield 'attributes' => [
'<?php function foo(
#[AnAttribute] string $a = null,
#[AnAttribute] string $b = null,
#[AnAttribute] string $c = null
) {}',
'<?php function foo(
#[AnAttribute] ?string $a = null,
#[AnAttribute] ?string $b = null,
#[AnAttribute] ?string $c = null
) {}',
];
}
}
16 changes: 16 additions & 0 deletions tests/Tokenizer/Analyzer/ArgumentsAnalyzerTest.php
Expand Up @@ -184,6 +184,22 @@ public function provideArgumentsInfoCases()

public function provideArgumentsInfo80Cases()
{
yield [
'<?php function foo(#[AnAttribute] ?string $param = null) {}',
5,
16,
new ArgumentAnalysis(
'$param',
12,
'null',
new TypeAnalysis(
'?string',
9,
10
)
),
];

foreach (['public', 'protected', 'private'] as $visibility) {
yield [
sprintf('<?php class Foo { public function __construct(%s ?string $param = null) {} }', $visibility),
Expand Down

0 comments on commit f2e555a

Please sign in to comment.