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

TryCatchSt   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 72.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 13
cts 18
cp 0.7221
rs 10
wmc 7
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
C compile() 0 22 7
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