Completed
Push — 8.x-1.x ( 09a174...2d9790 )
by Janez
03:18
created

File::validate()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 20
rs 8.8571
1
<?php
2
3
namespace Drupal\entity_browser\Plugin\EntityBrowser\WidgetValidation;
4
5
use Drupal\entity_browser\WidgetValidationBase;
6
use Symfony\Component\Validator\ConstraintViolation;
7
use Symfony\Component\Validator\ConstraintViolationList;
8
9
/**
10
 * Validates a file based on passed validators.
11
 *
12
 * @EntityBrowserWidgetValidation(
13
 *   id = "file",
14
 *   label = @Translation("File validator")
15
 * )
16
 */
17
class File extends WidgetValidationBase {
18
19
  /**
20
   * {@inheritdoc}
21
   */
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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $violations; (Symfony\Component\Valida...ConstraintViolationList) is incompatible with the return type declared by the interface Drupal\entity_browser\Wi...tionInterface::validate of type Symfony\Component\Valida...tViolationListInterface.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
41
  }
42
43
}
44