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 |
||
29 | class StopwatchSubscriber implements EventSubscriberInterface |
||
30 | { |
||
31 | /** |
||
32 | * @var Stopwatch |
||
33 | */ |
||
34 | private $stopwatch; |
||
35 | |||
36 | /** |
||
37 | * @param Stopwatch $stopwatch |
||
38 | */ |
||
39 | 72 | public function __construct(Stopwatch $stopwatch) |
|
43 | |||
44 | /** |
||
45 | * @return Stopwatch |
||
46 | */ |
||
47 | 9 | public function getStopwatch() |
|
51 | |||
52 | /** |
||
53 | * @param RequestCreatedEvent $event |
||
54 | */ |
||
55 | 9 | public function onRequestCreated(RequestCreatedEvent $event) |
|
59 | |||
60 | /** |
||
61 | * @param RequestSentEvent $event |
||
62 | */ |
||
63 | 9 | public function onRequestSent(RequestSentEvent $event) |
|
69 | |||
70 | /** |
||
71 | * @param RequestErroredEvent $event |
||
72 | */ |
||
73 | 9 | public function onRequestErrored(RequestErroredEvent $event) |
|
77 | |||
78 | /** |
||
79 | * @param MultiRequestCreatedEvent $event |
||
80 | */ |
||
81 | 9 | public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
87 | |||
88 | /** |
||
89 | * @param MultiRequestSentEvent $event |
||
90 | */ |
||
91 | 9 | public function onMultiRequestSent(MultiRequestSentEvent $event) |
|
99 | |||
100 | /** |
||
101 | * @param MultiRequestErroredEvent $event |
||
102 | */ |
||
103 | 9 | public function onMultiResponseErrored(MultiRequestErroredEvent $event) |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 9 | View Code Duplication | public static function getSubscribedEvents() |
132 | |||
133 | /** |
||
134 | * @param HttpAdapterInterface $httpAdapter |
||
135 | * @param InternalRequestInterface $internalRequest |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | 54 | private function getStopwatchName(HttpAdapterInterface $httpAdapter, InternalRequestInterface $internalRequest) |
|
143 | } |
||
144 |
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: