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