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 |
||
51 | abstract class EventDispatchingHttpCache extends HttpCache |
||
52 | { |
||
53 | /** |
||
54 | * Option for the "fos_default_subscribers" that enables all subscribers. |
||
55 | * |
||
56 | * See class phpdoc for examples. |
||
57 | */ |
||
58 | const SUBSCRIBER_ALL = -1; // Equals to ~0 |
||
59 | |||
60 | /** |
||
61 | * Option for the "fos_default_subscribers" that enables no subscribers. |
||
62 | * |
||
63 | * See class phpdoc for examples. |
||
64 | */ |
||
65 | const SUBSCRIBER_NONE = 0; |
||
66 | |||
67 | /** |
||
68 | * Option for the "fos_default_subscribers" that enables the user context subscriber. |
||
69 | * |
||
70 | * See class phpdoc for examples. |
||
71 | */ |
||
72 | const SUBSCRIBER_USER_CONTEXT = 1; |
||
73 | |||
74 | /** |
||
75 | * Option for the "fos_default_subscribers" that enables the purge subscriber. |
||
76 | * |
||
77 | * See class phpdoc for examples. |
||
78 | */ |
||
79 | const SUBSCRIBER_PURGE = 2; |
||
80 | |||
81 | /** |
||
82 | * Option for the "fos_default_subscribers" that enables the refresh subscriber. |
||
83 | * |
||
84 | * See class phpdoc for examples. |
||
85 | */ |
||
86 | const SUBSCRIBER_REFRESH = 4; |
||
87 | |||
88 | /** |
||
89 | * @var EventDispatcherInterface |
||
90 | */ |
||
91 | private $eventDispatcher; |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | * |
||
96 | * Adding the default subscribers to the event dispatcher. |
||
97 | */ |
||
98 | public function __construct(HttpKernelInterface $kernel, $cacheDir = null) |
||
99 | { |
||
100 | parent::__construct($kernel, $cacheDir); |
||
101 | |||
102 | foreach ($this->getDefaultSubscribers() as $subscriber) { |
||
103 | $this->addSubscriber($subscriber); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | * |
||
110 | * Adding the Events::PRE_HANDLE event. |
||
111 | */ |
||
112 | 2 | View Code Duplication | public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) |
|
|||
113 | { |
||
114 | 2 | if ($this->getEventDispatcher()->hasListeners(Events::PRE_HANDLE)) { |
|
115 | 2 | $event = new CacheEvent($this, $request); |
|
116 | 2 | $this->getEventDispatcher()->dispatch(Events::PRE_HANDLE, $event); |
|
117 | 2 | if ($event->getResponse()) { |
|
118 | 1 | return $event->getResponse(); |
|
119 | } |
||
120 | } |
||
121 | |||
122 | 1 | return parent::handle($request, $type, $catch); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Add a cache kernel event subscriber that listens to events listed in |
||
127 | * FOS\HttpCache\SymfonyCache\Event. |
||
128 | * |
||
129 | * @param EventSubscriberInterface $subscriber |
||
130 | */ |
||
131 | 2 | public function addSubscriber(EventSubscriberInterface $subscriber) |
|
135 | |||
136 | /** |
||
137 | * Made public to allow event subscribers to do refresh operations. |
||
138 | * |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function fetch(Request $request, $catch = false) |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | * |
||
149 | * Adding the Events::PRE_INVALIDATE event. |
||
150 | */ |
||
151 | View Code Duplication | protected function invalidate(Request $request, $catch = false) |
|
163 | |||
164 | /** |
||
165 | * Return the subscribers to be added to the event dispatcher, according to the |
||
166 | * fos_default_subscribers option in `getOptions()`. |
||
167 | * |
||
168 | * Override this method if you want to customize subscribers or add your own subscribers. |
||
169 | * |
||
170 | * @return EventSubscriberInterface[] |
||
171 | */ |
||
172 | 4 | protected function getDefaultSubscribers() |
|
189 | |||
190 | /** |
||
191 | * Get event dispatcher. |
||
192 | * |
||
193 | * @return EventDispatcherInterface |
||
194 | */ |
||
195 | 2 | private function getEventDispatcher() |
|
203 | } |
||
204 |
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.