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 StatusCodeSubscriber implements EventSubscriberInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var StatusCodeInterface |
||
| 32 | */ |
||
| 33 | private $statusCode; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param StatusCodeInterface|null $statusCode |
||
| 37 | */ |
||
| 38 | 63 | public function __construct(StatusCodeInterface $statusCode = null) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * @return StatusCodeInterface |
||
| 45 | */ |
||
| 46 | 18 | public function getStatusCode() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @param RequestSentEvent $event |
||
| 53 | */ |
||
| 54 | 18 | public function onRequestSent(RequestSentEvent $event) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param MultiRequestSentEvent $event |
||
| 67 | */ |
||
| 68 | 18 | public function onMultiRequestSent(MultiRequestSentEvent $event) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | 9 | View Code Duplication | public static function getSubscribedEvents() |
| 93 | |||
| 94 | /** |
||
| 95 | * @param ResponseInterface $response |
||
| 96 | * @param InternalRequestInterface $internalRequest |
||
| 97 | * @param HttpAdapterInterface $httpAdapter |
||
| 98 | * |
||
| 99 | * @return HttpAdapterException |
||
| 100 | */ |
||
| 101 | 18 | private function createStatusCodeException( |
|
| 117 | } |
||
| 118 |
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: