| Conditions | 1 |
| Paths | 1 |
| Total Lines | 94 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 34 | public function testCreateRequest(): void |
||
| 35 | { |
||
| 36 | $jwsBuilder = $this->prophesize(JWSBuilder::class); |
||
| 37 | $serializer = $this->prophesize(JWSSerializer::class); |
||
| 38 | |||
| 39 | $auth = new ClientSecretJwt( |
||
| 40 | $jwsBuilder->reveal(), |
||
| 41 | $serializer->reveal() |
||
| 42 | ); |
||
| 43 | |||
| 44 | $stream = $this->prophesize(StreamInterface::class); |
||
| 45 | $request = $this->prophesize(RequestInterface::class); |
||
| 46 | $client = $this->prophesize(ClientInterface::class); |
||
| 47 | $metadata = $this->prophesize(ClientMetadataInterface::class); |
||
| 48 | $issuer = $this->prophesize(IssuerInterface::class); |
||
| 49 | $issuerMetadata = $this->prophesize(IssuerMetadataInterface::class); |
||
| 50 | |||
| 51 | $client->getMetadata()->willReturn($metadata->reveal()); |
||
| 52 | $client->getIssuer()->willReturn($issuer->reveal()); |
||
| 53 | $metadata->getClientId()->willReturn('foo'); |
||
| 54 | $metadata->getClientSecret()->willReturn('bar'); |
||
| 55 | $issuer->getMetadata()->willReturn($issuerMetadata->reveal()); |
||
| 56 | $issuerMetadata->getIssuer()->willReturn('issuer'); |
||
| 57 | |||
| 58 | $jwsBuilder2 = $this->prophesize(JWSBuilder::class); |
||
| 59 | $jwsBuilder3 = $this->prophesize(JWSBuilder::class); |
||
| 60 | $jwsBuilder4 = $this->prophesize(JWSBuilder::class); |
||
| 61 | $jws = $this->prophesize(JWS::class); |
||
| 62 | |||
| 63 | $jwsBuilder->create()->shouldBeCalled()->willReturn($jwsBuilder2->reveal()); |
||
| 64 | $jwsBuilder2->withPayload(Argument::that(function (string $payload) { |
||
| 65 | $decoded = json_decode($payload, true); |
||
| 66 | |||
| 67 | static::assertIsArray($decoded); |
||
| 68 | |||
| 69 | static::assertArrayHasKey('iss', $decoded); |
||
| 70 | static::assertArrayHasKey('sub', $decoded); |
||
| 71 | static::assertArrayHasKey('aud', $decoded); |
||
| 72 | static::assertArrayHasKey('iat', $decoded); |
||
| 73 | static::assertArrayHasKey('exp', $decoded); |
||
| 74 | static::assertArrayHasKey('jti', $decoded); |
||
| 75 | |||
| 76 | static::assertSame('bar', $decoded['foo'] ?? null); |
||
| 77 | static::assertSame('foo', $decoded['iss'] ?? null); |
||
| 78 | static::assertSame('foo', $decoded['sub'] ?? null); |
||
| 79 | static::assertSame('issuer', $decoded['aud'] ?? null); |
||
| 80 | static::assertLessThanOrEqual(time(), $decoded['iat']); |
||
| 81 | static::assertLessThanOrEqual(time() + 60, $decoded['exp']); |
||
| 82 | static::assertGreaterThan(time(), $decoded['exp']); |
||
| 83 | |||
| 84 | return true; |
||
| 85 | })) |
||
| 86 | ->shouldBeCalled() |
||
| 87 | ->willReturn($jwsBuilder3->reveal()); |
||
| 88 | |||
| 89 | $jwsBuilder3->addSignature(Argument::allOf( |
||
| 90 | Argument::type(JWK::class), |
||
| 91 | Argument::that(function (JWK $jwk) { |
||
| 92 | static::assertSame('oct', $jwk->get('kty')); |
||
| 93 | static::assertSame(base64url_encode('bar'), $jwk->get('k')); |
||
| 94 | |||
| 95 | return true; |
||
| 96 | }) |
||
| 97 | ), Argument::allOf( |
||
| 98 | Argument::type('array'), |
||
| 99 | Argument::withEntry('alg', 'HS256'), |
||
| 100 | Argument::withKey('jti') |
||
| 101 | )) |
||
| 102 | ->shouldBeCalled() |
||
| 103 | ->willReturn($jwsBuilder4); |
||
| 104 | $jwsBuilder4->build()->willReturn($jws->reveal()); |
||
| 105 | |||
| 106 | $serializer->serialize($jws->reveal(), 0) |
||
| 107 | ->shouldBeCalled() |
||
| 108 | ->willReturn('assertion'); |
||
| 109 | |||
| 110 | $body = http_build_query([ |
||
| 111 | 'client_id' => 'foo', |
||
| 112 | 'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer', |
||
| 113 | 'client_assertion' => 'assertion', |
||
| 114 | 'foo' => 'bar', |
||
| 115 | ]); |
||
| 116 | |||
| 117 | $stream->write($body)->shouldBeCalled(); |
||
| 118 | |||
| 119 | $request->getBody()->willReturn($stream->reveal()); |
||
| 120 | |||
| 121 | $result = $auth->createRequest( |
||
| 122 | $request->reveal(), |
||
| 123 | $client->reveal(), |
||
| 124 | ['foo' => 'bar'] |
||
| 125 | ); |
||
| 126 | |||
| 127 | static::assertSame($request->reveal(), $result); |
||
| 128 | } |
||
| 157 |