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 |
||
11 | trait Representer |
||
12 | { |
||
13 | use ArraySerializer; |
||
14 | |||
15 | /** |
||
16 | * Object that is being represented |
||
17 | * or a collection handler |
||
18 | */ |
||
19 | protected $source; |
||
20 | /** |
||
21 | * Class name to be restored |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $targetClassName; |
||
26 | /** |
||
27 | * Strategy indicator |
||
28 | * 1 = one |
||
29 | * 2 = collection |
||
30 | * 3 = restore one |
||
31 | * 4 = restore collection |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $strategy; |
||
36 | |||
37 | 10 | public function __construct($source, $strategy) |
|
46 | |||
47 | public function rules() |
||
51 | |||
52 | /** |
||
53 | * Return property name to wrap a collection representation |
||
54 | * If `null` - no wrapper added |
||
55 | * |
||
56 | * @return null | string |
||
57 | */ |
||
58 | 2 | public function collectionWrapper() |
|
59 | { |
||
60 | 2 | return null; |
|
61 | } |
||
62 | |||
63 | 5 | public function setTargetClassName($name) |
|
64 | { |
||
65 | 5 | $this->targetClassName = $name; |
|
66 | 5 | } |
|
67 | |||
68 | /** |
||
69 | * @param $name |
||
70 | * @return PropertyRule |
||
71 | */ |
||
72 | 10 | public function property($name) |
|
73 | { |
||
74 | 10 | return new PropertyRule($this->source, $name); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Represent one instance |
||
79 | * |
||
80 | * @param $source |
||
81 | * @return static |
||
82 | */ |
||
83 | 8 | public static function one($source) |
|
84 | { |
||
85 | 8 | return new static($source, 1); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Represent collection of instances |
||
90 | * |
||
91 | * @param array $array |
||
92 | * @return static |
||
93 | */ |
||
94 | 4 | public static function collection(array $array) |
|
95 | { |
||
96 | 4 | return new static($array, 2); |
|
97 | } |
||
98 | |||
99 | 4 | protected function getCollectionRepresentation() |
|
100 | { |
||
101 | 4 | if (is_array($this->source) && count($this->source) > 0) { |
|
102 | $representation = array_map(function ($object) { |
||
103 | 4 | return static::one($object)->getOneRepresentation(); |
|
104 | 4 | }, $this->source); |
|
105 | |||
106 | 4 | if ($wrapperName = $this->collectionWrapper()) { |
|
|
|||
107 | 2 | return [$wrapperName => $representation]; |
|
108 | } else { |
||
109 | 2 | return $representation; |
|
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | 8 | View Code Duplication | protected function getOneRepresentation() |
115 | { |
||
116 | 8 | $rules = $this->rules(); |
|
117 | 8 | if (empty($rules)) { |
|
118 | throw new \Exception("There are rules specified in " . static::class . " representer"); |
||
119 | } |
||
120 | |||
121 | 8 | $represented = []; |
|
122 | |||
123 | 8 | foreach ($rules as $rule) { |
|
124 | /** @var $rule PropertyRule */ |
||
125 | 8 | $resultArray = $rule->compile(); |
|
126 | |||
127 | 8 | reset($resultArray); |
|
128 | 8 | $key = key($resultArray); |
|
129 | 8 | $value = $resultArray[$key]; |
|
130 | |||
131 | 8 | $represented[$key] = $value; |
|
132 | 8 | } |
|
133 | |||
134 | 8 | return $represented; |
|
135 | } |
||
136 | |||
137 | 8 | protected function getRepresentation() |
|
138 | { |
||
139 | 8 | switch ($this->strategy) { |
|
140 | 8 | case 1: |
|
141 | 4 | return $this->getOneRepresentation(); |
|
142 | 4 | case 2: |
|
143 | 4 | return $this->getCollectionRepresentation(); |
|
144 | default: |
||
145 | throw new \Exception('Wrong representation strategy selected. Maybe you have accidentally |
||
146 | called `toJSON` instead of `fromJSON`?'); |
||
147 | } |
||
148 | |||
149 | } |
||
150 | |||
151 | |||
152 | /** |
||
153 | * @param $className |
||
154 | * @return static |
||
155 | */ |
||
156 | 5 | public static function restore($className) |
|
157 | { |
||
158 | 5 | $instance = new static(null, 3); |
|
159 | 5 | $instance->setTargetClassName($className); |
|
160 | 5 | return $instance; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $className |
||
165 | * @return static |
||
166 | */ |
||
167 | 2 | public static function restoreCollection($className) |
|
168 | { |
||
169 | 2 | $instance = new static(null, 4); |
|
170 | 2 | $instance->setTargetClassName($className); |
|
171 | 2 | return $instance; |
|
172 | } |
||
173 | |||
174 | 5 | protected function getReverseRepresentation($projection) |
|
175 | { |
||
176 | 5 | switch ($this->strategy) { |
|
177 | 5 | case 3: |
|
178 | 3 | return $this->getOneReverseRepresentation($projection); |
|
179 | 2 | case 4: |
|
180 | 2 | return $this->getCollectionReverseRepresentation($projection); |
|
181 | default: |
||
182 | throw new \Exception('Reverse representation strategy not defined'); |
||
183 | } |
||
184 | |||
185 | } |
||
186 | |||
187 | 5 | View Code Duplication | protected function getOneReverseRepresentation($projection) |
188 | { |
||
189 | 5 | $rules = $this->rules(); |
|
190 | 5 | if (empty($rules)) { |
|
191 | throw new \Exception("There are rules specified in " . static::class . " representer"); |
||
192 | } |
||
193 | |||
194 | 5 | $target = new $this->targetClassName(); |
|
195 | |||
196 | 5 | foreach ($rules as $rule) { |
|
197 | /** @var $rule PropertyRule */ |
||
198 | 5 | $resultArray = $rule->reverseCompile($projection); |
|
199 | 5 | reset($resultArray); |
|
200 | 5 | $key = key($resultArray); |
|
201 | 5 | $value = $resultArray[$key]; |
|
202 | |||
203 | 5 | $target->$key = $value; |
|
204 | 5 | } |
|
205 | |||
206 | 5 | return $target; |
|
207 | } |
||
208 | |||
209 | 2 | protected function getCollectionReverseRepresentation($projectionArray) |
|
226 | } |
||
227 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.