Skip to content

Commit 7de329c

Browse files
committed
ForbidCheckedExceptionInCallableRuleTest: Split test methods into individual test cases.
To support PHPStan's enhanced dead code detection in closures (phpstan/phpstan-src#4148).
1 parent 9a2a033 commit 7de329c

File tree

1 file changed

+127
-7
lines changed
  • tests/Rule/data/ForbidCheckedExceptionInCallableRule

1 file changed

+127
-7
lines changed

tests/Rule/data/ForbidCheckedExceptionInCallableRule/code.php

Lines changed: 127 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,23 @@ public function allowThrowInBaseClass(callable $callable): void
4141

4242
class FirstClassCallableTest extends BaseCallableTest {
4343

44-
public function testDeclarations(): void
44+
public function testDeclarations1(): void
4545
{
4646
$this->noop(...);
47+
}
4748

49+
public function testDeclarations2(): void
50+
{
4851
$this->throws(...); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
52+
}
4953

54+
public function testDeclarations3(): void
55+
{
5056
// $this?->throws(...); // https://github.com/phpstan/phpstan/issues/9746
57+
}
5158

59+
public function testDeclarations4(): void
60+
{
5261
throwing_function(...); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
5362
}
5463

@@ -62,27 +71,51 @@ public function testExplicitExecution2(): void
6271
(throwing_function(...))();
6372
}
6473

65-
public function testPassedCallbacks(): void
74+
public function testPassedCallbacksA1(): void
6675
{
6776
$this->immediateThrow(null, $this->throws(...));
77+
}
6878

79+
public function testPassedCallbacksA2(): void
80+
{
6981
array_map($this->throws(...), []);
82+
}
7083

84+
public function testPassedCallbacksA3(): void
85+
{
7186
array_map(throwing_function(...), []);
87+
}
7288

89+
public function testPassedCallbacksA4(): void
90+
{
7391
$this->allowThrow(42, $this->throws(...));
92+
}
7493

94+
public function testPassedCallbacksA5(): void
95+
{
7596
$this->allowThrow(42, throwing_function(...));
97+
}
7698

99+
public function testPassedCallbacksA6(): void
100+
{
77101
$this->immediateThrow(
78102
$this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
79103
function () {},
80104
);
105+
}
81106

107+
public function testPassedCallbacksA7(): void
108+
{
82109
$this->allowThrowInBaseClass(throwing_function(...));
110+
}
83111

112+
public function testPassedCallbacksA8(): void
113+
{
84114
$this->allowThrowInInterface(throwing_function(...));
115+
}
85116

117+
public function testPassedCallbacksA9(): void
118+
{
86119
$this->denied($this->throws(...)); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
87120
}
88121

@@ -124,20 +157,29 @@ public function allowThrow(int $dummy, callable $callable): void
124157

125158
class ClosureTest extends BaseCallableTest {
126159

127-
public function testDeclarations(): void
160+
public function testDeclarationsB1(): void
128161
{
129162
$fn = function () {
130163
throw new CheckedException(); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in closure!
131164
};
165+
}
132166

167+
public function testDeclarationsB2(): void
168+
{
133169
$fn2 = function () {
134170
$this->throws(); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in closure!
135171
};
172+
}
136173

174+
public function testDeclarationsB3(): void
175+
{
137176
$fn3 = function () {
138177
$this->noop(); // implicit throw is ignored
139178
};
179+
}
140180

181+
public function testDeclarationsB4(): void
182+
{
141183
$fn4 = function (callable $c) {
142184
$c(); // implicit throw is ignored (https://github.com/phpstan/phpstan/issues/9779)
143185
};
@@ -150,58 +192,91 @@ public function testExplicitExecution(): void
150192
})();
151193
}
152194

153-
public function testPassedCallbacks(): void
195+
public function testPassedCallbacks1(): void
154196
{
155197
$this->immediateThrow(function () {
156198
throw new CheckedException();
157199
});
200+
}
158201

202+
public function testPassedCallbacks2(): void
203+
{
159204
$self = $this; // self is unknown variable in scope of the closure
160205
$self->immediateThrow(function () {
161206
throw new CheckedException();
162207
});
208+
}
163209

210+
public function testPassedCallbacks3(): void
211+
{
164212
array_map(function () {
165213
throw new CheckedException();
166214
}, []);
215+
}
167216

217+
public function testPassedCallbacks4(): void
218+
{
168219
array_map(function () {
169220
$this->throws();
170221
}, []);
222+
}
171223

224+
public function testPassedCallbacks5(): void
225+
{
172226
$this->allowThrow(function () {
173227
$this->throws();
174228
});
229+
}
175230

231+
public function testPassedCallbacks6(): void
232+
{
176233
$this->immediateThrow(
177234
function () {},
178235
function () {
179236
throw new CheckedException(); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in closure!
180237
},
181238
);
239+
}
182240

241+
public function testPassedCallbacks7(): void
242+
{
183243
$this->immediateThrow(
184244
denied: function () {},
185245
);
246+
}
186247

248+
public function testPassedCallbacks8(): void
249+
{
187250
$this->immediateThrow(
188251
denied: function () {
189252
throw new CheckedException(); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in closure!
190253
},
191254
);
255+
}
192256

257+
public function testPassedCallbacks9(): void
258+
{
193259
$this->allowThrowInBaseClass(function () {
194260
$this->throws();
195261
});
262+
}
196263

264+
public function testPassedCallbacks10(): void
265+
{
197266
$this->allowThrowInInterface(function () {
198267
$this->throws();
199268
});
269+
}
200270

271+
public function testPassedCallbacks11(): void
272+
{
201273
$this->denied(function () {
202274
throw new CheckedException(); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in closure!
203275
});
276+
}
204277

278+
public function testPassedCallbacks12(): void
279+
{
205280
$this?->denied(function () {
206281
$this->throws(); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in closure!
207282
});
@@ -250,14 +325,23 @@ public function __construct($callable)
250325
new self(fn () => throw new CheckedException());
251326
}
252327

253-
public function testDeclarations(): void
328+
public function testDeclarationsC1(): void
254329
{
255330
$fn = fn () => throw new CheckedException(); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in arrow function!
331+
}
256332

333+
public function testDeclarationsC2(): void
334+
{
257335
$fn2 = fn () => $this->throws(); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in arrow function!
336+
}
258337

338+
public function testDeclarationsC3(): void
339+
{
259340
$fn3 = fn () => $this->noop(); // implicit throw is ignored
341+
}
260342

343+
public function testDeclarationsC4(): void
344+
{
261345
$fn4 = fn (callable $c) => $c(); // implicit throw is ignored (https://github.com/phpstan/phpstan/issues/9779)
262346
}
263347

@@ -266,22 +350,43 @@ public function testExplicitExecution(): void
266350
(fn () => throw new CheckedException())();
267351
}
268352

269-
public function testPassedCallbacks(): void
353+
public function testPassedCallbacksC1(): void
270354
{
271355
$this->immediateThrow(fn () => throw new CheckedException());
356+
}
272357

358+
public function testPassedCallbacksC2(): void
359+
{
273360
array_map(fn () => throw new CheckedException(), []);
361+
}
274362

363+
public function testPassedCallbacksC3(): void
364+
{
275365
array_map(fn () => $this->throws(), []);
366+
}
276367

368+
public function testPassedCallbacksC4(): void
369+
{
277370
$this->allowThrow(fn () => $this->throws());
371+
}
278372

373+
public function testPassedCallbacksC5(): void
374+
{
279375
$this->allowThrowInBaseClass(fn () => $this->throws());
376+
}
280377

378+
public function testPassedCallbacksC6(): void
379+
{
281380
$this->allowThrowInInterface(fn () => $this->throws());
381+
}
282382

383+
public function testPassedCallbacksC7(): void
384+
{
283385
$this->denied(fn () => throw new CheckedException()); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in arrow function!
386+
}
284387

388+
public function testPassedCallbacksC8(): void
389+
{
285390
$this?->denied(fn () => throw new CheckedException()); // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in arrow function!
286391
}
287392

@@ -323,40 +428,55 @@ public function allowThrow(callable $callable): void
323428

324429
class ArgumentSwappingTest {
325430

326-
public function test()
431+
public function testD1(): void
327432
{
328433
$this->call(
329434
$this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
330435
$this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
331436
$this->throws(...),
332437
$this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
333438
);
439+
}
334440

441+
public function testD2(): void
442+
{
335443
$this->call(
336444
second: $this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
337445
first: $this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
338446
third: $this->throws(...),
339447
);
448+
}
340449

450+
public function testD3(): void
451+
{
341452
$this->call(
342453
forth: $this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
343454
first: $this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
344455
);
456+
}
345457

458+
public function testD4(): void
459+
{
346460
$this->call(
347461
$this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
348462
forth: $this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
349463
second: $this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
350464
third: $this->throws(...),
351465
);
466+
}
352467

468+
public function testD5(): void
469+
{
353470
$this->call(
354471
$this->noop(...),
355472
$this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
356473
forth: $this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!
357474
third: $this->noop(...),
358475
);
476+
}
359477

478+
public function testD6(): void
479+
{
360480
// this is not yet supported, the rule do not see this as argument pass
361481
$this->call(... [
362482
'third' => $this->throws(...), // error: Throwing checked exception ForbidCheckedExceptionInCallableRule\CheckedException in first-class-callable!

0 commit comments

Comments
 (0)