AnnotationType::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dgame\Annotation;
4
5
use Dgame\Type\Type;
6
use Dgame\Type\TypeFactory;
7
use Exception;
8
9
/**
10
 * Class AnnotationType
11
 * @package Dgame\Annotation
12
 */
13
final class AnnotationType
14
{
15
    /**
16
     * @var Type
17
     */
18
    private $type;
19
    /**
20
     * @var AnnotationType|null
21
     */
22
    private $next;
23
24
    /**
25
     * AnnotationType constructor.
26
     *
27
     * @param string              $type
28
     * @param AnnotationType|null $next
0 ignored issues
show
Documentation introduced by
Should the type for parameter $next not be null|\self?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
29
     *
30
     * @throws Exception
31
     */
32
    private function __construct(string $type, self $next = null)
33
    {
34
        $this->type = Type::import($type);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Dgame\Type\Type::import($type) of type object<self> is incompatible with the declared type object<Dgame\Type\Type> of property $type.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
        $this->next = $next;
0 ignored issues
show
Documentation Bug introduced by
It seems like $next can also be of type object<self>. However, the property $next is declared as type object<Dgame\Annotation\AnnotationType>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36
    }
37
38
    /**
39
     * @param string $type
40
     *
41
     * @return AnnotationType
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
42
     * @throws Exception
43
     */
44
    public static function parse(string $type): self
45
    {
46
        if (preg_match_all('/(\[\s*\])/S', $type, $brackets)) {
47
            $base = str_replace(['[', ']'], '', $type);
48
            $type = new self('array', new self($base));
0 ignored issues
show
Documentation introduced by
new self($base) is of type object<Dgame\Annotation\AnnotationType>, but the function expects a null|object<self>.

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...
Coding Style introduced by
Consider using a different name than the parameter $type. This often makes code more readable.
Loading history...
49
            for ($i = 1, $c = count($brackets[1]); $i < $c; $i++) {
50
                $type = new self('array', $type);
0 ignored issues
show
Documentation introduced by
$type is of type object<Dgame\Annotation\AnnotationType>, but the function expects a null|object<self>.

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...
Coding Style introduced by
Consider using a different name than the parameter $type. This often makes code more readable.
Loading history...
51
            }
52
53
            return $type;
54
        }
55
56
        return new self($type);
57
    }
58
59
    /**
60
     * @return Type
61
     */
62
    public function getType(): Type
63
    {
64
        return $this->type;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function hasNext(): bool
71
    {
72
        return $this->next !== null;
73
    }
74
75
    /**
76
     * @return AnnotationType|null
77
     */
78
    public function next(): ?self
79
    {
80
        return $this->next;
81
    }
82
83
    /**
84
     * @param mixed $expression
85
     *
86
     * @return bool
87
     * @throws Exception
88
     */
89
    public function isImplicit($expression): bool
90
    {
91
        $type = TypeFactory::expression($expression);
92
        if ($type->isArray() && $this->hasNext()) {
93
            return $this->compare($expression, function (self $type, $expr): bool {
94
                return $type->isImplicit($expr);
95
            });
96
        }
97
98
        return $type->isImplicitSame($this->type);
0 ignored issues
show
Documentation introduced by
$this->type is of type object<Dgame\Type\Type>, but the function expects a object<self>.

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...
99
    }
100
101
    /**
102
     * @param mixed $expression
103
     *
104
     * @return bool
105
     * @throws Exception
106
     */
107
    public function isSame($expression): bool
108
    {
109
        $type = TypeFactory::expression($expression);
110
        if ($type->isArray() && $this->hasNext()) {
111
            return $this->compare($expression, function (self $type, $expr): bool {
112
                return $type->isSame($expr);
113
            });
114
        }
115
116
        return $type->isSame($this->type);
0 ignored issues
show
Documentation introduced by
$this->type is of type object<Dgame\Type\Type>, but the function expects a object<self>.

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...
117
    }
118
119
    /**
120
     * @param array    $expression
121
     * @param callable $callback
122
     *
123
     * @return bool
124
     */
125
    private function compare(array $expression, callable $callback): bool
126
    {
127
        foreach ($expression as $expr) {
128
            if (!$callback($this->next(), $expr)) {
129
                return false;
130
            }
131
        }
132
133
        return true;
134
    }
135
136
    /**
137
     * @return int
138
     */
139
    public function getDimension(): int
140
    {
141
        $this->iterate($dimension);
142
143
        return $dimension;
144
    }
145
146
    /**
147
     * @return Type|null
148
     */
149
    public function getBaseType(): ?Type
150
    {
151
        $type = $this->iterate();
152
153
        return $type === null ? null : $type->getType();
154
    }
155
156
    /**
157
     * @param int|null $dimension
158
     *
159
     * @return AnnotationType|null
160
     */
161
    private function iterate(int &$dimension = null): ?self
162
    {
163
        $dimension = 0;
164
        $type      = $this;
165
        while ($type !== null && $type->hasNext()) {
166
            $type = $type->next();
167
            $dimension++;
168
        }
169
170
        return $type;
171
    }
172
173
    /**
174
     * @return string|null
175
     */
176
    public function export(): ?string
177
    {
178
        $type = $this->getBaseType();
179
180
        return $type === null ? null : $type->export() . str_repeat('[]', $this->getDimension());
181
    }
182
}
183