Completed
Push — master ( fc51f9...b3b654 )
by Freek
03:59
created

ScalarTypeHintsRemover   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A leaveNode() 0 14 4
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 === null) {
21
            return;
22
        }
23
24
        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, 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...
25
            $node->type = null;
26
        }
27
    }
28
29
    protected function isScalar(string $type) : bool
30
    {
31
        return in_array($type, ['int', 'integer', 'float', 'string', 'bool', 'boolean']);
32
    }
33
}
34