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 declare(strict_types = 1); |
||
22 | class RequestMatcherTest extends \PHPUnit_Framework_TestCase |
||
23 | { |
||
24 | const DOCUMENT_PATH = '/totally/non-existent/path'; |
||
25 | |||
26 | /** |
||
27 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
28 | */ |
||
29 | private $repositoryStub; |
||
30 | |||
31 | /** |
||
32 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
33 | */ |
||
34 | private $decriptionStub; |
||
35 | |||
36 | /** |
||
37 | * @var RequestMatcher |
||
38 | */ |
||
39 | private $matcher; |
||
40 | |||
41 | /** |
||
42 | * Create mocks |
||
43 | */ |
||
44 | protected function setUp() |
||
64 | |||
65 | /** |
||
66 | * @test |
||
67 | */ |
||
68 | public function willReturnFalseIfNoDocumentUriInAttributes() |
||
72 | |||
73 | /** |
||
74 | * @test |
||
75 | */ |
||
76 | public function willReturnFalseWhenOperationNotSecured() |
||
80 | |||
81 | /** |
||
82 | * @test |
||
83 | */ |
||
84 | public function willReturnTrueWhenOperationNotSecured() |
||
88 | |||
89 | /** |
||
90 | * @test |
||
91 | */ |
||
92 | public function settingMatchUnsecuredToTrueWillReturnTrueEvenIfMatchHasNoSecurityInfo() |
||
97 | |||
98 | /** |
||
99 | * @param bool $securedOperation |
||
100 | * @return Request |
||
101 | */ |
||
102 | private function createRequest(bool $securedOperation): Request |
||
103 | { |
||
104 | $this->repositoryStub |
||
105 | ->expects($this->any()) |
||
106 | ->method('get') |
||
107 | ->willReturn($this->decriptionStub); |
||
108 | |||
109 | $pathStub = $this |
||
110 | ->getMockBuilder(Path::class) |
||
111 | ->disableOriginalConstructor() |
||
112 | ->getMock(); |
||
113 | |||
114 | $this->decriptionStub |
||
115 | ->expects($this->any()) |
||
116 | ->method('getPath') |
||
117 | ->willReturn($pathStub); |
||
118 | |||
119 | $pathStub |
||
120 | ->expects($this->any()) |
||
121 | ->method('getOperation') |
||
122 | ->willReturn( |
||
123 | new Operation('', '', '', [], null, [], [], $securedOperation) |
||
124 | ); |
||
131 |