Completed
Pull Request — master (#85)
by Kévin
18:16 queued 14:06
created

TryCatchSt::compile()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8.0504

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 22
ccs 13
cts 18
cp 0.7221
crap 8.0504
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
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 1
    public function compile($statement, Context $context)
21
    {
22 1
        if (count($statement->stmts) > 0) {
23 1
            foreach ($statement->stmts as $stmt) {
24 1
                \PHPSA\nodeVisitorFactory($stmt, $context);
25 1
            }
26 1
        } else {
27
            $context->notice('not-implemented-body', 'Missing body', $statement);
28
        }
29
30 1
        if (count($statement->catches) > 0) {
31 1
            foreach ($statement->catches as $stmt) {
32 1
                \PHPSA\nodeVisitorFactory($stmt, $context);
33 1
            }
34 1
        }
35
36 1
        if (count($statement->finallyStmts) > 0) {
37
            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...
38
                \PHPSA\nodeVisitorFactory($stmt, $context);
39
            }
40
        }
41 1
    }
42
}
43