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 |
||
20 | class ProphecyMockingStrategy extends AbstractMockingStrategy |
||
21 | { |
||
22 | const CLASS_NAME = Prophet::class; |
||
23 | const PACKAGE_NAME = 'phpspec/prophecy'; |
||
24 | |||
25 | /** |
||
26 | * @var Prophet |
||
27 | */ |
||
28 | private $prophet; |
||
29 | |||
30 | /** |
||
31 | * PHPUnitMockingStrategy constructor. |
||
32 | * |
||
33 | * @throws MissingDependencyException |
||
34 | */ |
||
35 | 2 | public function __construct() |
|
42 | |||
43 | /** |
||
44 | * @param string $fqcn |
||
45 | * @return ObjectProphecy |
||
46 | */ |
||
47 | 27 | protected function doBuild(string $fqcn) |
|
51 | |||
52 | /** |
||
53 | * @param ObjectProphecy $mock |
||
54 | * @param MethodStub $stub |
||
55 | * @return void |
||
56 | */ |
||
57 | 25 | View Code Duplication | protected function doDecorateWithMethod($mock, MethodStub $stub) |
1 ignored issue
–
show
|
|||
58 | { |
||
59 | 25 | $methodName = $stub->getName(); |
|
60 | 25 | $methodValue = $stub->getValue(); |
|
61 | |||
62 | /** @var MethodProphecy $partial */ |
||
63 | 25 | $partial = $mock->$methodName(new MaxPriorityToken()); |
|
64 | 25 | $methodValue instanceof \Throwable |
|
65 | 25 | ? $partial->willThrow($methodValue) |
|
66 | 25 | : $partial->willReturn($methodValue); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param ObjectProphecy $mock |
||
71 | * @return mixed |
||
72 | * |
||
73 | * @throws MockNotCreatedException |
||
74 | */ |
||
75 | 16 | protected function doGet($mock) |
|
88 | |||
89 | /** |
||
90 | * @param ObjectProphecy $mock |
||
91 | * @param string $methodName |
||
92 | * @return object |
||
93 | */ |
||
94 | 2 | protected function doCall($mock, string $methodName) |
|
98 | } |
||
99 |
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.