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 CacheSubscriber implements EventSubscriberInterface |
||
30 | { |
||
31 | /** @var \Ivory\HttpAdapter\Event\Cache\CacheInterface */ |
||
32 | private $cache; |
||
33 | |||
34 | /** |
||
35 | * Creates a cache subscriber. |
||
36 | * |
||
37 | * @param \Ivory\HttpAdapter\Event\Cache\CacheInterface $cache The cache |
||
38 | */ |
||
39 | 228 | public function __construct(CacheInterface $cache) |
|
43 | |||
44 | /** |
||
45 | * Gets the cache. |
||
46 | * |
||
47 | * @return \Ivory\HttpAdapter\Event\Cache\CacheInterface The cache. |
||
48 | */ |
||
49 | 19 | public function getCache() |
|
53 | |||
54 | /** |
||
55 | * Sets the cache. |
||
56 | * |
||
57 | * @param \Ivory\HttpAdapter\Event\Cache\CacheInterface $cache The cache. |
||
58 | */ |
||
59 | 228 | public function setCache(CacheInterface $cache) |
|
63 | |||
64 | /** |
||
65 | * On request created. |
||
66 | * |
||
67 | * @param \Ivory\HttpAdapter\Event\RequestCreatedEvent $event |
||
68 | */ |
||
69 | 57 | public function onRequestCreated(RequestCreatedEvent $event) |
|
80 | |||
81 | /** |
||
82 | * On request sent. |
||
83 | * |
||
84 | * @param \Ivory\HttpAdapter\Event\RequestSentEvent $event |
||
85 | */ |
||
86 | 19 | public function onRequestSent(RequestSentEvent $event) |
|
90 | |||
91 | /** |
||
92 | * On request errored. |
||
93 | * |
||
94 | * @param \Ivory\HttpAdapter\Event\RequestErroredEvent $event |
||
95 | */ |
||
96 | 19 | public function onRequestErrored(RequestErroredEvent $event) |
|
100 | |||
101 | /** |
||
102 | * On multi request created. |
||
103 | * |
||
104 | * @param \Ivory\HttpAdapter\Event\MultiRequestCreatedEvent $event |
||
105 | */ |
||
106 | 57 | public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
120 | |||
121 | /** |
||
122 | * On multi request sent. |
||
123 | * |
||
124 | * @param \Ivory\HttpAdapter\Event\MultiRequestSentEvent $event |
||
125 | */ |
||
126 | 19 | public function onMultiRequestSent(MultiRequestSentEvent $event) |
|
132 | |||
133 | /** |
||
134 | * On multi request errored. |
||
135 | * |
||
136 | * @param \Ivory\HttpAdapter\Event\MultiRequestErroredEvent $event |
||
137 | */ |
||
138 | 19 | public function onMultiRequestErrored(MultiRequestErroredEvent $event) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 19 | View Code Duplication | public static function getSubscribedEvents() |
163 | } |
||
164 |
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: