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 |
||
25 | class ThrowPromise implements PromiseInterface |
||
26 | { |
||
27 | private $exception; |
||
28 | |||
29 | /** |
||
30 | * @var \Doctrine\Instantiator\Instantiator |
||
31 | */ |
||
32 | private $instantiator; |
||
33 | |||
34 | /** |
||
35 | * Initializes promise. |
||
36 | * |
||
37 | * @param string|\Exception|\Throwable $exception Exception class name or instance |
||
38 | * |
||
39 | * @throws \Prophecy\Exception\InvalidArgumentException |
||
40 | */ |
||
41 | public function __construct($exception) |
||
42 | { |
||
43 | if (is_string($exception)) { |
||
44 | View Code Duplication | if ((!class_exists($exception) && !interface_exists($exception)) || !$this->isAValidThrowable($exception)) { |
|
|
|||
45 | throw new InvalidArgumentException(sprintf( |
||
46 | 'Exception / Throwable class or instance expected as argument to ThrowPromise, but got %s.', |
||
47 | $exception |
||
48 | )); |
||
49 | } |
||
50 | } elseif (!$exception instanceof \Exception && !$exception instanceof \Throwable) { |
||
51 | throw new InvalidArgumentException(sprintf( |
||
52 | 'Exception / Throwable class or instance expected as argument to ThrowPromise, but got %s.', |
||
53 | is_object($exception) ? get_class($exception) : gettype($exception) |
||
54 | )); |
||
55 | } |
||
56 | |||
57 | $this->exception = $exception; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Throws predefined exception. |
||
62 | * |
||
63 | * @param array $args |
||
64 | * @param ObjectProphecy $object |
||
65 | * @param MethodProphecy $method |
||
66 | * |
||
67 | * @throws object |
||
68 | */ |
||
69 | public function execute(array $args, ObjectProphecy $object, MethodProphecy $method) |
||
89 | |||
90 | /** |
||
91 | * @param string $exception |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | private function isAValidThrowable($exception) |
||
100 | } |
||
101 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.