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 namespace Sofa\Revisionable\Laravel; |
||
7 | class Presenter |
||
8 | { |
||
9 | /** |
||
10 | * Revisionable fields labels. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $labels = []; |
||
15 | |||
16 | /** |
||
17 | * Revision model. |
||
18 | * |
||
19 | * @var \Sofa\Revisionable\Laravel\Revision |
||
20 | */ |
||
21 | protected $revision; |
||
22 | |||
23 | /** |
||
24 | * Revisoned model. |
||
25 | * |
||
26 | * @var \Illuminate\Database\Eloquent\Model |
||
27 | */ |
||
28 | protected $revisioned; |
||
29 | |||
30 | /** |
||
31 | * Revisionable fields translated to real data. For example |
||
32 | * show related model's property instead of its raw id. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $passThrough = []; |
||
37 | |||
38 | /** |
||
39 | * Translate revision actions. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $actions = [ |
||
44 | 'created' => 'created', |
||
45 | 'updated' => 'updated', |
||
46 | 'deleted' => 'deleted', |
||
47 | 'restored' => 'restored', |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * Old version of revisioned model. |
||
52 | * |
||
53 | * @var \Illuminate\Database\Eloquent\Model |
||
54 | */ |
||
55 | protected $oldVersion; |
||
56 | |||
57 | /** |
||
58 | * New version of revisioned model. |
||
59 | * |
||
60 | * @var \Illuminate\Database\Eloquent\Model |
||
61 | */ |
||
62 | protected $newVersion; |
||
63 | |||
64 | /** |
||
65 | * HTML templates. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $templates = []; |
||
70 | |||
71 | /** |
||
72 | * Create a new revision presenter. |
||
73 | * |
||
74 | * @param \Sofa\Revisionable\Revision $revision |
||
75 | * @param \Illuminat\Database\Eloquent\Model $revisioned |
||
76 | */ |
||
77 | public function __construct(Revision $revision, Model $revisioned) |
||
78 | { |
||
79 | $this->revision = $revision; |
||
80 | $this->revisioned = $revisioned; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Present action field. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function action() |
||
89 | { |
||
90 | $action = $this->revision->action; |
||
|
|||
91 | |||
92 | return array_get($this->actions, $action, $action); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get custom label for revisioned field. |
||
97 | * |
||
98 | * @param string $key |
||
99 | * @return string |
||
100 | */ |
||
101 | public function label($key) |
||
105 | |||
106 | /** |
||
107 | * Get value from the revision. |
||
108 | * |
||
109 | * @param string $version |
||
110 | * @param string $key |
||
111 | * @return mixed |
||
112 | */ |
||
113 | public function getFromRevision($version, $key) |
||
114 | { |
||
115 | return ($this->isPassedThrough($key)) |
||
116 | ? $this->passThrough($version, $key) |
||
117 | : array_get($this->{$version}, $key); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Determine whether the value should be fetched from the relation. |
||
122 | * |
||
123 | * @param string $key |
||
124 | * @return boolean |
||
125 | */ |
||
126 | protected function isPassedThrough($key) |
||
130 | |||
131 | /** |
||
132 | * Get value from the relation. |
||
133 | * |
||
134 | * @param string $version |
||
135 | * @param string $key |
||
136 | * @return mixed |
||
137 | */ |
||
138 | protected function passThrough($version, $key) |
||
139 | { |
||
140 | $revisioned = $this->getVersion($version); |
||
141 | |||
142 | $needle = $this->passThrough[$key]; |
||
143 | |||
144 | return $this->dataGet($revisioned, $needle); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Get pass through value using dot notation. |
||
149 | * |
||
150 | * @param mixed $target |
||
151 | * @param string $key |
||
152 | * @return mixed |
||
153 | */ |
||
154 | protected function dataGet($target, $key) |
||
155 | { |
||
156 | foreach (explode('.', $key) as $segment) { |
||
157 | if ($target instanceof Revisionable) { |
||
158 | $target = $this->passThroughRevisionable($target, $segment); |
||
159 | |||
160 | } elseif ($target instanceof Presenter || $target instanceof Revision) { |
||
161 | $target = $this->passThroughRevision($target, $segment); |
||
162 | |||
163 | } elseif ($target instanceof Model) { |
||
164 | $target = $this->passThroughModel($target, $segment); |
||
165 | |||
166 | } else { |
||
167 | $target = null; |
||
168 | } |
||
169 | |||
170 | if (!$target) { |
||
171 | return; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | return $target; |
||
176 | } |
||
177 | |||
178 | protected function passThroughRevisionable(Revisionable $revisionable, $key) |
||
195 | |||
196 | /** |
||
197 | * Get pass through value from another revision. |
||
198 | * |
||
199 | * @param \Sofa\Revisionable\Revision|\Sofa\Revisionable\Laravel\Presenter $revision |
||
200 | * @param string $key |
||
201 | * @return mixed |
||
202 | */ |
||
203 | protected function passThroughRevision($revision, $key) |
||
204 | { |
||
205 | $action = $revision->getAttribute('action'); |
||
206 | |||
207 | // @todo what about restored??? |
||
208 | if (in_array($action, ['created', 'updated'])) { |
||
209 | return $revision->new($key); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Get pass through value from the Eloquent model. |
||
215 | * |
||
216 | * @param \Illuminate\Database\Eloquent\Model $model |
||
217 | * @param string $key |
||
218 | * @return mixed |
||
219 | */ |
||
220 | protected function passThroughModel(Model $model, $key) |
||
224 | |||
225 | /** |
||
226 | * Get revisioned model with appropriate attributes. |
||
227 | * |
||
228 | * @return \Illuminate\Database\Eloquent\Model |
||
229 | */ |
||
230 | protected function getVersion($version) |
||
231 | { |
||
232 | if (!$this->{$version.'Version'}) { |
||
233 | $revisioned = get_class($this->revisioned); |
||
234 | |||
235 | $revision = new $revisioned; |
||
236 | $revision->setRawAttributes($this->{$version}); |
||
237 | |||
238 | $this->{$version.'Version'} = $revision; |
||
239 | } |
||
240 | |||
241 | return $this->{$version.'Version'}; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Decorate revision model or array/collection of models. |
||
246 | * |
||
247 | * @param mixed $revision |
||
248 | * @param \Illuminate\Database\Eloquent\Model $revisioned |
||
249 | * @return mixed |
||
250 | * |
||
251 | * @throws \InvalidArgumentException |
||
252 | */ |
||
253 | public static function make($revision, $revisioned) |
||
254 | { |
||
255 | if (is_array($revision)) { |
||
256 | return static::makeArray($revision, $revisioned); |
||
257 | } |
||
258 | |||
259 | if ($revision instanceof Collection) { |
||
260 | return static::makeCollection($revision, $revisioned); |
||
261 | } |
||
262 | |||
263 | if (! $revision || $revision instanceof Model) { |
||
264 | return static::makeOne($revision, $revisioned); |
||
265 | } |
||
266 | |||
267 | throw new \InvalidArgumentException( |
||
268 | 'Presenter::make accepts array, collection or single resource, '.gettype($revision).' given.' |
||
269 | ); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Decorate Eloquent model. |
||
274 | * |
||
275 | * @param \Illuminate\Database\Eloquent\Model|null $revision |
||
276 | * @param \Illuminate\Database\Eloquent\Model $revisioned |
||
277 | * @return static |
||
278 | */ |
||
279 | public static function makeOne(Model $revision, Model $revisioned) |
||
283 | |||
284 | /** |
||
285 | * Decorate array of Eloquent models. |
||
286 | * |
||
287 | * @param array $revisions |
||
288 | * @param \Illuminate\Database\Eloquent\Model $revisioned |
||
289 | * @return array |
||
290 | */ |
||
291 | public static function makeArray(array $revisions, Model $revisioned) |
||
295 | |||
296 | /** |
||
297 | * Decorate collection of models. |
||
298 | * |
||
299 | * @param \Illuminate\Database\Eloquent\Collection $revisions |
||
300 | * @param \Illuminate\Database\Eloquent\Model $revisioned |
||
301 | * @return \Illuminate\Database\Eloquent\Collection |
||
302 | */ |
||
303 | public static function makeCollection(Collection $revisions, Model $revisioned) |
||
307 | |||
308 | /** |
||
309 | * Get callback for the array map. |
||
310 | * |
||
311 | * @return \Closure |
||
312 | */ |
||
313 | protected static function getMapCallback($revisioned) |
||
314 | { |
||
315 | // We need to pass the calling class to the closure scope |
||
324 | |||
325 | /** |
||
326 | * Handle dynamic methods calls. |
||
327 | * |
||
328 | * @param string $method |
||
329 | * @param array $parameters |
||
330 | * @return mixed |
||
331 | */ |
||
332 | public function __call($method, $parameters) |
||
342 | |||
343 | /** |
||
344 | * Pass dynamic property calls on to underlying revision model. |
||
345 | * |
||
346 | * @param string $property |
||
347 | * @return mixed |
||
348 | */ |
||
349 | public function __get($property) |
||
358 | } |
||
359 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.