Skip to content

Commit b53bb1b

Browse files
committed
Test placeholder misuse in instantiations, nullsafe calls and by-ref parameters
Amp-Thread-ID: https://ampcode.com/threads/T-01a1c541-317f-7386-a2ed-9cbd66178685
1 parent 07f6786 commit b53bb1b

8 files changed

Lines changed: 60 additions & 2 deletions

‎Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ lint:
6161
--exclude tests/PHPStan/Rules/Properties/data/intersection-types.php \
6262
--exclude tests/PHPStan/Rules/Classes/data/first-class-instantiation-callable.php \
6363
--exclude tests/PHPStan/Rules/Classes/data/instantiation-callable.php \
64+
--exclude tests/PHPStan/Rules/Classes/data/instantiation-partial-function-application.php \
6465
--exclude tests/PHPStan/Rules/Classes/data/bug-9402.php \
6566
--exclude tests/PHPStan/Rules/Constants/data/class-as-class-constant.php \
6667
--exclude tests/PHPStan/Rules/Constants/data/value-assigned-to-class-constant-native-type.php \

‎build/collision-detector.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"../tests/PHPStan/Rules/Functions/data/partial-function-application-arguments.php",
3636
"../tests/PHPStan/Rules/Methods/data/partial-function-application-method-arguments.php",
3737
"../tests/PHPStan/Rules/Functions/data/partial-function-application-callbacks.php",
38-
"../tests/PHPStan/Analyser/nsrt/partial-function-application-replay.php"
38+
"../tests/PHPStan/Analyser/nsrt/partial-function-application-replay.php",
39+
"../tests/PHPStan/Rules/Classes/data/instantiation-partial-function-application.php"
3940
]
4041
}

‎tests/PHPStan/Rules/Classes/InstantiationCallableRuleTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPStan\Rules\Rule;
66
use PHPStan\Testing\RuleTestCase;
7+
use PHPUnit\Framework\Attributes\RequiresPhp;
78

89
/**
910
* @extends RuleTestCase<InstantiationCallableRule>
@@ -26,4 +27,19 @@ public function testRule(): void
2627
]);
2728
}
2829

30+
#[RequiresPhp('>= 8.6.0-dev')]
31+
public function testPartialFunctionApplication(): void
32+
{
33+
$this->analyse([__DIR__ . '/data/instantiation-partial-function-application.php'], [
34+
[
35+
'Cannot create callable from the new operator.',
36+
14,
37+
],
38+
[
39+
'Cannot create callable from the new operator.',
40+
15,
41+
],
42+
]);
43+
}
44+
2945
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php // lint >= 8.6
2+
3+
namespace InstantiationPartialFunctionApplication;
4+
5+
class Foo
6+
{
7+
8+
public function __construct(int $a, string $b)
9+
{
10+
}
11+
12+
public function doFoo(): void
13+
{
14+
$f = new self(?, ?);
15+
$g = new self(1, ...);
16+
}
17+
18+
}

‎tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ public function testPartialFunctionApplicationClosureCalls(): void
569569
'Unknown parameter $x in call to closure.',
570570
93,
571571
],
572+
[
573+
'Parameter #1 $a of closure is passed by reference, so it expects variables only.',
574+
105,
575+
],
572576
]);
573577
}
574578

‎tests/PHPStan/Rules/Functions/data/partial-function-application-closure-calls.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,16 @@ function magicMethods(Magic $magic): void
9393
$magic->magic(?, 1)(x: 'a');
9494
$magic->magic(?, ?)(arguments0: 'a', arguments1: 1);
9595
}
96+
97+
function byRef(int &$a, string $b): void
98+
{
99+
}
100+
101+
function byRefPlaceholder(): void
102+
{
103+
// the closure keeps the by-ref parameter its placeholder stands for
104+
$f = byRef(?, ?);
105+
$f(1, 'b');
106+
$i = 1;
107+
$f($i, 'b');
108+
}

‎tests/PHPStan/Rules/Methods/MethodCallableRuleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ public function testPartialFunctionApplicationArguments(): void
148148
'Variadic placeholder may only appear once.',
149149
78,
150150
],
151+
[
152+
'Cannot combine nullsafe operator with Closure creation.',
153+
80,
154+
],
151155
]);
152156
}
153157

‎tests/PHPStan/Rules/Methods/data/partial-function-application-method-arguments.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ function magicMethods(Magic $magic): void
7373
Magic::staticMagic(?, y: ?);
7474
}
7575

76-
function variadicPlaceholderPlacements(Foo $foo): void
76+
function variadicPlaceholderPlacements(Foo $foo, ?Foo $nullableFoo): void
7777
{
7878
$foo->doFoo(?, ..., ...);
7979
Foo::doStatic(?, ..., ...);
80+
$nullableFoo?->doFoo(1, ?);
8081
}

0 commit comments

Comments
 (0)