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 |
||
28 | class CookieSubscriber implements EventSubscriberInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var CookieJarInterface |
||
32 | */ |
||
33 | private $cookieJar; |
||
34 | |||
35 | /** |
||
36 | * @param CookieJarInterface|null $cookieJar |
||
37 | */ |
||
38 | 72 | public function __construct(CookieJarInterface $cookieJar = null) |
|
42 | |||
43 | /** |
||
44 | * @return CookieJarInterface |
||
45 | */ |
||
46 | 9 | public function getCookieJar() |
|
50 | |||
51 | /** |
||
52 | * @param RequestCreatedEvent $event |
||
53 | */ |
||
54 | 9 | public function onRequestCreated(RequestCreatedEvent $event) |
|
58 | |||
59 | /** |
||
60 | * @param RequestSentEvent $event |
||
61 | */ |
||
62 | 9 | public function onRequestSent(RequestSentEvent $event) |
|
66 | |||
67 | /** |
||
68 | * @param RequestErroredEvent $event |
||
69 | */ |
||
70 | 9 | public function onRequestErrored(RequestErroredEvent $event) |
|
76 | |||
77 | /** |
||
78 | * @param MultiRequestCreatedEvent $event |
||
79 | */ |
||
80 | 9 | public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
87 | |||
88 | /** |
||
89 | * @param MultiRequestSentEvent $event |
||
90 | */ |
||
91 | 9 | public function onMultiRequestSent(MultiRequestSentEvent $event) |
|
97 | |||
98 | /** |
||
99 | * @param MultiRequestErroredEvent $event |
||
100 | */ |
||
101 | 9 | public function onMultiResponseErrored(MultiRequestErroredEvent $event) |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 9 | View Code Duplication | public static function getSubscribedEvents() |
124 | } |
||
125 |
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: