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 |
||
26 | class PurgeListener extends AccessControlledListener |
||
27 | { |
||
28 | const DEFAULT_PURGE_METHOD = 'PURGE'; |
||
29 | const DEFAULT_PURGE_TAGS_HEADER = 'X-Cache-Tags'; |
||
30 | |||
31 | /** |
||
32 | * The purge method to use. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $purgeMethod; |
||
37 | |||
38 | /** |
||
39 | * The purge tags header to use. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | private $purgeTagsHeader; |
||
44 | |||
45 | /** |
||
46 | 8 | * When creating the purge listener, you can configure an additional option. |
|
47 | * |
||
48 | 8 | * - purge_method: HTTP method that identifies purge requests. |
|
49 | * - purge_tags_header: HTTP header that contains cache tags to invalidate. |
||
50 | 6 | * |
|
51 | 6 | * @param array $options Options to overwrite the default options |
|
52 | * |
||
53 | * @throws \InvalidArgumentException if unknown keys are found in $options |
||
54 | * |
||
55 | * @see AccessControlledListener::__construct |
||
56 | 1 | */ |
|
57 | public function __construct(array $options = []) |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public static function getSubscribedEvents() |
||
74 | 1 | ||
75 | /** |
||
76 | * Look at unsafe requests and handle purge requests. |
||
77 | 4 | * |
|
78 | 2 | * Prevents access when the request comes from a non-authorized client. |
|
79 | * |
||
80 | 2 | * @param CacheEvent $event |
|
81 | */ |
||
82 | public function handlePurge(CacheEvent $event) |
||
116 | |||
117 | /** |
||
118 | * Add the purge_method option. |
||
119 | * |
||
120 | * @return OptionsResolver |
||
121 | */ |
||
122 | View Code Duplication | protected function getOptionsResolver() |
|
132 | } |
||
133 |
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.