Completed
Push — master ( 1d77b9...c6a486 )
by David
12s
created

  C

Complexity

Conditions 8

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 6.6666
c 0
b 0
f 0
cc 8
1
<?php
2
3
4
namespace TheCodingMachine\PHPStan\Rules\Exceptions;
5
6
7
use PhpParser\Node;
8
use PhpParser\Node\Stmt\Catch_;
9
use PhpParser\NodeTraverser;
10
use PhpParser\NodeVisitorAbstract;
11
use PHPStan\Analyser\Scope;
12
use PHPStan\Broker\Broker;
13
use PHPStan\Rules\Rule;
14
15
/**
16
 * When throwing into a catch block, checks that the previous exception is passed to the new "throw" clause
17
 * (the initial stack trace must not be lost).
18
 */
19
class ThrowMustBundlePreviousExceptionRule implements Rule
20
{
21
    public function getNodeType(): string
22
    {
23
        return Catch_::class;
24
    }
25
26
    /**
27
     * @param Catch_ $node
28
     * @param \PHPStan\Analyser\Scope $scope
29
     * @return string[]
30
     */
31
    public function processNode(Node $node, Scope $scope): array
32
    {
33
        $visitor = new class($node->var) extends NodeVisitorAbstract {
34
            /**
35
             * @var string
36
             */
37
            private $catchedVariableName;
38
            private $exceptionUsedCount = 0;
39
            private $unusedThrows = [];
40
41
            public function __construct(string $catchedVariableName)
42
            {
43
44
                $this->catchedVariableName = $catchedVariableName;
45
            }
46
47
            public function leaveNode(Node $node) {
48
                if ($node instanceof Node\Expr\Variable) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
49
                    if ($node->name === $this->catchedVariableName) {
50
                        $this->exceptionUsedCount++;
51
                    }
52
                }
53
54
                // If the variable is used in the context of a method call (like $e->getMessage()), the exception is not passed as a "previous exception".
55
                if ($node instanceof Node\Expr\MethodCall) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\MethodCall does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
56
                    if ($node->var instanceof Node\Expr\Variable && $node->var->name === $this->catchedVariableName) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
57
                        $this->exceptionUsedCount--;
58
                    }
59
                }
60
61
                if ($node instanceof Node\Stmt\Throw_ && $this->exceptionUsedCount === 0) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Stmt\Throw_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
62
                    $this->unusedThrows[] = $node;
63
                }
64
            }
65
66
            /**
67
             * @return array
68
             */
69
            public function getUnusedThrows(): array
70
            {
71
                return $this->unusedThrows;
72
            }
73
74
        };
75
76
        $traverser = new NodeTraverser();
77
78
        $traverser->addVisitor($visitor);
79
80
        $traverser->traverse($node->stmts);
81
82
        $errors = [];
83
84
        foreach ($visitor->getUnusedThrows() as $throw) {
85
            $errors[] = sprintf('Thrown exceptions in a catch block must bundle the previous exception (see throw statement line %d)', $throw->getLine());
86
        }
87
88
        return $errors;
89
    }
90
}
91