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 |
||
| 18 | abstract class Model |
||
| 19 | { |
||
| 20 | protected $mappingClasses = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Contains property name mappings. |
||
| 24 | * |
||
| 25 | * [ |
||
| 26 | * 'data_array_property1' => 'objectProperty1', |
||
| 27 | * 'data_array_property2' => 'objectProperty2', |
||
| 28 | * ] |
||
| 29 | * |
||
| 30 | * Data array property uses as keys |
||
| 31 | * because there is can be more then one rule per object property |
||
| 32 | * |
||
| 33 | * f.g. $data['nmodels'] and ['modelsnum'] should map in modelsCount property. |
||
| 34 | * Otherwise not unique array keys cause remapping of properties. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $propNameMap = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor |
||
| 42 | * |
||
| 43 | * @param array $data |
||
| 44 | */ |
||
| 45 | 74 | public function __construct($data = []) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Set from array |
||
| 52 | * |
||
| 53 | * @param array $data |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | 74 | public function fromArray($data) |
|
| 57 | { |
||
| 58 | 74 | foreach ($data as $key => $val) { |
|
| 59 | 49 | if (is_int($key)) { |
|
| 60 | 39 | if (method_exists($this, "add")) { |
|
| 61 | 39 | $this->add($val); |
|
|
|
|||
| 62 | 39 | } |
|
| 63 | 39 | } |
|
| 64 | |||
| 65 | 49 | $propertyName = $key; |
|
| 66 | 49 | $ourPropertyName = array_search($propertyName, $this->propNameMap); |
|
| 67 | |||
| 68 | 49 | if ($ourPropertyName && isset($data[$ourPropertyName])) { |
|
| 69 | $propertyName = $ourPropertyName; |
||
| 70 | } |
||
| 71 | |||
| 72 | 49 | if (!empty($this->propNameMap)) { |
|
| 73 | 37 | if (array_key_exists($key, $this->propNameMap)) { |
|
| 74 | 36 | $propertyName = $this->propNameMap[$key]; |
|
| 75 | 36 | } |
|
| 76 | 37 | } |
|
| 77 | |||
| 78 | 49 | if (property_exists($this, $propertyName)) { |
|
| 79 | 49 | if (isset($this->mappingClasses[$propertyName])) { |
|
| 80 | 48 | $this->{$propertyName} = new $this->mappingClasses[$propertyName]($val); |
|
| 81 | 48 | } else { |
|
| 82 | 49 | $this->{$propertyName} = $val; |
|
| 83 | } |
||
| 84 | 49 | } |
|
| 85 | 74 | } |
|
| 86 | 74 | return $this; |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Set from json |
||
| 91 | * |
||
| 92 | * @param string $json |
||
| 93 | * @return $this |
||
| 94 | */ |
||
| 95 | 1 | public function fromJson($json) |
|
| 96 | { |
||
| 97 | 1 | $this->fromArray(json_decode($json, true)); |
|
| 98 | 1 | return $this; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get array from object |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | 8 | public function toArray() |
|
| 107 | { |
||
| 108 | 8 | return $this->toArrayRecursive($this); |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get array from object |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | 1 | public function toJson() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get array from object |
||
| 123 | * |
||
| 124 | * @param array|object $data |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | 9 | protected function toArrayRecursive($data) |
|
| 156 | } |
||
| 157 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: