Passed
Branch remove-strict-types-directive (4babd7)
by Jan Philipp
07:03
created

StrictTypesFeature::fix()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.246

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 11
cts 14
cp 0.7856
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 7
nop 1
crap 5.246
1
<?php declare (strict_types = 1);
2
3
4
namespace JanPiet\PhpTranspiler\Feature;
5
6
use JanPiet\PhpTranspiler\NodeSearch;
7
use PhpParser\Node\Stmt\Declare_;
8
use PhpParser\Node\Stmt\DeclareDeclare;
9
10
class StrictTypesFeature implements FeatureInterface
11
{
12
13
    /**
14
     * @param NodeSearch $nodeSearch
15
     * @return bool
16
     */
17 2
    public function fix(NodeSearch $nodeSearch): bool
18
    {
19 2
        $found = false;
20
21 2
        foreach ($nodeSearch->eachType(Declare_::class) as $declareNode) {
22 2
            $newDeclares = [];
23
24
            /** @var DeclareDeclare $declareStatement */
25 2
            foreach ($declareNode->declares as $declareStatement) {
26 2
                if ($declareStatement->key === 'strict_types') {
27 2
                    $found = true;
28 2
                    continue;
29
                }
30
31
                $newDeclares[] = $declareNode;
32
            }
33
34 2
            if ($newDeclares) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $newDeclares of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
35
                $declareNode->declares = $newDeclares;
36
                continue;
37
            }
38
39 2
            $nodeSearch->removeNode($declareNode);
40
        }
41
42 2
        return $found;
43
    }
44
}
45