Conditions | 5 |
Paths | 4 |
Total Lines | 25 |
Code Lines | 10 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
32 | public function processNext(EditableFormField $field) |
||
33 | { |
||
34 | // When we find a step, bubble up to the top |
||
35 | if ($field instanceof EditableFormStep) { |
||
36 | return $this->getParent()->processNext($field); |
||
37 | } |
||
38 | |||
39 | // Skip over fields that don't generate formfields |
||
40 | if (get_class($field) === EditableFormField::class || !$field->getFormField()) { |
||
41 | return $this; |
||
|
|||
42 | } |
||
43 | /** @var EditableFormField $formField */ |
||
44 | $formField = $field->getFormField(); |
||
45 | |||
46 | // Save this field |
||
47 | $this->push($formField); |
||
48 | |||
49 | // Nest fields that are containers |
||
50 | if ($formField instanceof UserFormsFieldContainer) { |
||
51 | return $formField->setParent($this); |
||
52 | } |
||
53 | |||
54 | // Add any subsequent fields to this |
||
55 | return $this; |
||
56 | } |
||
57 | } |
||
58 |
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.