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 |
||
15 | class SplashUrlNodeTest extends \PHPUnit_Framework_TestCase |
||
16 | { |
||
17 | View Code Duplication | public function testAddUrl() |
|
|
|||
18 | { |
||
19 | $splashUrlNode = new SplashUrlNode(); |
||
20 | $callback = new SplashRoute('toto/tata', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST')); |
||
21 | $splashUrlNode->registerCallback($callback); |
||
22 | |||
23 | $result = $splashUrlNode->walk('toto/tata', new ServerRequest([], [], 'toto/tata', 'GET')); |
||
24 | |||
25 | /* @var $result SplashRoute */ |
||
26 | $this->assertInstanceOf(SplashRoute::class, $result); |
||
27 | $this->assertEquals('myController', $result->getControllerInstanceName()); |
||
28 | $this->assertEquals('myMethod', $result->getMethodName()); |
||
29 | } |
||
30 | |||
31 | View Code Duplication | public function testTrailingSlashUrl() |
|
32 | { |
||
33 | $splashUrlNode = new SplashUrlNode(); |
||
34 | $callback = new SplashRoute('toto/tata/', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST')); |
||
35 | $splashUrlNode->registerCallback($callback); |
||
36 | |||
37 | $result = $splashUrlNode->walk('toto/tata/', new ServerRequest([], [], 'toto/tata', 'GET')); |
||
38 | /* @var $result SplashRoute */ |
||
39 | $this->assertInstanceOf(SplashRoute::class, $result); |
||
40 | $this->assertEquals('myController', $result->getControllerInstanceName()); |
||
41 | $this->assertEquals('myMethod', $result->getMethodName()); |
||
42 | } |
||
43 | |||
44 | View Code Duplication | public function testRootUrl() |
|
45 | { |
||
46 | $splashUrlNode = new SplashUrlNode(); |
||
47 | $callback = new SplashRoute('/', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST')); |
||
48 | $splashUrlNode->registerCallback($callback); |
||
49 | |||
50 | $result = $splashUrlNode->walk('/', new ServerRequest([], [], '/', 'GET')); |
||
51 | /* @var $result SplashRoute */ |
||
52 | $this->assertInstanceOf(SplashRoute::class, $result); |
||
53 | $this->assertEquals('myController', $result->getControllerInstanceName()); |
||
54 | $this->assertEquals('myMethod', $result->getMethodName()); |
||
55 | } |
||
56 | |||
57 | View Code Duplication | public function testSameUrls() |
|
58 | { |
||
59 | $splashUrlNode = new SplashUrlNode(); |
||
60 | $callback = new SplashRoute('/', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST')); |
||
61 | $splashUrlNode->registerCallback($callback); |
||
62 | $callback = new SplashRoute('/', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST')); |
||
63 | |||
64 | $this->expectException(SplashException::class); |
||
65 | $splashUrlNode->registerCallback($callback); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | */ |
||
71 | View Code Duplication | public function testGlobalUrlCatchGet() |
|
72 | { |
||
73 | $splashUrlNode = new SplashUrlNode(); |
||
74 | $callback = new SplashRoute('/toto', 'myController', 'myMethod', 'myTitle', 'fullComment', array()); |
||
75 | $splashUrlNode->registerCallback($callback); |
||
76 | |||
77 | $result = $splashUrlNode->walk('/toto', new ServerRequest([], [], '/toto', 'GET')); |
||
78 | /* @var $result SplashRoute */ |
||
79 | $this->assertInstanceOf(SplashRoute::class, $result); |
||
80 | $this->assertEquals('myController', $result->getControllerInstanceName()); |
||
81 | $this->assertEquals('myMethod', $result->getMethodName()); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * |
||
86 | */ |
||
87 | public function testMultiUrls() |
||
88 | { |
||
89 | $splashUrlNode = new SplashUrlNode(); |
||
90 | $callback = new SplashRoute('/toto', 'myControllerOk', 'myMethodOk', 'myTitle', 'fullComment', array()); |
||
91 | $splashUrlNode->registerCallback($callback); |
||
92 | $callback = new SplashRoute('/toto/tata', 'myController', 'myMethod', 'myTitle', 'fullComment', array()); |
||
93 | $splashUrlNode->registerCallback($callback); |
||
94 | $callback = new SplashRoute('/tata', 'myController', 'myMethod', 'myTitle', 'fullComment', array()); |
||
95 | $splashUrlNode->registerCallback($callback); |
||
96 | |||
97 | $result = $splashUrlNode->walk('/toto', new ServerRequest([], [], '/toto', 'POST')); |
||
98 | /* @var $result SplashRoute */ |
||
99 | $this->assertInstanceOf(SplashRoute::class, $result); |
||
100 | $this->assertEquals('myControllerOk', $result->getControllerInstanceName()); |
||
101 | $this->assertEquals('myMethodOk', $result->getMethodName()); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * |
||
106 | */ |
||
107 | public function testParametersUrls() |
||
108 | { |
||
109 | $splashUrlNode = new SplashUrlNode(); |
||
110 | $callback = new SplashRoute('/toto/{var}/tata', 'myController', 'myMethod', 'myTitle', 'fullComment', []); |
||
111 | $splashUrlNode->registerCallback($callback); |
||
112 | |||
113 | $result = $splashUrlNode->walk('/toto/12/tata', new ServerRequest([], [], '/toto/12/tata', 'POST')); |
||
114 | /* @var $result SplashRoute */ |
||
115 | $this->assertInstanceOf(SplashRoute::class, $result); |
||
116 | $this->assertEquals('myController', $result->getControllerInstanceName()); |
||
117 | $this->assertEquals('myMethod', $result->getMethodName()); |
||
118 | $this->assertEquals(12, $result->getFilledParameters()['var']); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * |
||
123 | */ |
||
124 | public function testWildcardUrls() |
||
125 | { |
||
126 | $splashUrlNode = new SplashUrlNode(); |
||
127 | $callback = new SplashRoute('/toto/*', 'myController', 'myMethod', 'myTitle', 'fullComment', array()); |
||
128 | $splashUrlNode->registerCallback($callback); |
||
129 | $callback2 = new SplashRoute('/toto/*', 'myControllerPost', 'myMethodPost', 'myTitle', 'fullComment', array('POST')); |
||
130 | $splashUrlNode->registerCallback($callback2); |
||
131 | |||
132 | $result = $splashUrlNode->walk('/toto/tata/titi', new ServerRequest([], [], '/toto', 'GET')); |
||
133 | /* @var $result SplashRoute */ |
||
134 | $this->assertInstanceOf(SplashRoute::class, $result); |
||
135 | $this->assertEquals('myController', $result->getControllerInstanceName()); |
||
136 | $this->assertEquals('myMethod', $result->getMethodName()); |
||
137 | |||
138 | $result = $splashUrlNode->walk('/toto/', new ServerRequest([], [], '/toto', 'GET')); |
||
139 | /* @var $result SplashRoute */ |
||
140 | $this->assertInstanceOf(SplashRoute::class, $result); |
||
141 | $this->assertEquals('myController', $result->getControllerInstanceName()); |
||
142 | $this->assertEquals('myMethod', $result->getMethodName()); |
||
143 | |||
144 | // Now, let's test an URL with HTTP method set. |
||
145 | $result = $splashUrlNode->walk('/toto/tata/titi', new ServerRequest([], [], '/toto', 'POST')); |
||
146 | $this->assertEquals('myControllerPost', $result->getControllerInstanceName()); |
||
147 | $this->assertEquals('myMethodPost', $result->getMethodName()); |
||
148 | } |
||
149 | |||
150 | public function testUnsupportedWildcard() |
||
157 | |||
158 | View Code Duplication | public function testDoubleMethod() |
|
167 | |||
168 | View Code Duplication | public function testDoubleWildcardMethod() |
|
177 | |||
178 | View Code Duplication | public function testDoubleWildcardMethodWithHttpMethod() |
|
179 | { |
||
180 | $splashUrlNode = new SplashUrlNode(); |
||
181 | $callback = new SplashRoute('foo/*', 'myController', 'myMethod', 'myTitle', 'fullComment', ['GET', 'POST']); |
||
182 | $splashUrlNode->registerCallback($callback); |
||
183 | $callback2 = new SplashRoute('foo/*', 'myController2', 'myMethod2', 'myTitle', 'fullComment', ['GET']); |
||
184 | $this->expectException(SplashException::class); |
||
185 | $splashUrlNode->registerCallback($callback2); |
||
186 | } |
||
187 | |||
188 | public function testDoubleParameter() |
||
197 | |||
198 | public function testFallbackToWilcard() |
||
199 | { |
||
200 | $splashUrlNode = new SplashUrlNode(); |
||
201 | $callback = new SplashRoute('foo/bar/baz', 'myController', 'myMethod', 'myTitle', 'fullComment'); |
||
202 | $splashUrlNode->registerCallback($callback); |
||
203 | $callback2 = new SplashRoute('foo/*', 'myController2', 'myMethod2', 'myTitle', 'fullComment'); |
||
204 | $splashUrlNode->registerCallback($callback2); |
||
205 | |||
206 | $result = $splashUrlNode->walk('foo/bar', new ServerRequest([], [], '/foo/bar/biz', 'POST')); |
||
210 | } |
||
211 |
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.