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 |
||
33 | class Symfony extends HttpProxyClient implements PurgeCapable, RefreshCapable, TagCapable |
||
34 | 4 | { |
|
35 | const HTTP_METHOD_REFRESH = 'GET'; |
||
36 | 4 | ||
37 | /** |
||
38 | 4 | * @var array |
|
39 | */ |
||
40 | private $queue; |
||
41 | |||
42 | /** |
||
43 | * @var HttpCacheAwareKernelInterface |
||
44 | 3 | */ |
|
45 | private $kernel; |
||
46 | 3 | ||
47 | 3 | /** |
|
48 | * Additional parameter for (optional) kernel. |
||
49 | 3 | * |
|
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 7 | public function __construct( |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public function purge($url, array $headers = []) |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | View Code Duplication | public function refresh($url, array $headers = []) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | protected function configureOptions() |
||
88 | { |
||
89 | $resolver = parent::configureOptions(); |
||
90 | $resolver->setDefaults([ |
||
91 | 'purge_method' => PurgeListener::DEFAULT_PURGE_METHOD, |
||
92 | 'tags_method' => PurgeTagsListener::DEFAULT_TAGS_METHOD, |
||
93 | 'tags_header' => PurgeTagsListener::DEFAULT_TAGS_HEADER, |
||
94 | 'header_length' => 7500, |
||
95 | 'enable_kernel_routing' => false, |
||
96 | ]); |
||
97 | $resolver->setAllowedTypes('purge_method', 'string'); |
||
98 | $resolver->setAllowedTypes('tags_method', 'string'); |
||
99 | $resolver->setAllowedTypes('tags_header', 'string'); |
||
100 | $resolver->setAllowedTypes('header_length', 'int'); |
||
101 | $resolver->setAllowedTypes('enable_kernel_routing', 'boolean'); |
||
102 | |||
103 | return $resolver; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function flush() |
||
110 | { |
||
111 | if (!$this->isDirectRoutingEnabled()) { |
||
112 | return parent::flush(); |
||
113 | } |
||
114 | |||
115 | $exceptions = new ExceptionCollection(); |
||
116 | |||
117 | foreach ($this->queue as $request) { |
||
118 | try { |
||
119 | $this->kernel->getHttpCache() |
||
120 | ->handle($request, HttpCacheAwareKernelInterface::MASTER_REQUEST); |
||
121 | } catch (\Exception $e) { |
||
122 | $exceptions->add($e); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | if (count($exceptions)) { |
||
127 | throw $exceptions; |
||
128 | } |
||
129 | |||
130 | return count($this->queue); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Remove/Expire cache objects based on cache tags. |
||
135 | * |
||
136 | * @param array $tags Tags that should be removed/expired from the cache |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function invalidateTags(array $tags) |
||
141 | { |
||
142 | $escapedTags = $this->escapeTags($tags); |
||
143 | |||
144 | $chunkSize = $this->determineTagsPerHeader($escapedTags, ','); |
||
145 | |||
146 | foreach (array_chunk($escapedTags, $chunkSize) as $tagchunk) { |
||
147 | $this->queueRequest( |
||
148 | $this->options['tags_method'], |
||
149 | '/', |
||
150 | [$this->options['tags_header'] => implode(',', $tagchunk)], |
||
151 | false |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | protected function queueRequest($method, $url, array $headers, $validateHost = true) |
||
162 | { |
||
163 | if (!$this->isDirectRoutingEnabled()) { |
||
164 | return parent::queueRequest($method, $url, $headers, $validateHost); |
||
165 | } |
||
166 | |||
167 | $request = Request::create((string) $url, $method); |
||
168 | $request->headers->replace($headers); |
||
169 | |||
170 | $this->queue[sha1((string) $request)] = $request; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return bool |
||
175 | */ |
||
176 | protected function isDirectRoutingEnabled() |
||
186 | } |
||
187 |
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.