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 |
||
| 11 | class RouterTest extends \PHPUnit\Framework\TestCase |
||
|
|
|||
| 12 | { |
||
| 13 | /** @var Router */ |
||
| 14 | protected $router; |
||
| 15 | |||
| 16 | public function setUp() |
||
| 17 | { |
||
| 18 | if( !class_exists( \Slim\Router::class, false ) ) { |
||
| 19 | $this->markTestSkipped( '\Slim\Router is not available' ); |
||
| 20 | } |
||
| 21 | |||
| 22 | $this->router = new \Aimeos\Slim\Router; |
||
| 23 | } |
||
| 24 | |||
| 25 | public function testRelativePathFor() |
||
| 26 | { |
||
| 27 | $this->router->setBasePath('/base/path'); |
||
| 28 | |||
| 29 | $methods = ['GET']; |
||
| 30 | $pattern = '/hello/{first:\w+}/{last}'; |
||
| 31 | $callable = function ($request, $response, $args) { |
||
| 32 | echo sprintf('Hello %s %s', $args['first'], $args['last']); |
||
| 33 | }; |
||
| 34 | $route = $this->router->map($methods, $pattern, $callable); |
||
| 35 | $route->setName('foo'); |
||
| 36 | |||
| 37 | $this->assertEquals( |
||
| 38 | '/hello/josh/lockhart', |
||
| 39 | $this->router->relativePathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testPathForWithNoBasePath() |
||
| 44 | { |
||
| 45 | $this->router->setBasePath(''); |
||
| 46 | |||
| 47 | $methods = ['GET']; |
||
| 48 | $pattern = '/hello/{first:\w+}/{last}'; |
||
| 49 | $callable = function ($request, $response, $args) { |
||
| 50 | echo sprintf('Hello %s %s', $args['first'], $args['last']); |
||
| 51 | }; |
||
| 52 | $route = $this->router->map($methods, $pattern, $callable); |
||
| 53 | $route->setName('foo'); |
||
| 54 | |||
| 55 | $this->assertEquals( |
||
| 56 | '/hello/josh/lockhart', |
||
| 57 | $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
||
| 58 | ); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testPathForWithBasePath() |
||
| 62 | { |
||
| 63 | $methods = ['GET']; |
||
| 64 | $pattern = '/hello/{first:\w+}/{last}'; |
||
| 65 | $callable = function ($request, $response, $args) { |
||
| 66 | echo sprintf('Hello %s %s', $args['first'], $args['last']); |
||
| 67 | }; |
||
| 68 | $this->router->setBasePath('/base/path'); |
||
| 69 | $route = $this->router->map($methods, $pattern, $callable); |
||
| 70 | $route->setName('foo'); |
||
| 71 | |||
| 72 | $this->assertEquals( |
||
| 73 | '/base/path/hello/josh/lockhart', |
||
| 74 | $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function testPathForWithOptionalParameters() |
||
| 79 | { |
||
| 80 | $methods = ['GET']; |
||
| 81 | $pattern = '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]'; |
||
| 82 | $callable = function ($request, $response, $args) { |
||
| 83 | return $response; |
||
| 84 | }; |
||
| 85 | $route = $this->router->map($methods, $pattern, $callable); |
||
| 86 | $route->setName('foo'); |
||
| 87 | |||
| 88 | $this->assertEquals( |
||
| 89 | '/archive/2015', |
||
| 90 | $this->router->pathFor('foo', ['year' => '2015']) |
||
| 91 | ); |
||
| 92 | $this->assertEquals( |
||
| 93 | '/archive/2015/07', |
||
| 94 | $this->router->pathFor('foo', ['year' => '2015', 'month' => '07']) |
||
| 95 | ); |
||
| 96 | $this->assertEquals( |
||
| 97 | '/archive/2015/07/d/19', |
||
| 98 | $this->router->pathFor('foo', ['year' => '2015', 'month' => '07', 'day' => '19']) |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function testPathForWithSurplusRouteParameters() |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @expectedException \InvalidArgumentException |
||
| 120 | */ |
||
| 121 | public function testPathForWithMissingSegmentData() |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @expectedException \RuntimeException |
||
| 136 | */ |
||
| 137 | public function testPathForRouteNotExists() |
||
| 148 | } |
||
| 149 | } |
||
| 150 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths