1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL\Ast\Common; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\NodeInterface; |
13
|
|
|
use Railt\Parser\Ast\Rule; |
14
|
|
|
use Railt\Parser\Ast\RuleInterface; |
15
|
|
|
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesTypeIndication as Hint; |
16
|
|
|
use Railt\SDL\Ast\ProvidesName; |
17
|
|
|
use Railt\SDL\Ast\Support\TypeNameProvider; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class TypeHintNode |
21
|
|
|
*/ |
22
|
|
|
class TypeHintNode extends Rule implements ProvidesName |
23
|
|
|
{ |
24
|
|
|
use TypeNameProvider; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $modifiers = 0; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* TypeHintNode constructor. |
33
|
|
|
* @param string $name |
34
|
|
|
* @param array $children |
35
|
|
|
* @param int $offset |
36
|
|
|
*/ |
37
|
11 |
|
public function __construct(string $name, array $children = [], int $offset = 0) |
38
|
|
|
{ |
39
|
11 |
|
parent::__construct($name, $children, $offset); |
40
|
|
|
|
41
|
11 |
|
foreach ($this->analyze($this->getChild(0)) as $child) { |
|
|
|
|
42
|
|
|
switch ($child) { |
43
|
|
|
case 'List': |
44
|
|
|
$this->modifiers |= Hint::IS_LIST; |
45
|
|
|
break; |
46
|
|
|
|
47
|
|
|
case 'NonNull': |
48
|
|
|
$this->modifiers |= $this->isList() ? Hint::IS_LIST_OF_NOT_NULL : Hint::IS_NOT_NULL; |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
} |
52
|
11 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param RuleInterface|NodeInterface $rule |
56
|
|
|
* @return \Generator |
57
|
|
|
*/ |
58
|
11 |
|
private function analyze(RuleInterface $rule): \Generator |
59
|
|
|
{ |
60
|
11 |
|
$name = $rule->getName(); |
61
|
|
|
|
62
|
11 |
|
if ($name !== 'TypeName') { |
63
|
|
|
yield $name; |
64
|
|
|
yield from $this->analyze($rule->getChild(0)); |
|
|
|
|
65
|
|
|
} |
66
|
11 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isList(): bool |
72
|
|
|
{ |
73
|
|
|
return (bool)($this->modifiers & Hint::IS_LIST); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function isNonNull(): bool |
80
|
|
|
{ |
81
|
|
|
return (bool)($this->modifiers & Hint::IS_NOT_NULL); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function isListOfNonNulls(): bool |
88
|
|
|
{ |
89
|
|
|
return (bool)($this->modifiers & Hint::IS_LIST_OF_NOT_NULL); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
6 |
|
public function getModifiers(): int |
96
|
|
|
{ |
97
|
6 |
|
return $this->modifiers; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return null|TypeNameNode|NodeInterface |
102
|
|
|
*/ |
103
|
6 |
|
protected function getTypeNameNode(): ?TypeNameNode |
104
|
|
|
{ |
105
|
6 |
|
return $this->first('TypeName', 3); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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: