1 | <?php |
||
24 | abstract class AbstractTask implements TaskInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var string $name |
||
28 | */ |
||
29 | private $name; |
||
30 | |||
31 | /** |
||
32 | * @var string $description |
||
33 | */ |
||
34 | private $description; |
||
35 | |||
36 | /** |
||
37 | * @var EventDispatcherInterface $dispatcher |
||
38 | */ |
||
39 | private $dispatcher; |
||
40 | |||
41 | /** |
||
42 | * @var array $parameters |
||
43 | */ |
||
44 | private $parameters = []; |
||
45 | |||
46 | /** |
||
47 | * @type HelperSet |
||
48 | */ |
||
49 | private $helperSet; |
||
50 | |||
51 | /** |
||
52 | * Returns whether or not AbstractTask will continue to the next AbstractTask when there is an error. |
||
53 | * |
||
54 | * @type bool |
||
55 | */ |
||
56 | private $continueOnError; |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function configure() |
||
64 | |||
65 | /** |
||
66 | * {@inheritDoc} |
||
67 | */ |
||
68 | public function getName() |
||
76 | |||
77 | /** |
||
78 | * Sets the name of the task |
||
79 | * |
||
80 | * @param string $name |
||
81 | * |
||
82 | * @return AbstractTask |
||
83 | */ |
||
84 | public function setName($name) |
||
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | public function getDescription() |
||
98 | |||
99 | /** |
||
100 | * Sets the description of the AbstractTask |
||
101 | * |
||
102 | * @param string $description |
||
103 | * |
||
104 | * @return AbstractTask |
||
105 | */ |
||
106 | public function setDescription($description) |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function getEventDispatcher() |
||
120 | |||
121 | /** |
||
122 | * @param EventDispatcherInterface $dispatcher |
||
123 | * |
||
124 | * @return AbstractTask |
||
125 | */ |
||
126 | public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
||
132 | |||
133 | /** |
||
134 | * @return array |
||
135 | */ |
||
136 | public function getParameterDefinition() |
||
140 | |||
141 | /** |
||
142 | * Adds a parameter to the definition of the Task |
||
143 | * |
||
144 | * @param string $name Name of the task parameter |
||
145 | * @param bool $required If true, task parameter is required |
||
146 | * @param string $description Description of the task parameter |
||
147 | * @param mixed $default Default value of the task parameter, null by default |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function addParameter($name, $required = false, $description = '', $default = null) |
||
163 | |||
164 | /** |
||
165 | * {@inheritDoc} |
||
166 | */ |
||
167 | public function getParameters() |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | * |
||
180 | * @throws ParameterNotFoundException |
||
181 | * @throws RequiredParameterException |
||
182 | */ |
||
183 | public function getParameter($name, $validate = true) |
||
203 | |||
204 | /** |
||
205 | * Returns true if the Task has a parameter with the given name, and the value is not null. |
||
206 | * Returns false otherwise. |
||
207 | * |
||
208 | * @param string $name |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function hasParameter($name) |
||
222 | |||
223 | /** |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function continueOnError() |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function setParameter($name, $value) |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function validate() |
||
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function getHelperSet() |
||
266 | |||
267 | /** |
||
268 | * Returns true if the HelperSet is set |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | public function hasHelperSet() |
||
276 | |||
277 | /** |
||
278 | * @param HelperSet $helperSet |
||
279 | * |
||
280 | * @return $this |
||
281 | */ |
||
282 | public function setHelperSet(HelperSet $helperSet) |
||
288 | |||
289 | /** |
||
290 | * Tokenize the given option, if it is a string. |
||
291 | * |
||
292 | * @param mixed $option |
||
293 | * |
||
294 | * @return mixed |
||
295 | */ |
||
296 | private function replaceTokens($option) |
||
317 | } |
||
318 |
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.