Completed
Push — 8.x-1.x ( 59e77e...5ec7b0 )
by Janez
03:12
created

CropType::getSoftLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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