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 |
||
25 | class BasePathTransformerSpec extends ObjectBehavior |
||
26 | { |
||
27 | function it_is_initializable() |
||
28 | { |
||
29 | $this->shouldHaveType(BasePathTransformer::class); |
||
30 | } |
||
31 | |||
32 | function it_implements_location_transformer_interface() |
||
33 | { |
||
34 | $this->shouldImplement(LocationTransformerInterface::class); |
||
35 | } |
||
36 | |||
37 | function it_may_have_an_http_request_as_a_context( |
||
38 | ServerRequestInterface $request |
||
39 | ) { |
||
40 | $this->setRequest($request) |
||
41 | ->shouldReturn($this->getWrappedObject()); |
||
42 | } |
||
43 | |||
44 | function it_only_generates_an_uri_if_it_has_an_http_request() |
||
45 | { |
||
46 | $this->transform('home')->shouldBeNull(); |
||
47 | } |
||
48 | |||
49 | function it_adds_the_base_path_to_the_location( |
||
50 | ServerRequestInterface $request |
||
51 | ) |
||
52 | { |
||
53 | $this->prepareRequest($request); |
||
54 | $this->transform('controller/action') |
||
55 | ->shouldBeAnUriWithPath('/base/controller/action'); |
||
56 | } |
||
57 | |||
58 | function it_accepts_query_params_in_options( |
||
59 | ServerRequestInterface $request |
||
60 | ) |
||
61 | { |
||
62 | $this->prepareRequest($request); |
||
63 | $this->transform('controller/action', ['query' => ['foo' => 'bar']]) |
||
64 | ->shouldBeAnUriWithPath('/base/controller/action?foo=bar'); |
||
65 | } |
||
66 | |||
67 | function it_can_reuse_host_name_from_context_request( |
||
68 | ServerRequestInterface $request |
||
69 | ) |
||
70 | { |
||
71 | $this->prepareRequest($request); |
||
72 | $this->transform('controller/action', ['reuseHostName' => 1]) |
||
73 | ->shouldBeAnUriWithPath( |
||
74 | 'https://localhost:12541/base/controller/action' |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | function it_can_reuse_the_request_query_params( |
||
79 | ServerRequestInterface $request |
||
80 | ) |
||
81 | { |
||
82 | $this->prepareRequest($request); |
||
83 | $this->transform('controller/action', [ |
||
84 | 'reuseParams' => 1, |
||
85 | 'query' => ['foo' => 'bar'] |
||
86 | ]) |
||
87 | ->shouldBeAnUriWithPath( |
||
88 | '/base/controller/action?foo=bar&baz=bar' |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Prepares the request collaborator |
||
94 | * |
||
95 | * @param ServerRequestInterface|Collaborator $request |
||
96 | */ |
||
97 | private function prepareRequest(ServerRequestInterface $request) |
||
98 | { |
||
99 | $serverData = [ |
||
100 | 'SCRIPT_NAME' => '/base/test.php', |
||
101 | 'HTTPS' => 'not-empty', |
||
102 | 'SERVER_PORT' => '12541', |
||
103 | 'SERVER_NAME' => 'localhost', |
||
104 | ]; |
||
105 | $request->getServerParams() |
||
106 | ->shouldBeCalled() |
||
107 | ->willReturn($serverData); |
||
108 | $request->getQueryParams()->willReturn( |
||
109 | ['foo' => 'bar', 'baz' => 'bar'] |
||
110 | ); |
||
111 | $this->setRequest($request); |
||
112 | } |
||
113 | |||
114 | public function getMatchers() |
||
115 | { |
||
116 | return [ |
||
117 | 'beAnUriWithPath' => function ($uri, $path) |
||
118 | { |
||
119 | if (!$uri instanceof UriInterface) { |
||
120 | $class = UriInterface::class; |
||
121 | $type = gettype($uri); |
||
122 | throw new FailureException( |
||
123 | "Expected {$class} instance, but got '{$type}'" |
||
124 | ); |
||
125 | } |
||
126 | if ($uri->__toString() !== $path) { |
||
127 | throw new FailureException( |
||
128 | "Expected URI with path '{$path}', but got '{$uri}'" |
||
129 | ); |
||
130 | } |
||
131 | return true; |
||
132 | } |
||
133 | ]; |
||
134 | } |
||
135 | } |