@@ -297,5 +297,104 @@ class TestAlternativeControlStructures {
297
297
$ var_after_class_in_global_space = 1 ;
298
298
do_something_else ();
299
299
300
+ // These are parse errors, but that's not the concern of the sniff.
301
+ function parseError1 () {
302
+ defined ('FOO ' ) or return 'foo ' ;
303
+ echo 'unreachable ' ;
304
+ }
305
+
306
+ function parseError2 () {
307
+ defined ('FOO ' ) || continue;
308
+ echo 'unreachable ' ;
309
+ }
310
+
311
+ // All logical operators are allowed with inline expressions (but this was not correctly handled by the sniff).
312
+ function exitExpressionsWithLogicalOperators () {
313
+ $ condition = false ;
314
+ $ condition || exit ();
315
+ $ condition or die ();
316
+
317
+ $ condition = true ;
318
+ $ condition && die ();
319
+ $ condition and exit;
320
+
321
+ $ condition xor die ();
322
+
323
+ echo 'still executable as exit, in all of the above cases, is used as part of an expression ' ;
324
+ }
325
+
326
+ // Inline expressions are allowed in ternaries.
327
+ function exitExpressionsInTernary () {
328
+ $ value = $ myValue ? $ myValue : exit ();
329
+ $ value = $ myValue ?: exit ();
330
+ $ value = $ var == 'foo ' ? 'bar ' : die ( 'world ' );
331
+
332
+ $ value = (!$ myValue ) ? exit () : $ myValue ;
333
+ $ value = $ var != 'foo ' ? die ( 'world ' ) : 'bar ' ;
334
+
335
+ echo 'still executable ' ;
336
+ }
337
+
338
+ // Inline expressions are allowed with null coalesce and null coalesce equals.
339
+ function exitExpressionsWithNullCoalesce () {
340
+ $ value = $ nullableValue ?? exit ();
341
+ $ value ??= die ();
342
+ echo 'still executable ' ;
343
+ }
344
+
345
+ // Inline expressions are allowed in arrow functions.
346
+ function exitExpressionsInArrowFunction () {
347
+ $ callable = fn () => die ();
348
+ echo 'still executable ' ;
349
+ }
350
+
351
+ // PHP 8.0+: throw expressions which don't stop execution.
352
+ function nonStoppingThrowExpressions () {
353
+ $ callable = fn () => throw new Exception ();
354
+
355
+ $ value = $ myValue ? 'something ' : throw new Exception ();
356
+ $ value = $ myValue ?: throw new Exception ();
357
+ $ value = $ myValue ? throw new Exception () : 'something ' ;
358
+
359
+ $ value = $ nullableValue ?? throw new Exception ();
360
+ $ value ??= throw new Exception ();
361
+
362
+ $ condition && throw new Exception ();
363
+ $ condition || throw new Exception ();
364
+ $ condition and throw new Exception ();
365
+ $ condition or throw new Exception ();
366
+
367
+ echo 'still executable as throw, in all of the above cases, is used as part of an expression ' ;
368
+
369
+ throw new Exception ();
370
+ echo 'non-executable ' ;
371
+ }
372
+
373
+ // PHP 8.0+: throw expressions which do stop execution.
374
+ function executionStoppingThrowExpressionsA () {
375
+ $ condition xor throw new Exception ();
376
+ echo 'non-executable ' ;
377
+ }
378
+
379
+ function executionStoppingThrowExpressionsB () {
380
+ throw $ userIsAuthorized ? new ForbiddenException () : new UnauthorizedException ();
381
+ echo 'non-executable ' ;
382
+ }
383
+
384
+ function executionStoppingThrowExpressionsC () {
385
+ throw $ condition1 && $ condition2 ? new Exception1 () : new Exception2 ();
386
+ echo 'non-executable ' ;
387
+ }
388
+
389
+ function executionStoppingThrowExpressionsD () {
390
+ throw $ exception ??= new Exception ();
391
+ echo 'non-executable ' ;
392
+ }
393
+
394
+ function executionStoppingThrowExpressionsE () {
395
+ throw $ maybeNullException ?? new Exception ();
396
+ echo 'non-executable ' ;
397
+ }
398
+
300
399
// Intentional syntax error.
301
400
return array_map(
0 commit comments