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 CacheControlListener 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 CacheControlListener, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class CacheControlListener implements EventSubscriberInterface |
||
30 | { |
||
31 | /** |
||
32 | * Whether to skip this response and not set any cache headers. |
||
33 | * |
||
34 | * @var bool |
||
35 | */ |
||
36 | private $skip = false; |
||
37 | |||
38 | /** |
||
39 | * Cache control directives directly supported by Response. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $supportedDirectives = [ |
||
44 | 'max_age' => true, |
||
45 | 's_maxage' => true, |
||
46 | 'private' => true, |
||
47 | 'public' => true, |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @var array List of arrays with RuleMatcherInterface, settings array |
||
52 | */ |
||
53 | private $rulesMap = []; |
||
54 | |||
55 | /** |
||
56 | * If not empty, add a debug header with that name to all responses, |
||
57 | * telling the cache proxy to add debug output. |
||
58 | * |
||
59 | * @var string|bool Name of the header or false to add no header |
||
60 | */ |
||
61 | private $debugHeader; |
||
62 | |||
63 | /** |
||
64 | * @param string|bool $debugHeader Header to set to trigger debugging, or false to send no header |
||
65 | */ |
||
66 | 36 | public function __construct($debugHeader = false) |
|
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 2 | public static function getSubscribedEvents() |
|
80 | |||
81 | /** |
||
82 | * Set whether to skip this response completely. |
||
83 | * |
||
84 | * This can be called when other parts of the application took care of all |
||
85 | * cache headers. No attempt to merge cache headers is made anymore. |
||
86 | * |
||
87 | * The debug header is still added if configured. |
||
88 | * |
||
89 | * @param bool $skip |
||
90 | */ |
||
91 | 1 | public function setSkip($skip = true) |
|
95 | |||
96 | /** |
||
97 | * Apply the header rules if the request matches. |
||
98 | * |
||
99 | * @param FilterResponseEvent $event |
||
100 | */ |
||
101 | 35 | public function onKernelResponse(FilterResponseEvent $event) |
|
154 | |||
155 | /** |
||
156 | * Add a rule matcher with a list of header directives to apply if the |
||
157 | * request and response are matched. |
||
158 | * |
||
159 | * @param RuleMatcherInterface $ruleMatcher The headers apply to request and response matched by this matcher |
||
160 | * @param array $settings An array of header configuration |
||
161 | */ |
||
162 | 35 | public function addRule( |
|
168 | |||
169 | /** |
||
170 | * Return the settings for the current request if any rule matches. |
||
171 | * |
||
172 | * @param Request $request |
||
173 | * |
||
174 | * @return array|false Settings to apply or false if no rule matched |
||
175 | */ |
||
176 | 25 | View Code Duplication | private function matchRule(Request $request, Response $response) |
186 | |||
187 | /** |
||
188 | * Set cache headers on response. |
||
189 | * |
||
190 | * @param Response $response |
||
191 | * @param array $directives |
||
192 | * @param bool $overwrite Whether to keep existing cache headers or to overwrite them |
||
193 | */ |
||
194 | 9 | private function setCache(Response $response, array $directives, $overwrite) |
|
223 | |||
224 | /** |
||
225 | * Add extra cache control directives on response. |
||
226 | * |
||
227 | * @param Response $response |
||
228 | * @param array $controls |
||
229 | * @param bool $overwrite Whether to keep existing cache headers or to overwrite them |
||
230 | */ |
||
231 | 7 | private function setExtraCacheDirectives(Response $response, array $controls, $overwrite) |
|
254 | } |
||
255 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.