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 |
||
| 27 | class CacheSubscriber implements EventSubscriberInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var CacheInterface |
||
| 31 | */ |
||
| 32 | private $cache; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param CacheInterface $cache |
||
| 36 | */ |
||
| 37 | 108 | public function __construct(CacheInterface $cache) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @return CacheInterface |
||
| 44 | */ |
||
| 45 | 9 | public function getCache() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @param CacheInterface $cache |
||
| 52 | */ |
||
| 53 | 108 | public function setCache(CacheInterface $cache) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param RequestCreatedEvent $event |
||
| 60 | */ |
||
| 61 | 27 | public function onRequestCreated(RequestCreatedEvent $event) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param RequestSentEvent $event |
||
| 75 | */ |
||
| 76 | 9 | public function onRequestSent(RequestSentEvent $event) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @param RequestErroredEvent $event |
||
| 83 | */ |
||
| 84 | 9 | public function onRequestErrored(RequestErroredEvent $event) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @param MultiRequestCreatedEvent $event |
||
| 91 | */ |
||
| 92 | 27 | public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param MultiRequestSentEvent $event |
||
| 109 | */ |
||
| 110 | 9 | public function onMultiRequestSent(MultiRequestSentEvent $event) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param MultiRequestErroredEvent $event |
||
| 119 | */ |
||
| 120 | 9 | public function onMultiRequestErrored(MultiRequestErroredEvent $event) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | 9 | View Code Duplication | public static function getSubscribedEvents() |
| 145 | } |
||
| 146 |
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: