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

TryCatchSt   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 69.23%

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 9
cts 13
cp 0.6923
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B compile() 0 16 5
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