1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pinepain/php-v8-js-sandbox PHP library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the |
11
|
|
|
* LICENSE file that was distributed with this source or visit |
12
|
|
|
* http://opensource.org/licenses/MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
namespace Pinepain\JsSandbox\Specs; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Pinepain\JsSandbox\Specs\Parameters\ParameterSpecInterface; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class ParametersList implements ParametersListInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $number_of_parameters = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $number_of_required_parameters = 0; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $variadic = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Parameters\ParameterSpecInterface[] |
41
|
|
|
*/ |
42
|
|
|
private $parameters; |
43
|
|
|
|
44
|
|
|
// TODO: constructor should accept all props by value, not guess them all |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Parameters\ParameterSpecInterface[] ...$parameters |
48
|
|
|
*/ |
49
|
|
|
public function __construct(ParameterSpecInterface ...$parameters) |
50
|
|
|
{ |
51
|
|
|
$this->parameters = $parameters; |
|
|
|
|
52
|
|
|
|
53
|
|
|
$this->number_of_parameters = $this->getNumberOfParametersFromArray($parameters); |
|
|
|
|
54
|
|
|
$this->number_of_required_parameters = $this->getNumberOfRequiredParametersFromArray($parameters); |
|
|
|
|
55
|
|
|
$this->variadic = $this->getIsVariadicFromArray($parameters); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getNumberOfParameters(): int |
62
|
|
|
{ |
63
|
|
|
return $this->number_of_parameters; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getNumberOfRequiredParameters(): int |
70
|
|
|
{ |
71
|
|
|
return $this->number_of_required_parameters; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function isVariadic(): bool |
78
|
|
|
{ |
79
|
|
|
return $this->variadic; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getParameters(): array |
86
|
|
|
{ |
87
|
|
|
return $this->parameters; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Parameters\ParameterSpecInterface[] ...$parameters |
92
|
|
|
* |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
protected function getNumberOfParametersFromArray(array $parameters): int |
96
|
|
|
{ |
97
|
|
|
return count($parameters); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Parameters\ParameterSpecInterface[] ...$parameters |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
protected function getNumberOfRequiredParametersFromArray(array $parameters): int |
106
|
|
|
{ |
107
|
|
|
$required = 0; |
108
|
|
|
|
109
|
|
|
foreach ($parameters as $p) { |
110
|
|
|
if ($p->isOptional() || $p->isVariadic()) { |
111
|
|
|
break; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$required++; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $required; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Parameters\ParameterSpecInterface[] ...$parameters |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
protected function getIsVariadicFromArray(array $parameters): bool |
126
|
|
|
{ |
127
|
|
|
foreach ($parameters as $p) { |
128
|
|
|
if ($p->isVariadic()) { |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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..