ScalarTypeHintsRemover   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A leaveNode() 0 19 5
A isScalar() 0 4 1
1
<?php
2
3
namespace Spatie\Php7to5\NodeVisitors;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Param;
7
use PhpParser\NodeVisitorAbstract;
8
9
class ScalarTypeHintsRemover extends NodeVisitorAbstract
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function leaveNode(Node $node)
15
    {
16
        if (!$node instanceof Param) {
17
            return;
18
        }
19
20
        if ($node->type instanceof Node\NullableType) {
21
            $node->type = $node->type->type;
22
            if (!$node->default) {
23
                $node->default = new Node\Expr\ConstFetch(
24
                    new Node\Name('null')
25
                );
26
            }
27
        }
28
29
        if ($this->isScalar($node->type)) {
0 ignored issues
show
Bug introduced by
It seems like $node->type can also be of type object<PhpParser\Node\Name>; however, Spatie\Php7to5\NodeVisit...intsRemover::isScalar() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
30
            $node->type = null;
31
        }
32
    }
33
34
    /**
35
     * @param string|null $type
36
     *
37
     * @return bool
38
     */
39
    protected function isScalar($type)
40
    {
41
        return in_array($type, ['int', 'integer', 'float', 'string', 'bool', 'boolean']);
42
    }
43
}
44