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 |
||
28 | class CacheControlListener extends AbstractRuleListener implements EventSubscriberInterface |
||
29 | { |
||
30 | /** |
||
31 | * Whether to skip this response and not set any cache headers. |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | private $skip = false; |
||
36 | |||
37 | /** |
||
38 | * Cache control directives directly supported by Response. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $supportedDirectives = array( |
||
43 | 'max_age' => true, |
||
44 | 's_maxage' => true, |
||
45 | 'private' => true, |
||
46 | 'public' => true, |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * If not empty, add a debug header with that name to all responses, |
||
51 | * telling the cache proxy to add debug output. |
||
52 | * |
||
53 | * @var string|bool Name of the header or false to add no header |
||
54 | */ |
||
55 | private $debugHeader; |
||
56 | |||
57 | /** |
||
58 | * @param string|bool $debugHeader Header to set to trigger debugging, or false to send no header |
||
59 | */ |
||
60 | 29 | public function __construct($debugHeader = false) |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 23 | public static function getSubscribedEvents() |
|
74 | |||
75 | /** |
||
76 | * Set whether to skip this response completely. |
||
77 | * |
||
78 | * This can be called when other parts of the application took care of all |
||
79 | * cache headers. No attempt to merge cache headers is made anymore. |
||
80 | * |
||
81 | * The debug header is still added if configured. |
||
82 | * |
||
83 | * @param bool $skip |
||
84 | */ |
||
85 | 1 | public function setSkip($skip = true) |
|
89 | |||
90 | /** |
||
91 | * Apply the header rules if the request matches. |
||
92 | * |
||
93 | * @param FilterResponseEvent $event |
||
94 | */ |
||
95 | 29 | public function onKernelResponse(FilterResponseEvent $event) |
|
145 | |||
146 | /** |
||
147 | * Set cache headers on response. |
||
148 | * |
||
149 | * @param Response $response |
||
150 | * @param array $directives |
||
151 | * @param bool $overwrite Whether to keep existing cache headers or to overwrite them |
||
152 | */ |
||
153 | 9 | private function setCache(Response $response, array $directives, $overwrite) |
|
182 | |||
183 | /** |
||
184 | * Add extra cache control directives. |
||
185 | * |
||
186 | * @param Response $response |
||
187 | * @param array $controls |
||
188 | * @param bool $overwrite Whether to keep existing cache headers or to overwrite them |
||
189 | */ |
||
190 | 6 | private function setExtraCacheDirectives(Response $response, array $controls, $overwrite) |
|
213 | |||
214 | /** |
||
215 | * Decide whether to even look for matching rules with the current request. |
||
216 | * |
||
217 | * @param Request $request |
||
218 | * |
||
219 | * @return bool True if the request is safe and headers can be set |
||
220 | */ |
||
221 | 28 | protected function isRequestSafe(Request $request) |
|
225 | } |
||
226 |
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.