Skip to content

Commit d9c3346

Browse files
authored
Merge pull request #383 from simPod/slevomat-catch-exceptions-order
feat(rules): enforce catch exception order
2 parents 5a40402 + 3770eb2 commit d9c3346

8 files changed

Lines changed: 55 additions & 25 deletions

File tree

lib/Doctrine/ruleset.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@
313313
<rule ref="SlevomatCodingStandard.ControlStructures.UselessIfConditionWithReturn"/>
314314
<!-- Forbid usage of boolean-only ternary operator usage (e.g. $foo ? true : false) -->
315315
<rule ref="SlevomatCodingStandard.ControlStructures.UselessTernaryOperator"/>
316+
<!-- Require catch exception types to be sorted alphabetically -->
317+
<rule ref="SlevomatCodingStandard.Exceptions.CatchExceptionsOrder"/>
316318
<!-- Forbid useless unreachable catch blocks -->
317319
<rule ref="SlevomatCodingStandard.Exceptions.DeadCatch"/>
318320
<!-- Require using Throwable instead of Exception -->

tests/expected_report.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tests/input/duplicate-assignment-variable.php 1 0
2121
tests/input/EarlyReturn.php 8 0
2222
tests/input/example-class.php 49 0
2323
tests/input/ExampleBackedEnum.php 5 0
24-
tests/input/Exceptions.php 1 0
24+
tests/input/Exceptions.php 2 0
2525
tests/input/forbidden-comments.php 14 0
2626
tests/input/forbidden-functions.php 6 0
2727
tests/input/inline_type_hint_assertions.php 10 0
@@ -56,9 +56,9 @@ tests/input/use-ordering.php 2 0
5656
tests/input/useless-semicolon.php 2 0
5757
tests/input/UselessConditions.php 21 0
5858
----------------------------------------------------------------------
59-
A TOTAL OF 486 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
59+
A TOTAL OF 487 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
6060
----------------------------------------------------------------------
61-
PHPCBF CAN FIX 400 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
61+
PHPCBF CAN FIX 401 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6262
----------------------------------------------------------------------
6363

6464

tests/fixed/Exceptions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
namespace Exceptions;
66

77
use Exception;
8+
use LogicException;
9+
use RuntimeException;
810
use Throwable;
911

1012
try {
1113
throw new Exception();
1214
} catch (Throwable) {
1315
}
16+
17+
try {
18+
throw new LogicException();
19+
} catch (LogicException | RuntimeException) {
20+
}

tests/input/Exceptions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
namespace Exceptions;
66

77
use Exception;
8+
use LogicException;
9+
use RuntimeException;
810
use Throwable;
911

1012
try {
1113
throw new Exception();
1214
} catch (Throwable $throwable) {
1315
}
16+
17+
try {
18+
throw new LogicException();
19+
} catch (RuntimeException | LogicException) {
20+
}

tests/php74-compatibility.patch

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ index 8547171..f01ece0 100644
1515
-tests/input/EarlyReturn.php 8 0
1616
-tests/input/example-class.php 49 0
1717
-tests/input/ExampleBackedEnum.php 5 0
18-
-tests/input/Exceptions.php 1 0
18+
-tests/input/Exceptions.php 2 0
1919
+tests/input/EarlyReturn.php 7 0
2020
+tests/input/example-class.php 44 0
2121
tests/input/forbidden-comments.php 14 0
@@ -57,10 +57,10 @@ index 8547171..f01ece0 100644
5757
-tests/input/UselessConditions.php 21 0
5858
+tests/input/UselessConditions.php 20 0
5959
----------------------------------------------------------------------
60-
-A TOTAL OF 486 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
60+
-A TOTAL OF 487 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
6161
+A TOTAL OF 435 ERRORS AND 2 WARNINGS WERE FOUND IN 48 FILES
6262
----------------------------------------------------------------------
63-
-PHPCBF CAN FIX 400 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
63+
-PHPCBF CAN FIX 401 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6464
+PHPCBF CAN FIX 350 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6565
----------------------------------------------------------------------
6666
diff --git a/tests/fixed/ClassKeywordOrder.php b/tests/fixed/ClassKeywordOrder.php
@@ -142,21 +142,28 @@ index fd701eb..fe54eb9 100644
142142
- case BAM = 1234;
143143
-}
144144
diff --git a/tests/fixed/Exceptions.php b/tests/fixed/Exceptions.php
145-
index db7408b..9b146c6 100644
145+
index eba1df2..9b146c6 100644
146146
--- a/tests/fixed/Exceptions.php
147147
+++ b/tests/fixed/Exceptions.php
148-
@@ -3,11 +3,3 @@
148+
@@ -3,18 +3,3 @@
149149
declare(strict_types=1);
150150

151151
namespace Exceptions;
152152
-
153153
-use Exception;
154+
-use LogicException;
155+
-use RuntimeException;
154156
-use Throwable;
155157
-
156158
-try {
157159
- throw new Exception();
158160
-} catch (Throwable) {
159161
-}
162+
-
163+
-try {
164+
- throw new LogicException();
165+
-} catch (LogicException | RuntimeException) {
166+
-}
160167
diff --git a/tests/fixed/NamingCamelCase.php b/tests/fixed/NamingCamelCase.php
161168
index 5493471..57d9f2b 100644
162169
--- a/tests/fixed/NamingCamelCase.php
@@ -466,21 +473,28 @@ index 3b09f47..fe54eb9 100644
466473
- case BAM = 1234;
467474
-}
468475
diff --git a/tests/input/Exceptions.php b/tests/input/Exceptions.php
469-
index 3aaa30f..9b146c6 100644
476+
index 048b7fb..9b146c6 100644
470477
--- a/tests/input/Exceptions.php
471478
+++ b/tests/input/Exceptions.php
472-
@@ -3,11 +3,3 @@
479+
@@ -3,18 +3,3 @@
473480
declare(strict_types=1);
474481

475482
namespace Exceptions;
476483
-
477484
-use Exception;
485+
-use LogicException;
486+
-use RuntimeException;
478487
-use Throwable;
479488
-
480489
-try {
481490
- throw new Exception();
482491
-} catch (Throwable $throwable) {
483492
-}
493+
-
494+
-try {
495+
- throw new LogicException();
496+
-} catch (RuntimeException | LogicException) {
497+
-}
484498
diff --git a/tests/input/PropertyDeclaration.php b/tests/input/PropertyDeclaration.php
485499
index acdc445..0891e12 100644
486500
--- a/tests/input/PropertyDeclaration.php

tests/php80-compatibility.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ index 9b19b9e..760ee4a 100644
1616
-tests/input/example-class.php 49 0
1717
-tests/input/ExampleBackedEnum.php 5 0
1818
+tests/input/example-class.php 48 0
19-
tests/input/Exceptions.php 1 0
19+
tests/input/Exceptions.php 2 0
2020
tests/input/forbidden-comments.php 14 0
2121
tests/input/forbidden-functions.php 6 0
2222
@@ -34,8 +33,8 @@ tests/input/null_coalesce_equal_operator.php 5 0
@@ -33,11 +33,11 @@ index 9b19b9e..760ee4a 100644
3333
tests/input/useless-semicolon.php 2 0
3434
tests/input/UselessConditions.php 21 0
3535
----------------------------------------------------------------------
36-
-A TOTAL OF 486 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
37-
+A TOTAL OF 468 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
36+
-A TOTAL OF 487 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
37+
+A TOTAL OF 469 ERRORS AND 2 WARNINGS WERE FOUND IN 50 FILES
3838
----------------------------------------------------------------------
39-
-PHPCBF CAN FIX 400 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
40-
+PHPCBF CAN FIX 383 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
39+
-PHPCBF CAN FIX 401 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
40+
+PHPCBF CAN FIX 384 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
4141
----------------------------------------------------------------------
4242
diff --git a/tests/fixed/ClassKeywordOrder.php b/tests/fixed/ClassKeywordOrder.php
4343
index 29f6164..4b28352 100644

tests/php81-compatibility.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ index 8547171..a7a42fa 100644
1616
-tests/input/example-class.php 49 0
1717
+tests/input/example-class.php 48 0
1818
tests/input/ExampleBackedEnum.php 5 0
19-
tests/input/Exceptions.php 1 0
19+
tests/input/Exceptions.php 2 0
2020
tests/input/forbidden-comments.php 14 0
2121
@@ -55,7 +54,7 @@ tests/input/use-ordering.php 1 0
2222
tests/input/useless-semicolon.php 2 0
2323
tests/input/UselessConditions.php 21 0
2424
----------------------------------------------------------------------
25-
-A TOTAL OF 486 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
26-
+A TOTAL OF 478 ERRORS AND 2 WARNINGS WERE FOUND IN 51 FILES
25+
-A TOTAL OF 487 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
26+
+A TOTAL OF 479 ERRORS AND 2 WARNINGS WERE FOUND IN 51 FILES
2727
----------------------------------------------------------------------
28-
-PHPCBF CAN FIX 400 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
29-
+PHPCBF CAN FIX 393 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
28+
-PHPCBF CAN FIX 401 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
29+
+PHPCBF CAN FIX 394 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
3030
----------------------------------------------------------------------
3131
diff --git a/tests/fixed/ClassKeywordOrder.php b/tests/fixed/ClassKeywordOrder.php
3232
index 29f6164..4b28352 100644

tests/php82-compatibility.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ index 8547171..a7a42fa 100644
1616
-tests/input/example-class.php 49 0
1717
+tests/input/example-class.php 48 0
1818
tests/input/ExampleBackedEnum.php 5 0
19-
tests/input/Exceptions.php 1 0
19+
tests/input/Exceptions.php 2 0
2020
tests/input/forbidden-comments.php 14 0
2121
@@ -55,7 +55,7 @@ tests/input/use-ordering.php 2 0
2222
tests/input/useless-semicolon.php 2 0
2323
tests/input/UselessConditions.php 21 0
2424
----------------------------------------------------------------------
25-
-A TOTAL OF 486 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
26-
+A TOTAL OF 479 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
25+
-A TOTAL OF 487 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
26+
+A TOTAL OF 480 ERRORS AND 2 WARNINGS WERE FOUND IN 52 FILES
2727
----------------------------------------------------------------------
28-
-PHPCBF CAN FIX 400 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
29-
+PHPCBF CAN FIX 394 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
28+
-PHPCBF CAN FIX 401 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
29+
+PHPCBF CAN FIX 395 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
3030
----------------------------------------------------------------------
3131
diff --git a/tests/fixed/constants-var.php b/tests/fixed/constants-var.php
3232
index f10c235..d4268cb 100644

0 commit comments

Comments
 (0)