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:
Complex classes like CacheMiddleware often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CacheMiddleware, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class CacheMiddleware |
||
30 | { |
||
31 | /** |
||
32 | * PSR-6 cache item pool. |
||
33 | * |
||
34 | * @var CacheItemPool |
||
35 | */ |
||
36 | private $cachePool; |
||
37 | |||
38 | /** |
||
39 | * Time-to-live in seconds. |
||
40 | * |
||
41 | * @var integer |
||
42 | */ |
||
43 | private $cacheTtl; |
||
44 | |||
45 | /** |
||
46 | * If set, only queries matching this path |
||
47 | * @var null|string|array |
||
48 | */ |
||
49 | private $includedPath; |
||
50 | |||
51 | /** |
||
52 | * Queries matching |
||
53 | * @var null|string|array |
||
54 | */ |
||
55 | private $excludedPath; |
||
56 | |||
57 | /** |
||
58 | * Only cache the request if it matches one of those methods. |
||
59 | * @var string[] |
||
60 | */ |
||
61 | private $methods; |
||
62 | |||
63 | /** |
||
64 | * Only cache the request if it matches one of those status codes. |
||
65 | * @var int[] |
||
66 | */ |
||
67 | private $statusCode; |
||
|
|||
68 | |||
69 | /** |
||
70 | * @var array|string|null |
||
71 | */ |
||
72 | private $includedQuery; |
||
73 | |||
74 | /** |
||
75 | * @var array|string|null |
||
76 | */ |
||
77 | private $excludedQuery; |
||
78 | |||
79 | /** |
||
80 | * @var array|string|null |
||
81 | */ |
||
82 | private $ignoredQuery; |
||
83 | |||
84 | /** |
||
85 | * @var boolean |
||
86 | */ |
||
87 | private $headers; |
||
88 | |||
89 | /** |
||
90 | * @param array $data Constructor dependencies and options. |
||
91 | */ |
||
92 | public function __construct(array $data) |
||
111 | |||
112 | /** |
||
113 | * Default middleware options. |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | public function defaults() |
||
135 | |||
136 | /** |
||
137 | * Load a route content from path's cache. |
||
138 | * |
||
139 | * This method is as dumb / simple as possible. |
||
140 | * It does not rely on any sort of settings / configuration. |
||
141 | * Simply: if the cache for the route exists, it will be used to display the page. |
||
142 | * The `$next` callback will not be called, therefore stopping the middleware stack. |
||
143 | * |
||
144 | * To generate the cache used in this middleware, |
||
145 | * @see \Charcoal\App\Middleware\CacheGeneratorMiddleware. |
||
146 | * |
||
147 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
148 | * @param ResponseInterface $response The PSR-7 HTTP response. |
||
149 | * @param callable $next The next middleware callable in the stack. |
||
150 | * @return ResponseInterface |
||
151 | */ |
||
152 | public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) |
||
205 | |||
206 | /** |
||
207 | * @param string $path The query path (route). |
||
208 | * @param array $queryParams The query parameters. |
||
209 | * @return string |
||
210 | */ |
||
211 | private function cacheKey($path, array $queryParams) |
||
221 | |||
222 | /** |
||
223 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
224 | * @param ResponseInterface $response The PSR-7 HTTP response. |
||
225 | * @return boolean |
||
226 | */ |
||
227 | private function isActive(RequestInterface $request, ResponseInterface $response) |
||
239 | |||
240 | /** |
||
241 | * @param string $path The request path (route) to verify. |
||
242 | * @return boolean |
||
243 | */ |
||
244 | View Code Duplication | private function isPathIncluded($path) |
|
267 | |||
268 | /** |
||
269 | * @param string $path The request path (route) to verify. |
||
270 | * @return boolean |
||
271 | */ |
||
272 | View Code Duplication | private function isPathExcluded($path) |
|
295 | |||
296 | /** |
||
297 | * @param array $queryParams The query parameters. |
||
298 | * @return boolean |
||
299 | */ |
||
300 | View Code Duplication | private function isQueryIncluded(array $queryParams) |
|
316 | |||
317 | /** |
||
318 | * @param array $queryParams The query parameters. |
||
319 | * @return boolean |
||
320 | */ |
||
321 | View Code Duplication | private function isQueryExcluded(array $queryParams) |
|
335 | |||
336 | /** |
||
337 | * @param array $queryParams The query parameters. |
||
338 | * @return array |
||
339 | */ |
||
340 | private function parseIgnoredParams(array $queryParams) |
||
367 | } |
||
368 |
This check marks private properties in classes that are never used. Those properties can be removed.