Completed
Push — master ( 4c018e...ece8e1 )
by Randy
01:04
created

AnnotationType::next()   A

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 0
1
<?php
2
3
namespace Dgame\Annotation;
4
5
use Dgame\Type\Type;
6
use Dgame\Type\TypeFactory;
7
8
/**
9
 * Class AnnotationType
10
 * @package Dgame\Annotation
11
 */
12
final class AnnotationType
13
{
14
    /**
15
     * @var Type
16
     */
17
    private $type;
18
    /**
19
     * @var AnnotationType
20
     */
21
    private $next;
22
23
    /**
24
     * AnnotationType constructor.
25
     *
26
     * @param string              $type
27
     * @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...
28
     */
29
    private function __construct(string $type, self $next = null)
30
    {
31
        $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...
32
        $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>. 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...
33
    }
34
35
    /**
36
     * @param string $type
37
     *
38
     * @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...
39
     */
40
    public static function parse(string $type): self
41
    {
42
        if (preg_match_all('/(\[\s*\])/i', $type, $brackets)) {
43
            $base = str_replace(['[', ']'], '', $type);
44
            $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...
45
            for ($i = 1, $c = count($brackets[1]); $i < $c; $i++) {
46
                $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...
47
            }
48
49
            return $type;
50
        }
51
52
        return new self($type);
53
    }
54
55
    /**
56
     * @return Type
57
     */
58
    public function getType(): Type
59
    {
60
        return $this->type;
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    public function hasNext(): bool
67
    {
68
        return $this->next !== null;
69
    }
70
71
    /**
72
     * @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...
73
     */
74
    public function next(): self
75
    {
76
        return $this->next;
77
    }
78
79
    /**
80
     * @param $expression
81
     *
82
     * @return bool
83
     */
84
    public function isImplicit($expression): bool
85
    {
86
        $type = TypeFactory::expression($expression);
87
        if ($type->isArray() && $this->hasNext()) {
88
            return $this->compare($expression, function (self $type, $expr): bool {
89
                return $type->isImplicit($expr);
90
            });
91
        }
92
93
        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...
94
    }
95
96
    /**
97
     * @param $expression
98
     *
99
     * @return bool
100
     */
101
    public function isSame($expression): bool
102
    {
103
        $type = TypeFactory::expression($expression);
104
        if ($type->isArray() && $this->hasNext()) {
105
            return $this->compare($expression, function (self $type, $expr): bool {
106
                return $type->isSame($expr);
107
            });
108
        }
109
110
        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...
111
    }
112
113
    /**
114
     * @param array    $expression
115
     * @param callable $callback
116
     *
117
     * @return bool
118
     */
119
    private function compare(array $expression, callable $callback): bool
120
    {
121
        foreach ($expression as $expr) {
122
            if (!$callback($this->next(), $expr)) {
123
                return false;
124
            }
125
        }
126
127
        return true;
128
    }
129
130
    /**
131
     * @return int
132
     */
133
    public function getDimension(): int
134
    {
135
        $this->iterate($dimension);
136
137
        return $dimension;
138
    }
139
140
    /**
141
     * @return Type
142
     */
143
    public function getBaseType(): Type
144
    {
145
        return $this->iterate()->getType();
146
    }
147
148
    /**
149
     * @param int|null $dimension
150
     *
151
     * @return AnnotationType
152
     */
153
    private function iterate(int &$dimension = null): AnnotationType
154
    {
155
        $dimension = 0;
156
        $type      = $this;
157
        while ($type->hasNext()) {
158
            $type = $type->next();
159
            $dimension++;
160
        }
161
162
        return $type;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function export(): string
169
    {
170
        return $this->getBaseType()->export() . str_repeat('[]', $this->getDimension());
171
    }
172
}