Total Complexity | 41 |
Total Lines | 294 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractRequester often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractRequester, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | abstract class AbstractRequester |
||
36 | { |
||
37 | /** |
||
38 | * @var Schema|null |
||
39 | */ |
||
40 | protected ?Schema $schema = null; |
||
41 | |||
42 | protected int $statusExpected = 200; |
||
43 | protected array $assertHeader = []; |
||
44 | protected array $assertBody = []; |
||
45 | |||
46 | /** |
||
47 | * @var RequestInterface |
||
48 | */ |
||
49 | protected RequestInterface $psr7Request; |
||
50 | |||
51 | /** |
||
52 | * AbstractRequester constructor. |
||
53 | * @throws MessageException |
||
54 | * @throws RequestException |
||
55 | */ |
||
56 | public function __construct() |
||
57 | { |
||
58 | $this->withPsr7Request(Request::getInstance(new Uri("/"))->withMethod("get")); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * abstract function to be implemented by derived classes |
||
63 | * |
||
64 | * This function must be implemented by derived classes. It should process |
||
65 | * the given request and return an according response. |
||
66 | * |
||
67 | * @param RequestInterface $request |
||
68 | * @return ResponseInterface |
||
69 | */ |
||
70 | abstract protected function handleRequest(RequestInterface $request): ResponseInterface; |
||
71 | |||
72 | /** |
||
73 | * @param Schema $schema |
||
74 | * @return $this |
||
75 | */ |
||
76 | public function withSchema(Schema $schema): self |
||
77 | { |
||
78 | $this->schema = $schema; |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function hasSchema(): bool |
||
87 | { |
||
88 | return !empty($this->schema); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param string $method |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function withMethod(string $method): self |
||
96 | { |
||
97 | $this->psr7Request = $this->psr7Request->withMethod($method); |
||
98 | |||
99 | return $this; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param string $path |
||
104 | * @return $this |
||
105 | */ |
||
106 | public function withPath(string $path): self |
||
107 | { |
||
108 | $uri = $this->psr7Request->getUri()->withPath($path); |
||
109 | $this->psr7Request = $this->psr7Request->withUri($uri); |
||
110 | |||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param string|array $requestHeader |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function withRequestHeader(string|array $requestHeader): self |
||
119 | { |
||
120 | foreach ((array)$requestHeader as $name => $value) { |
||
121 | $this->psr7Request = $this->psr7Request->withHeader($name, $value); |
||
122 | } |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param array|null $query |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function withQuery(array $query = null): self |
||
132 | { |
||
133 | $uri = $this->psr7Request->getUri(); |
||
134 | |||
135 | if (is_null($query)) { |
||
136 | $uri = $uri->withQuery(""); |
||
137 | $this->psr7Request = $this->psr7Request->withUri($uri); |
||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | $currentQuery = []; |
||
142 | parse_str($uri->getQuery(), $currentQuery); |
||
143 | |||
144 | $uri = $uri->withQuery(http_build_query(array_merge($currentQuery, $query))); |
||
145 | $this->psr7Request = $this->psr7Request->withUri($uri); |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param mixed $requestBody |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function withRequestBody(array|string $requestBody): self |
||
155 | { |
||
156 | $contentType = $this->psr7Request->getHeaderLine("Content-Type"); |
||
157 | if (is_array($requestBody) && (empty($contentType) || str_contains($contentType, "application/json"))) { |
||
158 | $requestBody = json_encode($requestBody); |
||
159 | } |
||
160 | $this->psr7Request = $this->psr7Request->withBody(new MemoryStream($requestBody)); |
||
161 | |||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param RequestInterface $requestInterface |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function withPsr7Request(RequestInterface $requestInterface): self |
||
170 | { |
||
171 | $this->psr7Request = $requestInterface->withHeader("Accept", "application/json"); |
||
172 | |||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | public function assertResponseCode(int $code): self |
||
177 | { |
||
178 | $this->statusExpected = $code; |
||
179 | |||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | public function assertHeaderContains(string $header, string $contains): self |
||
184 | { |
||
185 | $this->assertHeader[$header] = $contains; |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | public function assertBodyContains(string $contains): self |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @return ResponseInterface |
||
199 | * @throws DefinitionNotFoundException |
||
200 | * @throws GenericSwaggerException |
||
201 | * @throws HttpMethodNotFoundException |
||
202 | * @throws InvalidDefinitionException |
||
203 | * @throws InvalidRequestException |
||
204 | * @throws NotMatchedException |
||
205 | * @throws PathNotFoundException |
||
206 | * @throws RequiredArgumentNotFound |
||
207 | * @throws StatusCodeNotMatchedException |
||
208 | */ |
||
209 | public function send(): ResponseInterface |
||
296 | } |
||
297 | |||
298 | protected function parseMultiPartForm(?string $contentType, string $body): array|null |
||
331 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.