CropType   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 120
rs 10
c 1
b 0
f 0
wmc 7
lcom 2
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 3 1
A getAspectRatio() 0 3 1
A validate() 0 4 1
A getIterator() 0 3 1
A getCropTypeNames() 0 8 1
A getSoftLimit() 0 6 1
A getHardLimit() 0 6 1
1
<?php
2
3
namespace Drupal\crop\Entity;
4
5
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
6
use Drupal\crop\CropTypeInterface;
7
use Symfony\Component\Validator\ConstraintViolationList;
8
9
/**
10
 * Defines the Crop type configuration entity.
11
 *
12
 * @ConfigEntityType(
13
 *   id = "crop_type",
14
 *   label = @Translation("Crop type"),
15
 *   handlers = {
16
 *     "form" = {
17
 *       "add" = "Drupal\crop\Form\CropTypeForm",
18
 *       "edit" = "Drupal\crop\Form\CropTypeForm",
19
 *       "delete" = "Drupal\crop\Form\CropTypeDeleteForm"
20
 *     },
21
 *     "list_builder" = "Drupal\crop\CropTypeListBuilder",
22
 *   },
23
 *   admin_permission = "administer crop types",
24
 *   config_prefix = "type",
25
 *   bundle_of = "crop",
26
 *   entity_keys = {
27
 *     "id" = "id",
28
 *     "label" = "label",
29
 *   },
30
 *   links = {
31
 *     "edit-form" = "/admin/config/media/crop/manage/{crop_type}",
32
 *     "delete-form" = "/admin/config/media/crop/manage/{crop_type}/delete",
33
 *   },
34
 *   constraints = {
35
 *     "CropTypeMachineNameValidation" = {},
36
 *     "CropTypeAspectRatioValidation" = {},
37
 *   }
38
 * )
39
 */
40
class CropType extends ConfigEntityBundleBase implements \IteratorAggregate, CropTypeInterface {
41
42
  /**
43
   * The machine name of this crop type.
44
   *
45
   * @var string
46
   */
47
  public $id;
48
49
  /**
50
   * The human-readable name of the crop type.
51
   *
52
   * @var string
53
   */
54
  public $label;
55
56
  /**
57
   * A brief description of this crop type.
58
   *
59
   * @var string
60
   */
61
  public $description;
62
63
  /**
64
   * The ratio of the image of this crop type.
65
   *
66
   * @var string
67
   */
68
  public $aspect_ratio;
69
70
  /**
71
   * Soft limit width in px.
72
   *
73
   * @var int
74
   */
75
  public $soft_limit_width;
76
77
  /**
78
   * Soft limit height in px.
79
   *
80
   * @var int
81
   */
82
  public $soft_limit_height;
83
84
  /**
85
   * Hard limit width in px.
86
   *
87
   * @var int
88
   */
89
  public $hard_limit_width;
90
91
  /**
92
   * Hard limit height in px.
93
   *
94
   * @var int
95
   */
96
  public $hard_limit_height;
97
98
  /**
99
   * {@inheritdoc}
100
   */
101
  public function id() {
102
    return $this->id;
103
  }
104
105
  /**
106
   * {@inheritdoc}
107
   */
108
  public function getAspectRatio() {
109
    return $this->aspect_ratio;
110
  }
111
112
  /**
113
   * {@inheritdoc}
114
   */
115
  public function validate() {
116
    $violations = $this->getTypedData()->validate();
117
    return new ConstraintViolationList(iterator_to_array($violations));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...to_array($violations)); (Symfony\Component\Valida...ConstraintViolationList) is incompatible with the return type declared by the interface Drupal\crop\CropTypeInterface::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...
118
  }
119
120
  /**
121
   * {@inheritdoc}
122
   */
123
  public function getIterator() {
124
    return new \ArrayIterator();
125
  }
126
127
  /**
128
   * {@inheritdoc}
129
   */
130
  public static function getCropTypeNames() {
131
    return array_map(
132
      function ($bundle_info) {
133
        return $bundle_info['label'];
134
      },
135
      \Drupal::service('entity_type.bundle.info')->getBundleInfo('crop')
136
    );
137
  }
138
139
  /**
140
   * {@inheritdoc}
141
   */
142
  public function getSoftLimit() {
143
    return [
144
      'width' => $this->soft_limit_width,
145
      'height' => $this->soft_limit_height,
146
    ];
147
  }
148
149
  /**
150
   * {@inheritdoc}
151
   */
152
  public function getHardLimit() {
153
    return [
154
      'width' => $this->hard_limit_width,
155
      'height' => $this->hard_limit_height,
156
    ];
157
  }
158
159
}
160