Completed
Push — master ( 8536ac...566946 )
by Дмитрий
12:19 queued 08:32
created

TryCatchSt::compile()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 3
b 0
f 0
nc 12
nop 2
dl 0
loc 22
ccs 0
cts 18
cp 0
crap 56
rs 6.9811
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
use PHPSA\Compiler\Expression;
11
12
class TryCatchSt extends AbstractCompiler
13
{
14
    protected $name = '\PhpParser\Node\Stmt\TryCatch';
15
16
    /**
17
     * @param \PhpParser\Node\Stmt\TryCatch $statement
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21
    public function compile($statement, Context $context)
22
    {
23
        if (count($statement->stmts) > 0) {
24
            foreach ($statement->stmts as $stmt) {
25
                \PHPSA\nodeVisitorFactory($stmt, $context);
26
            }
27
        } else {
28
            $context->notice('not-implemented-body', 'Missing body', $statement);
29
        }
30
31
        if (count($statement->catches) > 0) {
32
            foreach ($statement->catches as $stmt) {
33
                \PHPSA\nodeVisitorFactory($stmt, $context);
34
            }
35
        }
36
37
        if (count($statement->finallyStmts) > 0) {
38
            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...
39
                \PHPSA\nodeVisitorFactory($stmt, $context);
40
            }
41
        }
42
    }
43
}
44