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:
Complex classes like iDokladAbstractModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use iDokladAbstractModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | abstract class iDokladAbstractModel implements iDokladModelInterface |
||
20 | { |
||
21 | // remove nulls in toArray() calling. |
||
22 | const TOARRAY_REMOVE_NULLS = 'rnulls'; |
||
23 | |||
24 | /** |
||
25 | * @param ResponseInterface $response |
||
26 | * |
||
27 | * @throws \RuntimeException |
||
28 | * |
||
29 | * @return iDokladModelInterface |
||
30 | */ |
||
31 | public static function createFromResponse(ResponseInterface $response): iDokladModelInterface |
||
41 | |||
42 | /** |
||
43 | * Need convert some property to iDokladAbstractModel instance? |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | public static function getModelMap(): array |
||
51 | |||
52 | /** |
||
53 | * Need convert some property to Enum object? |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | public static function getEnumMap(): array |
||
61 | |||
62 | /** |
||
63 | * Need convert some DateTime public properties of this model? |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public static function getDateMap(): array |
||
71 | |||
72 | /** |
||
73 | * @param \stdClass $data |
||
74 | * |
||
75 | * @return iDokladModelInterface |
||
76 | */ |
||
77 | public static function createFromStd(\stdClass $data): iDokladModelInterface |
||
118 | |||
119 | /** |
||
120 | * @param array $options |
||
121 | * |
||
122 | * @throws \InvalidArgumentException |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function toArray(array $options = []): array |
||
141 | |||
142 | /** |
||
143 | * @param mixed $value |
||
144 | * @param array $options |
||
145 | * |
||
146 | * @return array|string |
||
147 | */ |
||
148 | public function recursiveToArray($value, array $options) |
||
180 | |||
181 | /** |
||
182 | * @param array $options |
||
183 | * @param array $data |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | protected function toArrayProcessOptions(array $options, array $data): array |
||
203 | |||
204 | /** |
||
205 | * @return ConstraintViolationListInterface |
||
206 | * |
||
207 | * @throws \RuntimeException |
||
208 | * @throws \InvalidArgumentException |
||
209 | * @throws \ReflectionException |
||
210 | */ |
||
211 | public function validate(): ConstraintViolationListInterface |
||
220 | |||
221 | /** |
||
222 | * @param string $name |
||
223 | * @param array $arguments |
||
224 | * |
||
225 | * @throws \InvalidArgumentException |
||
226 | * |
||
227 | * @return mixed |
||
228 | */ |
||
229 | public function __call($name, $arguments) |
||
249 | |||
250 | /** |
||
251 | * @param array $properties |
||
252 | * |
||
253 | * @throws \InvalidArgumentException |
||
254 | */ |
||
255 | View Code Duplication | protected function processProperties(array $properties) |
|
269 | } |
||
270 |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.