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 Anomaly\Streams\Platform\View; |
||
21 | class ViewComposer |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Runtime cache. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $cache = []; |
||
30 | |||
31 | /** |
||
32 | * The view factory. |
||
33 | * |
||
34 | * @var Factory |
||
35 | */ |
||
36 | protected $view; |
||
37 | |||
38 | /** |
||
39 | * The agent utility. |
||
40 | * |
||
41 | * @var Mobile_Detect |
||
42 | */ |
||
43 | protected $agent; |
||
44 | |||
45 | /** |
||
46 | * The event dispatcher. |
||
47 | * |
||
48 | * @var Dispatcher |
||
49 | */ |
||
50 | protected $events; |
||
51 | |||
52 | /** |
||
53 | * The current theme. |
||
54 | * |
||
55 | * @var Theme|null |
||
56 | */ |
||
57 | protected $theme; |
||
58 | |||
59 | /** |
||
60 | * The active module. |
||
61 | * |
||
62 | * @var Module|null |
||
63 | */ |
||
64 | protected $module; |
||
65 | |||
66 | /** |
||
67 | * The addon collection. |
||
68 | * |
||
69 | * @var AddonCollection |
||
70 | */ |
||
71 | protected $addons; |
||
72 | |||
73 | /** |
||
74 | * The request object. |
||
75 | * |
||
76 | * @var Request |
||
77 | */ |
||
78 | protected $request; |
||
79 | |||
80 | /** |
||
81 | * The view overrides collection. |
||
82 | * |
||
83 | * @var ViewOverrides |
||
84 | */ |
||
85 | protected $overrides; |
||
86 | |||
87 | /** |
||
88 | * The view mobile overrides. |
||
89 | * |
||
90 | * @var ViewMobileOverrides |
||
91 | */ |
||
92 | protected $mobiles; |
||
93 | |||
94 | /** |
||
95 | * @param Factory $view |
||
96 | * @param Mobile_Detect $agent |
||
97 | * @param Dispatcher $events |
||
98 | * @param AddonCollection $addons |
||
99 | * @param Request $request |
||
100 | * @param ViewOverrides $overrides |
||
101 | * @param ViewMobileOverrides $mobiles |
||
102 | */ |
||
103 | function __construct( |
||
127 | |||
128 | /** |
||
129 | * Compose the view before rendering. |
||
130 | * |
||
131 | * @param View $view |
||
132 | * @return View |
||
133 | */ |
||
134 | public function compose(View $view) |
||
135 | { |
||
136 | if (!$this->theme || !env('APP_INSTALLED')) { |
||
137 | |||
138 | $this->events->fire(new ViewComposed($view)); |
||
139 | |||
140 | return $view; |
||
141 | } |
||
142 | |||
143 | $mobile = $this->mobiles->get($this->theme->getNamespace(), []); |
||
144 | $overrides = $this->overrides->get($this->theme->getNamespace(), []); |
||
145 | |||
146 | View Code Duplication | if ($this->mobile && $path = array_get($mobile, $view->getName(), null)) { |
|
147 | $view->setPath($path); |
||
148 | } elseif ($path = array_get($overrides, $view->getName(), null)) { |
||
149 | $view->setPath($path); |
||
150 | } |
||
151 | |||
152 | if ($this->module) { |
||
153 | |||
154 | $mobile = $this->mobiles->get($this->module->getNamespace(), []); |
||
155 | $overrides = $this->overrides->get($this->module->getNamespace(), []); |
||
156 | |||
157 | View Code Duplication | if ($this->mobile && $path = array_get($mobile, $view->getName(), null)) { |
|
158 | $view->setPath($path); |
||
159 | } elseif ($path = array_get($overrides, $view->getName(), null)) { |
||
160 | $view->setPath($path); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | if ($overload = $this->getOverloadPath($view)) { |
||
165 | $view->setPath($overload); |
||
166 | } |
||
167 | |||
168 | $this->events->fire(new ViewComposed($view)); |
||
169 | |||
170 | return $view; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Get the override view path. |
||
175 | * |
||
176 | * @param $view |
||
177 | * @return null|string |
||
178 | */ |
||
179 | public function getOverloadPath(View $view) |
||
256 | } |
||
257 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.