1 | <?php namespace nyx\console\input\parameter\definitions; |
||
17 | class Options extends input\parameter\Definitions |
||
18 | { |
||
19 | /** |
||
20 | * @var input\Option[] A map of Option names to their Definitions. |
||
21 | */ |
||
22 | protected $items = []; |
||
23 | |||
24 | /** |
||
25 | * @var input\Option[] A map of Option shortcuts to their Definitions. |
||
26 | */ |
||
27 | protected $shortcuts = []; |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | public function __construct($options = null) |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function replace($items) : core\collections\interfaces\Collection |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function add(core\interfaces\Named $option) : core\collections\interfaces\NamedObjectSet |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function remove(string $name): core\collections\interfaces\NamedObjectSet |
||
89 | |||
90 | /** |
||
91 | * Returns the map of Option shortcuts to their Definitions. |
||
92 | * |
||
93 | * @return input\Option[] |
||
94 | */ |
||
95 | public function getShortcuts() : array |
||
99 | |||
100 | /** |
||
101 | * Returns the Option matching a given shortcut name. |
||
102 | * |
||
103 | * @param string $shortcut The name of the shortcut. |
||
104 | * @return input\Option The Option matching the shortcut name. |
||
105 | * @throws core\collections\exceptions\KeyNotExists When the given shortcut is not defined. |
||
106 | */ |
||
107 | public function getByShortcut(string $shortcut) : input\Option |
||
115 | |||
116 | /** |
||
117 | * Unpacks a sequence of Option constructor arguments into an Option instance. |
||
118 | * |
||
119 | * @see \nyx\console\input\Option::__construct() |
||
120 | * |
||
121 | * @param array $definition The arguments to unpack. The order must match the constructor's signature. |
||
122 | * @return input\Option |
||
123 | */ |
||
124 | protected function unpack(array $definition) : input\Option |
||
134 | } |
||
135 |
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.