File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33namespace PHPStan \Analyser \StmtHandler ;
44
5+ use Error ;
6+ use Exception ;
57use PhpParser \Node ;
68use PhpParser \Node \Expr ;
79use PhpParser \Node \Stmt ;
@@ -147,7 +149,13 @@ public function processStmt(
147149 }
148150
149151 // implicit only
150- if (count ($ matchingThrowPoints ) === 0 || $ onlyExplicitIsThrow ) {
152+ // Broad catches also cover undocumented exceptions when a documented throw matches.
153+ if (
154+ count ($ matchingThrowPoints ) === 0
155+ || $ onlyExplicitIsThrow
156+ || $ originalCatchType ->isSuperTypeOf (new ObjectType (Exception::class))->yes () // phpcs:ignore SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException
157+ || $ originalCatchType ->isSuperTypeOf (new ObjectType (Error::class))->yes ()
158+ ) {
151159 foreach ($ throwPoints as $ throwPointIndex => $ throwPoint ) {
152160 if ($ throwPoint ->isExplicit () || $ throwPoint ->getType () instanceof NeverType) {
153161 continue ;
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace BroadCatchImplicitThrows ;
4+
5+ use function PHPStan \Testing \assertType ;
6+
7+ function mayFail (): void
8+ {
9+ if (rand (0 , 1 )) {
10+ throw new \RuntimeException ();
11+ }
12+ }
13+
14+ /** @throws \InvalidArgumentException */
15+ function invalidArgument (): void
16+ {
17+ throw new \InvalidArgumentException ();
18+ }
19+
20+ /** @throws \TypeError */
21+ function typeError (): void
22+ {
23+ throw new \TypeError ();
24+ }
25+
26+ function catchException (bool $ condition ): void
27+ {
28+ try {
29+ mayFail ();
30+ if ($ condition ) {
31+ invalidArgument ();
32+ }
33+ } catch (\Exception $ e ) {
34+ assertType ('bool ' , $ condition );
35+ assertType ('Exception ' , $ e );
36+ }
37+ }
38+
39+ function catchError (bool $ condition ): void
40+ {
41+ try {
42+ mayFail ();
43+ if ($ condition ) {
44+ typeError ();
45+ }
46+ } catch (\Error $ e ) {
47+ assertType ('bool ' , $ condition );
48+ assertType ('Error ' , $ e );
49+ }
50+ }
Original file line number Diff line number Diff line change @@ -387,4 +387,11 @@ public function testArrowReferenceParameters(): void
387387 ]);
388388 }
389389
390+ public function testBroadCatchesIncludeImplicitThrows (): void
391+ {
392+ $ this ->analyse ([__DIR__ . '/data/unused-variable-broad-catch.php ' ], [
393+ ['Value assigned to variable $file is never read. ' , 71 ],
394+ ]);
395+ }
396+
390397}
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.0
2+
3+ namespace UnusedVariableBroadCatch ;
4+
5+ function readDocumentation (\ReflectionParameter $ parameter ): void
6+ {
7+ if ($ parameter ->getName () === 'required ' ) {
8+ throw new \RuntimeException ('Missing documentation ' );
9+ }
10+ }
11+
12+ function defaultValue (\ReflectionParameter $ parameter ): mixed
13+ {
14+ try {
15+ readDocumentation ($ parameter );
16+ return $ parameter ->isDefaultValueAvailable () ? $ parameter ->getDefaultValue () : null ;
17+ } catch (\Exception ) {
18+ $ default = null ;
19+ if ($ parameter ->isDefaultValueAvailable ()) {
20+ $ default = $ parameter ->getDefaultValue ();
21+ }
22+ return $ default ;
23+ }
24+ }
25+
26+ function readDefinition (bool $ fail ): void
27+ {
28+ if ($ fail ) {
29+ throw new \Error ('Missing definition ' );
30+ }
31+ }
32+
33+ /** @throws \TypeError */
34+ function checkType (): void
35+ {
36+ throw new \TypeError ();
37+ }
38+
39+ function definition (bool $ fail , bool $ check ): ?int
40+ {
41+ try {
42+ readDefinition ($ fail );
43+ if ($ check ) {
44+ checkType ();
45+ }
46+ return 1 ;
47+ } catch (\Error ) {
48+ $ fallback = null ;
49+ if ($ check ) {
50+ $ fallback = 0 ;
51+ }
52+ return $ fallback ;
53+ }
54+ }
55+
56+ /** @throws \RuntimeException */
57+ function loadFile (): object
58+ {
59+ return new \stdClass ();
60+ }
61+
62+ function validateFile (object $ file ): void
63+ {
64+ if (!isset ($ file ->valid )) {
65+ throw new \RuntimeException ('Invalid file ' );
66+ }
67+ }
68+
69+ function validatedFile (): ?object
70+ {
71+ $ file = null ;
72+ try {
73+ $ file = loadFile ();
74+ validateFile ($ file );
75+ } catch (\Exception ) {
76+ $ file = null ;
77+ }
78+ return $ file ;
79+ }
You can’t perform that action at this time.
0 commit comments