Completed
Pull Request — master (#229)
by Enrico
04:42
created

TryCatchSt::compile()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.7283

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 12
nop 2
dl 0
loc 16
ccs 9
cts 13
cp 0.6923
crap 5.7283
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Compiler\Statement;
7
8
use PHPSA\CompiledExpression;
9
use PHPSA\Context;
10
11
class TryCatchSt extends AbstractCompiler
12
{
13
    protected $name = '\PhpParser\Node\Stmt\TryCatch';
14
15
    /**
16
     * @param \PhpParser\Node\Stmt\TryCatch $statement
17
     * @param Context $context
18
     * @return CompiledExpression
19
     */
20 2
    public function compile($statement, Context $context)
21
    {
22 2
        foreach ($statement->stmts as $stmt) {
23 2
            \PHPSA\nodeVisitorFactory($stmt, $context);
24 2
        }
25
26 2
        foreach ($statement->catches as $stmt) {
27 2
            \PHPSA\nodeVisitorFactory($stmt, $context);
28 2
        }
29
30 2
        if (count($statement->finallyStmts) > 0) {
31
            foreach ($statement->finallyStmts as $stmt) {
0 ignored issues
show
Bug introduced by
The expression $statement->finallyStmts of type null|array<integer,object<PhpParser\Node>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
32
                \PHPSA\nodeVisitorFactory($stmt, $context);
33
            }
34
        }
35 2
    }
36
}
37