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 |
||
26 | class ControllerDispatchInflectorSpec extends ObjectBehavior |
||
27 | { |
||
28 | |||
29 | function its_a_controller_inflector() |
||
30 | { |
||
31 | $this->shouldBeAnInstanceOf(ControllerDispatchInflectorInterface::class); |
||
32 | } |
||
33 | |||
34 | function it_is_initializable() |
||
35 | { |
||
36 | $this->shouldHaveType(ControllerDispatchInflector::class); |
||
37 | } |
||
38 | |||
39 | function it_inflects_controller_dispatch_from_route_attributes() |
||
40 | { |
||
41 | $route = new Route(); |
||
42 | $route->attributes([ |
||
43 | 'namespace' => 'spec\Slick\WebStack\Dispatcher', |
||
44 | 'controller' => 'pages', |
||
45 | 'action' => 'index', |
||
46 | 'args' => [123, 'test'] |
||
47 | ]); |
||
48 | $this->inflect($route)->shouldBeAnInstanceOf(ControllerDispatch::class); |
||
49 | } |
||
50 | function it_converts_dashed_url_controller_param_into_controller_class_names() |
||
51 | { |
||
52 | $route = new Route(); |
||
53 | $route->attributes([ |
||
54 | 'namespace' => 'spec\Slick\WebStack\Dispatcher', |
||
55 | 'controller' => 'yellow-pages', |
||
56 | 'action' => 'index' |
||
57 | ]); |
||
58 | $this->inflect($route)->shouldHaveControllerNamed('spec\Slick\WebStack\Dispatcher\YellowPages'); |
||
59 | } |
||
60 | function it_converts_underscored_url_controller_param_into_controller_class_names() |
||
61 | { |
||
62 | $route = new Route(); |
||
63 | $route->attributes([ |
||
64 | 'namespace' => 'spec\Slick\WebStack\Dispatcher', |
||
65 | 'controller' => 'yellow_pages', |
||
66 | 'action' => 'index' |
||
67 | ]); |
||
68 | $this->inflect($route)->shouldHaveControllerNamed('spec\Slick\WebStack\Dispatcher\YellowPages'); |
||
69 | } |
||
70 | public function getMatchers() |
||
71 | { |
||
72 | return [ |
||
73 | 'haveControllerNamed' => function (ControllerDispatch $subject, $name) { |
||
74 | if ($subject->controllerClass()->getName() !== $name) { |
||
75 | throw new FailureException( |
||
76 | "Expected controller name to be '{$name}', ". |
||
77 | "but got {$subject->controllerClass()->getName()}" |
||
78 | ); |
||
79 | } |
||
80 | return true; |
||
81 | } |
||
82 | ]; |
||
83 | } |
||
84 | } |
||
85 | |||
98 | } |