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 |
||
32 | class LoggerSubscriber extends AbstractFormatterSubscriber |
||
33 | { |
||
34 | /** |
||
35 | * @var LoggerInterface |
||
36 | */ |
||
37 | private $logger; |
||
38 | |||
39 | /** |
||
40 | * @param LoggerInterface $logger |
||
41 | * @param FormatterInterface|null $formatter |
||
42 | * @param TimerInterface|null $timer |
||
43 | */ |
||
44 | 63 | public function __construct( |
|
53 | |||
54 | /** |
||
55 | * @return LoggerInterface |
||
56 | */ |
||
57 | 18 | public function getLogger() |
|
61 | |||
62 | /** |
||
63 | * @param RequestCreatedEvent $event |
||
64 | */ |
||
65 | 18 | public function onRequestCreated(RequestCreatedEvent $event) |
|
69 | |||
70 | /** |
||
71 | * @param RequestSentEvent $event |
||
72 | */ |
||
73 | 9 | public function onRequestSent(RequestSentEvent $event) |
|
77 | |||
78 | /** |
||
79 | * @param RequestErroredEvent $event |
||
80 | */ |
||
81 | 9 | public function onRequestErrored(RequestErroredEvent $event) |
|
85 | |||
86 | /** |
||
87 | * @param MultiRequestCreatedEvent $event |
||
88 | */ |
||
89 | 18 | public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
96 | |||
97 | /** |
||
98 | * @param MultiRequestSentEvent $event |
||
99 | */ |
||
100 | 9 | View Code Duplication | public function onMultiRequestSent(MultiRequestSentEvent $event) |
109 | |||
110 | /** |
||
111 | * @param MultiRequestErroredEvent $event |
||
112 | */ |
||
113 | 9 | public function onMultiResponseErrored(MultiRequestErroredEvent $event) |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 9 | View Code Duplication | public static function getSubscribedEvents() |
134 | |||
135 | /** |
||
136 | * @param HttpAdapterInterface $httpAdapter |
||
137 | * @param InternalRequestInterface $request |
||
138 | * @param ResponseInterface $response |
||
139 | * |
||
140 | * @return InternalRequestInterface |
||
141 | */ |
||
142 | 18 | private function debug( |
|
165 | |||
166 | /** |
||
167 | * @param HttpAdapterInterface $httpAdapter |
||
168 | * @param HttpAdapterException $exception |
||
169 | * |
||
170 | * @return InternalRequestInterface |
||
171 | */ |
||
172 | 18 | private function error(HttpAdapterInterface $httpAdapter, HttpAdapterException $exception) |
|
194 | } |
||
195 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: