Skip to content

Commit

Permalink
bug #6265 NullableTypeDeclarationForDefaultNullValueFixer - handle "r…
Browse files Browse the repository at this point in the history
…eadonly" a… (SpacePossum)

This PR was merged into the master branch.

Discussion
----------

NullableTypeDeclarationForDefaultNullValueFixer - handle "readonly" a…

…s constructor property modifier

closes #6239

Commits
-------

a07f453 NullableTypeDeclarationForDefaultNullValueFixer - handle "readonly" as constructor property modifier
  • Loading branch information
SpacePossum committed Feb 5, 2022
2 parents ed3f63e + a07f453 commit 3a17d14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Expand Up @@ -121,6 +121,16 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
*/
private function fixFunctionParameters(Tokens $tokens, array $arguments): void
{
$constructorPropertyModifiers = [
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
];

if (\defined('T_READONLY')) { // @TODO: drop condition when PHP 8.1+ is required
$constructorPropertyModifiers[] = T_READONLY;
}

foreach (array_reverse($arguments) as $argumentInfo) {
if (
// Skip, if the parameter
Expand All @@ -136,17 +146,10 @@ private function fixFunctionParameters(Tokens $tokens, array $arguments): void

$argumentTypeInfo = $argumentInfo->getTypeAnalysis();

if (
\PHP_VERSION_ID >= 80000
&& false === $this->configuration['use_nullable_type_declaration']
) {
if (\PHP_VERSION_ID >= 80000 && false === $this->configuration['use_nullable_type_declaration']) {
$visibility = $tokens[$tokens->getPrevMeaningfulToken($argumentTypeInfo->getStartIndex())];

if ($visibility->isGivenKind([
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
])) {
if ($visibility->isGivenKind($constructorPropertyModifiers)) {
continue;
}
}
Expand Down
Expand Up @@ -505,8 +505,12 @@ function foo2(?string &/*comment*/$param2 = null) {}
* @dataProvider provideFix81Cases
* @requires PHP 8.1
*/
public function testFix81(string $expected): void
public function testFix81(string $expected, ?array $config): void
{
if (null !== $config) {
$this->fixer->configure($config);
}

$this->doTest($expected);
}

Expand All @@ -521,6 +525,19 @@ public function __construct(
) {}
}
',
null,
];

yield [
'<?php
class Foo {
public function __construct(
public readonly ?string $readonlyString = null,
readonly public ?int $readonlyInt = null,
) {}
}',
['use_nullable_type_declaration' => false],
];
}
}

0 comments on commit 3a17d14

Please sign in to comment.