| Conditions | 1 |
| Total Lines | 63 |
| 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 | private $serverRequest; |
||
| 53 | |||
| 54 | public function __construct(string $expectedScheme) |
||
| 55 | { |
||
| 56 | $this->expectedScheme = $expectedScheme; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return static |
||
| 61 | */ |
||
| 62 | public function withServerRequest(ServerRequestInterface $serverRequest) |
||
| 63 | { |
||
| 64 | $new = clone $this; |
||
| 65 | $new->serverRequest = $serverRequest; |
||
| 66 | return $new; |
||
| 67 | } |
||
| 68 | |||
| 69 | public function getRemoteIp(): string |
||
| 70 | { |
||
| 71 | throw new \RuntimeException('Not supported!'); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getUserIp(): string |
||
| 75 | { |
||
| 76 | throw new \RuntimeException('Not supported!'); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function getRequestScheme(): string |
||
| 80 | { |
||
| 81 | return $this->expectedScheme; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function isSecureConnection(): bool |
||
| 85 | { |
||
| 86 | throw new \RuntimeException('Not supported!'); |
||
| 87 | } |
||
| 88 | }; |
||
| 89 | $middleware = new NetworkResolver($nr); |
||
| 90 | $middleware->process($request, $requestHandler); |
||
| 91 | $resultRequest = $requestHandler->request; |
||
| 92 | /* @var $resultRequest ServerRequestInterface */ |
||
| 93 | $this->assertSame($expectedScheme, $resultRequest->getUri()->getScheme()); |
||
| 94 | } |
||
| 97 |