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 |
||
44 | class InvalidationListener extends AbstractRuleListener implements EventSubscriberInterface |
||
45 | { |
||
46 | /** |
||
47 | * Cache manager. |
||
48 | * |
||
49 | * @var CacheManager |
||
50 | */ |
||
51 | private $cacheManager; |
||
52 | |||
53 | /** |
||
54 | * Router. |
||
55 | * |
||
56 | * @var UrlGeneratorInterface |
||
57 | */ |
||
58 | private $urlGenerator; |
||
59 | |||
60 | /** |
||
61 | * Router. |
||
62 | * |
||
63 | * @var ExpressionLanguage|null |
||
64 | */ |
||
65 | private $expressionLanguage; |
||
66 | |||
67 | /** |
||
68 | * @var RuleMatcherInterface |
||
69 | */ |
||
70 | private $mustInvalidateRule; |
||
71 | |||
72 | /** |
||
73 | * Constructor. |
||
74 | */ |
||
75 | View Code Duplication | public function __construct( |
|
|
|||
76 | CacheManager $cacheManager, |
||
77 | UrlGeneratorInterface $urlGenerator, |
||
78 | RuleMatcherInterface $mustInvalidateRule, |
||
79 | 33 | ExpressionLanguage $expressionLanguage = null |
|
80 | ) { |
||
81 | $this->cacheManager = $cacheManager; |
||
82 | $this->urlGenerator = $urlGenerator; |
||
83 | $this->expressionLanguage = $expressionLanguage; |
||
84 | $this->mustInvalidateRule = $mustInvalidateRule; |
||
85 | 33 | } |
|
86 | 33 | ||
87 | 33 | /** |
|
88 | 33 | * Apply invalidators and flush cache manager. |
|
89 | 33 | * |
|
90 | * On kernel.terminate: |
||
91 | * - see if any invalidators apply to the current request and, if so, add |
||
92 | * their routes to the cache manager; |
||
93 | * - flush the cache manager in order to send invalidation requests to the |
||
94 | * HTTP cache. |
||
95 | */ |
||
96 | public function onKernelTerminate(InvalidationTerminateEvent $event) |
||
113 | |||
114 | /** |
||
115 | * Flush cache manager when kernel exception occurs. |
||
116 | 23 | */ |
|
117 | public function onKernelException() |
||
126 | |||
127 | /** |
||
128 | * Flush cache manager when console terminates or errors. |
||
129 | 1 | * |
|
130 | * @throws ExceptionCollection If an exception occurs during flush |
||
131 | */ |
||
132 | public function onConsoleTerminate(ConsoleEvent $event) |
||
140 | 8 | ||
141 | 7 | /** |
|
142 | * {@inheritdoc} |
||
143 | 8 | */ |
|
144 | public static function getSubscribedEvents() |
||
152 | 2 | ||
153 | 2 | /** |
|
154 | * Handle the invalidation annotations and configured invalidators. |
||
155 | */ |
||
156 | private function handleInvalidation(Request $request, Response $response) |
||
187 | |||
188 | 1 | /** |
|
189 | * Invalidate paths from annotations. |
||
190 | * |
||
191 | 4 | * @param array|InvalidatePath[] $pathConfigurations |
|
192 | */ |
||
193 | 4 | private function invalidatePaths(array $pathConfigurations) |
|
201 | |||
202 | 4 | /** |
|
203 | 4 | * Invalidate routes from annotations. |
|
204 | 4 | * |
|
205 | * @param array|InvalidateRoute[] $routes |
||
206 | */ |
||
207 | 4 | private function invalidateRoutes(array $routes, Request $request) |
|
230 | |||
231 | 2 | View Code Duplication | private function getExpressionLanguage(): ExpressionLanguage |
243 | } |
||
244 |
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.