| Total Complexity | 41 |
| Total Lines | 296 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 1 | 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 |
||
| 34 | abstract class AbstractRequester |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var Schema|null |
||
| 38 | */ |
||
| 39 | protected ?Schema $schema = null; |
||
| 40 | |||
| 41 | protected int $statusExpected = 200; |
||
| 42 | protected array $assertHeader = []; |
||
| 43 | protected array $assertBody = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var RequestInterface |
||
| 47 | */ |
||
| 48 | protected RequestInterface $psr7Request; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * AbstractRequester constructor. |
||
| 52 | * @throws MessageException |
||
| 53 | * @throws RequestException |
||
| 54 | */ |
||
| 55 | public function __construct() |
||
| 56 | { |
||
| 57 | $this->withPsr7Request(Request::getInstance(new Uri("/"))->withMethod("get")); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * abstract function to be implemented by derived classes |
||
| 62 | * |
||
| 63 | * This function must be implemented by derived classes. It should process |
||
| 64 | * the given request and return an according response. |
||
| 65 | * |
||
| 66 | * @param RequestInterface $request |
||
| 67 | * @return ResponseInterface |
||
| 68 | */ |
||
| 69 | abstract protected function handleRequest(RequestInterface $request): ResponseInterface; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param Schema $schema |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | public function withSchema(Schema $schema): self |
||
| 76 | { |
||
| 77 | $this->schema = $schema; |
||
| 78 | |||
| 79 | return $this; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | public function hasSchema(): bool |
||
| 86 | { |
||
| 87 | return !empty($this->schema); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $method |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | public function withMethod(string $method): self |
||
| 95 | { |
||
| 96 | $this->psr7Request = $this->psr7Request->withMethod($method); |
||
| 97 | |||
| 98 | return $this; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $path |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function withPath(string $path): self |
||
| 106 | { |
||
| 107 | $uri = $this->psr7Request->getUri()->withPath($path); |
||
| 108 | $this->psr7Request = $this->psr7Request->withUri($uri); |
||
| 109 | |||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string|array $requestHeader |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | public function withRequestHeader(string|array $requestHeader): self |
||
| 118 | { |
||
| 119 | foreach ((array)$requestHeader as $name => $value) { |
||
| 120 | $this->psr7Request = $this->psr7Request->withHeader($name, $value); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $this; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param array|null $query |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function withQuery(array $query = null): self |
||
| 131 | { |
||
| 132 | $uri = $this->psr7Request->getUri(); |
||
| 133 | |||
| 134 | if (is_null($query)) { |
||
| 135 | $uri = $uri->withQuery(""); |
||
| 136 | $this->psr7Request = $this->psr7Request->withUri($uri); |
||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | $currentQuery = []; |
||
| 141 | parse_str($uri->getQuery(), $currentQuery); |
||
| 142 | |||
| 143 | $uri = $uri->withQuery(http_build_query(array_merge($currentQuery, $query))); |
||
| 144 | $this->psr7Request = $this->psr7Request->withUri($uri); |
||
| 145 | |||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param mixed $requestBody |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | public function withRequestBody(array|string $requestBody): self |
||
| 154 | { |
||
| 155 | $contentType = $this->psr7Request->getHeaderLine("Content-Type"); |
||
| 156 | if (is_array($requestBody) && (empty($contentType) || str_contains($contentType, "application/json"))) { |
||
| 157 | $requestBody = json_encode($requestBody); |
||
| 158 | } |
||
| 159 | $this->psr7Request = $this->psr7Request->withBody(new MemoryStream($requestBody)); |
||
| 160 | |||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param RequestInterface $requestInterface |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | public function withPsr7Request(RequestInterface $requestInterface): self |
||
| 169 | { |
||
| 170 | $this->psr7Request = $requestInterface->withHeader("Accept", "application/json"); |
||
| 171 | |||
| 172 | return $this; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function assertResponseCode(int $code): self |
||
| 176 | { |
||
| 177 | $this->statusExpected = $code; |
||
| 178 | |||
| 179 | return $this; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function assertHeaderContains(string $header, string $contains): self |
||
| 183 | { |
||
| 184 | $this->assertHeader[$header] = $contains; |
||
| 185 | |||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function assertBodyContains(string $contains): self |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return ResponseInterface |
||
| 198 | * @throws DefinitionNotFoundException |
||
| 199 | * @throws GenericSwaggerException |
||
| 200 | * @throws HttpMethodNotFoundException |
||
| 201 | * @throws InvalidDefinitionException |
||
| 202 | * @throws InvalidRequestException |
||
| 203 | * @throws NotMatchedException |
||
| 204 | * @throws PathNotFoundException |
||
| 205 | * @throws RequiredArgumentNotFound |
||
| 206 | * @throws StatusCodeNotMatchedException |
||
| 207 | */ |
||
| 208 | public function send(): ResponseInterface |
||
| 297 | } |
||
| 298 | |||
| 299 | protected function parseMultiPartForm(?string $contentType, string $body): array|null |
||
| 330 | } |
||
| 331 | } |
||
| 332 |
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.