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 HistorySubscriber extends AbstractTimerSubscriber |
||
29 | { |
||
30 | /** |
||
31 | * @var JournalInterface |
||
32 | */ |
||
33 | private $journal; |
||
34 | |||
35 | /** |
||
36 | * @param JournalInterface|null $journal |
||
37 | * @param TimerInterface|null $timer |
||
38 | */ |
||
39 | 45 | public function __construct(JournalInterface $journal = null, TimerInterface $timer = null) |
|
45 | |||
46 | /** |
||
47 | * @return JournalInterface |
||
48 | */ |
||
49 | 18 | public function getJournal() |
|
53 | |||
54 | /** |
||
55 | * @param RequestCreatedEvent $event |
||
56 | */ |
||
57 | 9 | public function onRequestCreated(RequestCreatedEvent $event) |
|
61 | |||
62 | /** |
||
63 | * @param RequestSentEvent $event |
||
64 | */ |
||
65 | 9 | public function onRequestSent(RequestSentEvent $event) |
|
69 | |||
70 | /** |
||
71 | * @param MultiRequestCreatedEvent $event |
||
72 | */ |
||
73 | 9 | public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
80 | |||
81 | /** |
||
82 | * @param MultiRequestSentEvent $event |
||
83 | */ |
||
84 | 9 | View Code Duplication | public function onMultiRequestSent(MultiRequestSentEvent $event) |
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 9 | public static function getSubscribedEvents() |
|
106 | |||
107 | /** |
||
108 | * @param InternalRequestInterface $internalRequest |
||
109 | * @param ResponseInterface $response |
||
110 | * |
||
111 | * @return InternalRequestInterface |
||
112 | */ |
||
113 | 18 | private function record(InternalRequestInterface $internalRequest, ResponseInterface $response) |
|
120 | } |
||
121 |
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: