Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3513,7 +3513,35 @@ public function processArgs(
$deferredInvalidateExpressions = [];
/** @var ProcessClosureResult[] $deferredByRefClosureResults */
$deferredByRefClosureResults = [];
foreach ($args as $i => $arg) {

$processingOrder = array_keys($args);
$hasReorderedArgs = false;
foreach ($args as $arg) {
if ($arg->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE) !== null) {
$hasReorderedArgs = true;
break;
}
}
if ($hasReorderedArgs) {
usort($processingOrder, static function (int $a, int $b) use ($args): int {
$aOriginal = $args[$a]->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE);
$bOriginal = $args[$b]->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE);
if ($aOriginal === null && $bOriginal === null) {
return $a <=> $b;
}
if ($aOriginal === null) {
return 1;
}
if ($bOriginal === null) {
return -1;
}

return $aOriginal->getStartTokenPos() <=> $bOriginal->getStartTokenPos();
});
}

foreach ($processingOrder as $i) {
$arg = $args[$i];
$assignByReference = false;
$parameter = null;
$parameterType = null;
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9392.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php // lint >= 8.0

namespace Bug9392;

use function PHPStan\Testing\assertType;

class Range
{
public function __construct(
public ?string $notInRangeMessage = null,
public mixed $min = null,
public mixed $max = null,
) {
}
}

function () {
new Range(
min: $min = 20 * 100,
max: $max = 5_000 * 100,
notInRangeMessage: sprintf('The price must be between %s and %s.', round($min / 100, 2), round($max / 100, 2)),
);

assertType('2000', $min);
assertType('500000', $max);
};
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1626,4 +1626,14 @@ public function testBug6833(): void
]);
}

#[RequiresPhp('>= 8.0.0')]
public function testBug9392(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-9392.php'], []);
}

}
78 changes: 78 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-9392.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php // lint >= 8.0

namespace Bug9392;

class Range
{
public function __construct(
public ?string $notInRangeMessage = null,
public mixed $min = null,
public mixed $max = null,
) {
}
}

new Range(
min: $min = 20 * 100,
max: $max = 5_000 * 100,
notInRangeMessage: sprintf('The price must be between %s and %s.', round($min / 100, 2), round($max / 100, 2)),
);

function foo(?string $c = null, mixed $a = null, mixed $b = null): void
{
}

foo(
a: $a = 10,
b: $b = 20,
c: sprintf('%s %s', $a, $b),
);

class Foo
{
public function bar(?string $c = null, mixed $a = null, mixed $b = null): void
{
}

public static function baz(?string $c = null, mixed $a = null, mixed $b = null): void
{
}
}

$foo = new Foo();

$foo->bar(
a: $x = 10,
b: $y = 20,
c: sprintf('%s %s', $x, $y),
);

Foo::baz(
a: $p = 10,
b: $q = 20,
c: sprintf('%s %s', $p, $q),
);

// Mixed positional and named args
function mixed_args(int $first, ?string $c = null, mixed $a = null, mixed $b = null): void
{
}

mixed_args(
1,
a: $m1 = 10,
b: $m2 = 20,
c: sprintf('%s %s', $m1, $m2),
);

// Variable assigned in named arg used after the call
function after_call(?string $c = null, mixed $a = null): void
{
}

after_call(
a: $afterVar = 42,
c: (string) $afterVar,
);

echo $afterVar;
Loading