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 |
||
12 | class PartialCache |
||
13 | { |
||
14 | /** |
||
15 | * @var \Illuminate\Contracts\View\Factory |
||
16 | */ |
||
17 | protected $view; |
||
18 | |||
19 | /** |
||
20 | * @var \Illuminate\Contracts\Cache\Repository |
||
21 | */ |
||
22 | protected $cache; |
||
23 | |||
24 | /** |
||
25 | * @var \Illuminate\Contracts\Cache\Factory |
||
26 | */ |
||
27 | protected $cacheManager; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $cacheKey; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $cacheIsTaggable; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $enabled; |
||
43 | |||
44 | /** |
||
45 | * @param \Illuminate\Contracts\View\Factory $view |
||
46 | * @param \Illuminate\Contracts\Cache\Repository $cache |
||
47 | * @param \Illuminate\Contracts\Cache\Factory $cacheManager |
||
48 | * @param \Illuminate\Contracts\Config\Repository $config |
||
49 | */ |
||
50 | public function __construct(View $view, Cache $cache, CacheManager $cacheManager, Config $config) |
||
60 | |||
61 | /** |
||
62 | * Cache a view. If minutes are null, the view is cached forever. |
||
63 | * |
||
64 | * @param array $data |
||
65 | * @param string $view |
||
66 | * @param array $mergeData |
||
67 | * @param int $minutes |
||
68 | * @param string $key |
||
69 | * @param string|array $tag |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function cache($data, $view, $mergeData = null, $minutes = null, $key = null, $tag = null) |
||
105 | |||
106 | /** |
||
107 | * Create a key name for the cached view. |
||
108 | * |
||
109 | * @param string $view |
||
110 | * @param string $key |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function getCacheKeyForView($view, $key = null) |
||
124 | |||
125 | /** |
||
126 | * Forget a rendered view. |
||
127 | * |
||
128 | * @param string $view |
||
129 | * @param string $key |
||
130 | * @param null|string|array $tag |
||
131 | */ |
||
132 | public function forget($view, $key = null, $tag = null) |
||
143 | |||
144 | /** |
||
145 | * Empty all views linked to a tag or the complete partial cache. |
||
146 | * Note: Only supported by Taggable cache drivers. |
||
147 | * |
||
148 | * @param string $tag |
||
149 | * |
||
150 | * @throws \Spatie\PartialCache\Exceptions\MethodNotSupportedException |
||
151 | */ |
||
152 | public function flush($tag = null) |
||
162 | |||
163 | /** |
||
164 | * Render a view. |
||
165 | * |
||
166 | * @param string $view |
||
167 | * @param array $data |
||
168 | * @param array $mergeData |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | protected function renderView($view, $data, $mergeData) |
||
181 | |||
182 | /** |
||
183 | * Constructs tag array |
||
184 | * |
||
185 | * @param null|string|array $tag |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | protected function getTags($tag = null) |
||
203 | } |
||
204 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.