| Conditions | 3 |
| Paths | 4 |
| Total Lines | 54 |
| Code Lines | 35 |
| Lines | 7 |
| Ratio | 12.96 % |
| 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 |
||
| 55 | public function testOneToManyLoading() |
||
| 56 | { |
||
| 57 | $repository = $this->getManager()->getRepository(TestEntity::class); |
||
| 58 | |||
| 59 | $this->getClient('test-client')->push( |
||
| 60 | $this->getResponseMock( |
||
| 61 | true, |
||
| 62 | (object)[ |
||
| 63 | 'id' => '1', |
||
| 64 | 'payload' => 'test-payload', |
||
| 65 | ] |
||
| 66 | ) |
||
| 67 | ); |
||
| 68 | |||
| 69 | $this->getClient('test-reference-client')->push( |
||
| 70 | $this->getResponseMock( |
||
| 71 | true, |
||
| 72 | [ |
||
| 73 | (object)[ |
||
| 74 | 'id' => '5', |
||
| 75 | 'reference-payload' => 'test-payload-5', |
||
| 76 | 'owner' => '1', |
||
| 77 | ], |
||
| 78 | (object)[ |
||
| 79 | 'id' => '7', |
||
| 80 | 'reference-payload' => 'test-payload-7', |
||
| 81 | 'owner' => '1', |
||
| 82 | ], |
||
| 83 | ] |
||
| 84 | ) |
||
| 85 | ); |
||
| 86 | |||
| 87 | /** @var TestEntity $entity */ |
||
| 88 | $entity = $repository->find(1); |
||
| 89 | |||
| 90 | self::assertInstanceOf(TestEntity::class, $entity); |
||
| 91 | self::assertEquals('test-payload', $entity->getPayload()); |
||
| 92 | self::assertEquals(1, $entity->getId()); |
||
| 93 | self::assertInstanceOf(Collection::class, $entity->getReferences()); |
||
| 94 | self::assertEquals(1, $this->getClient('test-reference-client')->count()); |
||
| 95 | self::assertCount(2, $entity->getReferences()); |
||
| 96 | self::assertEquals(0, $this->getClient('test-reference-client')->count()); |
||
| 97 | foreach ($entity->getReferences() as $reference) { |
||
| 98 | } |
||
| 99 | self::assertInstanceOf(Collection::class, $entity->getReferences()); |
||
| 100 | |||
| 101 | View Code Duplication | foreach ($entity->getReferences() as $reference) { |
|
| 102 | self::assertInternalType('int', $reference->getId()); |
||
| 103 | self::assertInternalType('int', $reference->getOwner()->getId()); |
||
| 104 | self::assertInstanceOf(TestReference::class, $reference); |
||
| 105 | self::assertEquals('test-payload-' . $reference->getId(), $reference->getReferencePayload()); |
||
| 106 | self::assertEquals($entity, $reference->getOwner()); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 230 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: