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\Compiler\Builder\Common; |
11
|
|
|
|
12
|
|
|
use Railt\Reflection\Contracts\Definition; |
13
|
|
|
use Railt\Reflection\Contracts\Definition\InputDefinition; |
14
|
|
|
use Railt\Reflection\Contracts\Invocation\InputInvocation; |
15
|
|
|
use Railt\Reflection\Contracts\Invocation\TypeInvocation; |
16
|
|
|
use Railt\SDL\Exception\SemanticException; |
17
|
|
|
use Railt\SDL\Exception\TypeConflictException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ValueInvocation |
21
|
|
|
*/ |
22
|
|
|
class ValueInvocation |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var \SplQueue |
26
|
|
|
*/ |
27
|
|
|
private $stack; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ValueInvocation constructor. |
31
|
|
|
*/ |
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->stack = new \SplQueue(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param mixed $value |
39
|
|
|
* @return mixed |
40
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
41
|
|
|
* @throws \InvalidArgumentException |
42
|
|
|
*/ |
43
|
|
|
public function invoke($value) |
44
|
|
|
{ |
45
|
|
|
if ($value instanceof Definition) { |
46
|
|
|
$this->checkRecursiveReferring($value); |
47
|
|
|
$this->push($value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($value instanceof InputInvocation) { |
|
|
|
|
51
|
|
|
return $this->invokeInput($value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Definition $value |
59
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
60
|
|
|
*/ |
61
|
|
|
private function checkRecursiveReferring(Definition $value): void |
62
|
|
|
{ |
63
|
|
|
if ($this->has($value)) { |
64
|
|
|
$error = 'Recursive reference (%s -> %s) detected while %s invocation'; |
65
|
|
|
$error = \sprintf($error, $this->getStackAsString(), $value, $value); |
66
|
|
|
|
67
|
|
|
throw (new SemanticException($error))->throwsIn($value->getFile(), $value->getLine(), $value->getColumn()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Definition $definition |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
private function has(Definition $definition): bool |
76
|
|
|
{ |
77
|
|
|
$stack = clone $this->stack; |
78
|
|
|
|
79
|
|
|
foreach ($stack as $i) { |
80
|
|
|
if ((string)$i === (string)$definition) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
private function getStackAsString(): string |
92
|
|
|
{ |
93
|
|
|
return \implode(' -> ', \iterator_to_array(clone $this->stack)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Definition $definition |
98
|
|
|
*/ |
99
|
|
|
private function push(Definition $definition): void |
100
|
|
|
{ |
101
|
|
|
$this->stack->push($definition); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param InputInvocation $input |
106
|
|
|
* @return InputInvocation |
107
|
|
|
* @throws \InvalidArgumentException |
108
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
109
|
|
|
*/ |
110
|
|
|
private function invokeInput(InputInvocation $input): InputInvocation |
111
|
|
|
{ |
112
|
|
|
/** @var InputDefinition $definition */ |
113
|
|
|
$definition = $input->getDefinition(); |
114
|
|
|
|
115
|
|
|
foreach ($definition->getFields() as $field) { |
116
|
|
|
$argument = $input->getArgument($field->getName()); |
117
|
|
|
|
118
|
|
|
if ($argument === null && $field->isNonNull() && ! $field->hasDefaultValue()) { |
119
|
|
|
$error = \sprintf('Missing value for required argument %s', $field); |
120
|
|
|
throw (new TypeConflictException($error))->throwsIn($input->getFile(), $input->getLine(), |
121
|
|
|
$input->getColumn()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->invoke($argument); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $input; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.