Skip to content

Commit

Permalink
Resolve PHPStan issue with deprecated parameter order in PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sayuprc committed Mar 10, 2024
1 parent 952b313 commit 6b1d358
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function deprecatesRequiredParameterAfterOptional(): bool
return $this->versionId >= 80000;
}

public function deprecatesRequiredParameterAfterOptionalNullableAndDefaultNull(): bool
{
return $this->versionId >= 80100;
}

public function supportsLessOverridenParametersWithVariadic(): bool
{
return $this->versionId >= 80000;
Expand Down
7 changes: 7 additions & 0 deletions src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
Expand Down Expand Up @@ -494,6 +495,12 @@ private function checkRequiredParameterAfterOptional(array $parameterNodes): arr

$constantName = $defaultValue->name->toLowerString();
if ($constantName === 'null') {
if ($this->phpVersion->deprecatesRequiredParameterAfterOptionalNullableAndDefaultNull()) {
if ($parameterNode->type instanceof NullableType) {
$optionalParameter = $parameterName;
}
}

continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,35 @@ public function dataRequiredParameterAfterOptional(): array
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
11,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
13,
],
],
],
[
80100,
[
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
5,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
9,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
11,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
13,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
15,
],
],
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,35 @@ public function dataRequiredParameterAfterOptional(): array
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
17,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
21,
],
],
],
[
80100,
[
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
5,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
13,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
17,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
21,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
25,
],
],
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,35 @@ public function dataRequiredParameterAfterOptional(): array
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
18,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
26,
],
],
],
[
80100,
[
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
5,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
14,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
18,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
26,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
30,
],
],
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
fn (int $foo = 1, $bar): int => 1; // not OK

fn (bool $foo = true, $bar): int => 1; // not OK

fn (?int $foo = 1, $bar): int => 1; // not OK

fn (?int $foo = null, $bar): int => 1; // not OK
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ function (int $foo = 1, $bar): void // not OK
function(bool $foo = true, $bar): void // not OK
{
};

function (?int $foo = 1, $bar): void // not OK
{
};

function (?int $foo = null, $bar): void // not OK
{
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ function doLorem(bool $foo = true, $bar): void // not OK
function doIpsum(bool $foo = true, ...$bar): void // OK
{
}

function doDolor(?int $foo = 1, $bar): void // not OK
{
}

function doSit(?int $foo = null, $bar): void // not OK
{
}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Methods/ExistingClassesInTypehintsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,35 @@ public function dataRequiredParameterAfterOptional(): array
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
21,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
25,
],
],
],
[
80100,
[
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
8,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
17,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
21,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
25,
],
[
'Deprecated in PHP 8.0: Required parameter $bar follows optional parameter $foo.',
29,
],
],
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ public function doLorem(bool $foo = true, $bar): void // not OK
{
}

public function doDolor(?int $foo = 1, $bar): void // not OK
{
}

public function doSit(?int $foo = null, $bar): void // not OK
{
}
}

0 comments on commit 6b1d358

Please sign in to comment.