1 | <?php namespace nyx\console\input\parameter\definitions; |
||
18 | class Arguments extends input\parameter\Definitions |
||
19 | { |
||
20 | /** |
||
21 | * @var int The number of arguments required to be present in an Input Arguments collection for it to |
||
22 | * to conform to the Definitions in this collection. |
||
23 | */ |
||
24 | private $required = 0; |
||
25 | |||
26 | /** |
||
27 | * @var bool Whether one of the arguments accepts multiple values and therefore must be the last argument |
||
28 | * present in the definition. |
||
29 | */ |
||
30 | private $hasMultiparam = false; |
||
31 | |||
32 | /** |
||
33 | * @var bool Whether one of the arguments is optional, meaning no more required arguments can be defined. |
||
34 | */ |
||
35 | private $hasOptional = false; |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public function __construct($arguments = null) |
||
46 | |||
47 | /** |
||
48 | * Returns the next Input Argument Definition for the given Input Argument values collection, |
||
49 | * unless the collection already exceeds the number of defined Arguments. |
||
50 | * |
||
51 | * @param input\parameter\values\Arguments $values |
||
52 | * @return input\Argument |
||
53 | */ |
||
54 | public function getNextDefinition(input\parameter\values\Arguments $values) : ?input\Argument |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function replace($items) : core\collections\interfaces\Collection |
||
93 | |||
94 | /** |
||
95 | * Adds a Input Argument Definition to this collection. |
||
96 | * |
||
97 | * @param input\Argument $argument The argument definition. |
||
98 | * @throws \InvalidArgumentException When the argument's type is invalid. |
||
99 | * @throws \LogicException When an incorrect argument was given. |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function add(core\interfaces\Named $argument) : core\collections\interfaces\NamedObjectSet |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | * |
||
150 | * Overridden to update the counters and flags we're keeping track of internally. |
||
151 | */ |
||
152 | public function remove(string $name) : core\collections\interfaces\NamedObjectSet |
||
178 | |||
179 | /** |
||
180 | * Returns the number of Arguments defined in this collection. |
||
181 | * |
||
182 | * @return int |
||
183 | */ |
||
184 | public function count() : int |
||
188 | |||
189 | /** |
||
190 | * Returns the number of arguments required to be present in an Input Arguments collection for it to |
||
191 | * to conform to the Definitions in this collection. |
||
192 | * |
||
193 | * @return int |
||
194 | */ |
||
195 | public function required() : int |
||
199 | |||
200 | /** |
||
201 | * Unpacks a sequence of Argument constructor arguments into an Argument instance. |
||
202 | * |
||
203 | * @see \nyx\console\input\Argument::__construct() |
||
204 | * |
||
205 | * @param array $definition The arguments to unpack. The order must match the constructor's signature. |
||
206 | * @return input\Argument |
||
207 | */ |
||
208 | protected function unpack(array $definition) : input\Argument |
||
218 | } |
||
219 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.