| Conditions | 5 |
| Paths | 5 |
| Total Lines | 20 |
| Code Lines | 9 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 22 | public function validate(array $entities, $options = []) { |
||
| 23 | $violations = new ConstraintViolationList(); |
||
| 24 | |||
| 25 | // We implement the same logic as \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator |
||
| 26 | // here as core does not always write constraints with non-form use cases |
||
| 27 | // in mind. |
||
| 28 | foreach ($entities as $entity) { |
||
| 29 | if (isset($options['validators'])) { |
||
| 30 | // Checks that a file meets the criteria specified by the validators. |
||
| 31 | if ($errors = file_validate($entity, $options['validators'])) { |
||
| 32 | foreach ($errors as $error) { |
||
| 33 | $violation = new ConstraintViolation($error, $error, [], $entity, '', $entity); |
||
| 34 | $violations->add($violation); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | return $violations; |
||
|
|
|||
| 41 | } |
||
| 42 | |||
| 44 |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.