Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
21 | class DependencyResultEvent extends BaseEvent |
||
22 | { |
||
23 | |||
24 | const CHECK_DEPENDENCIES = 'check-dependencies'; |
||
25 | const DELETE = 'delete'; |
||
26 | |||
27 | /** |
||
28 | * @var EntityInterface |
||
29 | */ |
||
30 | private $entity; |
||
31 | |||
32 | /** |
||
33 | * @var DependencyResultCollection |
||
34 | */ |
||
35 | private $dependencyResultCollection; |
||
36 | |||
37 | /** |
||
38 | * DependencyResultEvent constructor. |
||
39 | * |
||
40 | * @param null|string $name |
||
41 | * @param null|object $target |
||
42 | * @param null|\Traversable|array $params |
||
43 | */ |
||
44 | public function __construct($name = null, $target = null, $params = null) |
||
50 | |||
51 | /** |
||
52 | * @internal |
||
53 | * this is needed, because Core\EventManager clones the event prototype |
||
54 | * and internal object references are not cloned recursively. |
||
55 | */ |
||
56 | public function __clone() |
||
60 | |||
61 | /** |
||
62 | * Resets the dependencyResultCollection |
||
63 | */ |
||
64 | private function resetDependencyResultCollection() |
||
68 | |||
69 | |||
70 | /** |
||
71 | * @return EntityInterface |
||
72 | */ |
||
73 | public function getEntity() |
||
77 | |||
78 | /** |
||
79 | * @param EntityInterface $entity |
||
80 | * |
||
81 | * @return self |
||
82 | */ |
||
83 | public function setEntity($entity) |
||
89 | |||
90 | /** |
||
91 | * Gets the class name of the entity. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getEntityClass() |
||
99 | |||
100 | /** |
||
101 | * Checks the entitys' type. |
||
102 | * |
||
103 | * @param string $class Type name to check against. |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function isEntityInstanceOf($class) |
||
111 | |||
112 | |||
113 | /** |
||
114 | * @return DependencyResultCollection |
||
115 | */ |
||
116 | public function getDependencyResultCollection() |
||
120 | |||
121 | /** |
||
122 | * Shortcut to add dependencies to the collection. |
||
123 | * |
||
124 | * @param string|\Traversable|array $name |
||
125 | * @param null|array|\Traversable $entities |
||
126 | * @param array|null $options |
||
127 | * |
||
128 | * @return DependencyResultCollection |
||
129 | */ |
||
130 | public function addDependencies($name, $entities = null, array $options = null) |
||
134 | |||
135 | /** |
||
136 | * Returns true, if this is a delete event. |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function isDelete() |
||
144 | |||
145 | public function setParam($name, $value) |
||
154 | |||
155 | View Code Duplication | public function setParams($params) |
|
171 | } |
||
172 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.