| Conditions | 1 |
| Total Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 31 | public function testSimple(string $scheme, string $expectedScheme) |
||
| 32 | { |
||
| 33 | $request = new ServerRequest('GET', '/'); |
||
| 34 | $uri = $request->getUri()->withScheme($scheme); |
||
| 35 | $request = $request->withUri($uri); |
||
| 36 | |||
| 37 | $requestHandler = new class implements RequestHandlerInterface |
||
| 38 | { |
||
| 39 | public $request; |
||
| 40 | |||
| 41 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 42 | { |
||
| 43 | $this->request = $request; |
||
| 44 | return new Response(200); |
||
| 45 | } |
||
| 46 | }; |
||
| 47 | |||
| 48 | $nr = new class($expectedScheme) implements NetworkResolverInterface |
||
| 49 | { |
||
| 50 | |||
| 51 | private $expectedScheme; |
||
| 52 | /** |
||
| 53 | * @var ServerRequestInterface |
||
| 54 | */ |
||
| 55 | private $serverRequest; |
||
| 56 | |||
| 57 | public function __construct(string $expectedScheme) |
||
| 58 | { |
||
| 59 | $this->expectedScheme = $expectedScheme; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return static |
||
| 64 | */ |
||
| 65 | public function withServerRequest(ServerRequestInterface $serverRequest) |
||
| 66 | { |
||
| 67 | $new = clone $this; |
||
| 68 | $new->serverRequest = $serverRequest; |
||
| 69 | return $new; |
||
| 70 | } |
||
| 71 | |||
| 72 | public function getRemoteIp(): string |
||
| 73 | { |
||
| 74 | throw new \RuntimeException('Not supported!'); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function getUserIp(): string |
||
| 78 | { |
||
| 79 | throw new \RuntimeException('Not supported!'); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getServerRequest(): ServerRequestInterface |
||
| 83 | { |
||
| 84 | return $this->serverRequest->withUri($this->serverRequest->getUri()->withScheme($this->expectedScheme)); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function isSecureConnection(): bool |
||
| 88 | { |
||
| 89 | throw new \RuntimeException('Not supported!'); |
||
| 90 | } |
||
| 91 | }; |
||
| 92 | $middleware = new NetworkResolver($nr); |
||
| 93 | $middleware->process($request, $requestHandler); |
||
| 94 | $resultRequest = $requestHandler->request; |
||
| 95 | /* @var $resultRequest ServerRequestInterface */ |
||
| 96 | $this->assertSame($expectedScheme, $resultRequest->getUri()->getScheme()); |
||
| 97 | } |
||
| 100 |