| Conditions | 1 |
| Paths | 1 |
| Total Lines | 55 |
| Code Lines | 41 |
| 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 |
||
| 10 | public function testReadAction() |
||
| 11 | { |
||
| 12 | $em = $this->getEntityManager(); |
||
| 13 | $entity = new MyEntity('my-test-secret'); |
||
| 14 | $em->persist($entity); |
||
| 15 | $parent = new MyEntity('non-recursing-entity'); |
||
| 16 | $em->persist($parent); |
||
| 17 | $entity->setParent($parent); |
||
| 18 | $em->flush(); |
||
| 19 | $em->clear(); |
||
| 20 | |||
| 21 | $client = self::createClient(); |
||
| 22 | $client->request( |
||
| 23 | 'GET', |
||
| 24 | '/api/entity/my-entity/get', |
||
| 25 | ['identifier' => $entity->getId()], |
||
| 26 | [], |
||
| 27 | ['HTTP_CONTENT_TYPE' => 'application/json'] |
||
| 28 | ); |
||
| 29 | $response = $client->getResponse(); |
||
| 30 | |||
| 31 | self::assertTrue($response->isSuccessful()); |
||
| 32 | $data = json_decode($response->getContent()); |
||
| 33 | |||
| 34 | self::assertEquals(JSON_ERROR_NONE, json_last_error()); |
||
| 35 | |||
| 36 | self::assertInstanceOf(\stdClass::class, $data); |
||
| 37 | self::assertSame($entity->getId(), $data->id); |
||
| 38 | self::assertObjectHasAttribute('public_api_field', $data); |
||
| 39 | self::assertSame('defaults', $data->public_api_field); |
||
| 40 | self::assertObjectNotHasAttribute('private_field', $data); |
||
| 41 | self::assertSame($parent->getId(), $data->parent); |
||
| 42 | self::assertSame([], $data->children); |
||
| 43 | |||
| 44 | $client->request( |
||
| 45 | 'GET', |
||
| 46 | '/api/entity/my-entity/get', |
||
| 47 | ['identifier' => $parent->getId()], |
||
| 48 | [], |
||
| 49 | ['HTTP_CONTENT_TYPE' => 'application/json'] |
||
| 50 | ); |
||
| 51 | $response = $client->getResponse(); |
||
| 52 | |||
| 53 | self::assertTrue($response->isSuccessful()); |
||
| 54 | $data = json_decode($response->getContent()); |
||
| 55 | |||
| 56 | self::assertEquals(JSON_ERROR_NONE, json_last_error()); |
||
| 57 | |||
| 58 | self::assertInstanceOf(\stdClass::class, $data); |
||
| 59 | self::assertSame($parent->getId(), $data->id); |
||
| 60 | self::assertSame('defaults', $data->public_api_field); |
||
| 61 | self::assertObjectNotHasAttribute('private_field', $data); |
||
| 62 | self::assertNull($data->parent); |
||
| 63 | self::assertSame([$entity->getId()], $data->children); |
||
| 64 | } |
||
| 65 | } |
||
| 66 |