ClassAssignmentTransformer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Padawan\Parser\Transformer;
4
5
use PhpParser\Node\Expr\Assign;
6
use PhpParser\Node\Expr\PropertyFetch;
7
use Padawan\Domain\Project\Node\ClassData;
8
use Padawan\Domain\Project\Node\ClassProperty;
9
use Padawan\Framework\Complete\Resolver\NodeTypeResolver;
10
use PhpParser\Node\Expr\Variable;
11
use Padawan\Domain\Project\FQCN;
12
13
class ClassAssignmentTransformer
14
{
15
    public function __construct(NodeTypeResolver $typeResolver)
16
    {
17
        $this->typeResolver = $typeResolver;
18
    }
19
    public function transform(Assign $node, ClassData $class, $scope, $index)
20
    {
21
        $fetch = $node->var;
22
        if (!$fetch instanceof PropertyFetch) {
23
            return;
24
        }
25
        if (!$fetch->var instanceof Variable || $fetch->var->name !== 'this') {
26
            return;
27
        }
28
        $type = $this->typeResolver->getType(
29
            $node->expr,
30
            $index,
31
            $scope
32
        );
33
        if ($class->hasProp($fetch->name)) {
34
            $prop = $class->getProp($fetch->name);
35
            if ($type instanceof FQCN) {
36
                $prop->setType($type);
37
            }
38
        } else {
39
            $class->addProp(
40
                new ClassProperty($fetch->name, $type)
0 ignored issues
show
Bug introduced by
It seems like $fetch->name can also be of type object<PhpParser\Node\Expr>; however, Padawan\Domain\Project\N...Property::__construct() 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...
Documentation introduced by
$type is of type object<Padawan\Domain\Project\FQCN>|null|boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
            );
42
        }
43
    }
44
45
    private $typeResolver;
46
}
47