| Conditions | 3 |
| Paths | 1 |
| Total Lines | 70 |
| 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 |
||
| 21 | protected function prepareProxy() : void |
||
| 22 | { |
||
| 23 | $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; |
||
| 24 | $objectOne = new FormObject(); |
||
| 25 | $objectTwo = new FormObject(); |
||
| 26 | |||
| 27 | $objectOne->setId(1) |
||
| 28 | ->setUsername('object one username') |
||
| 29 | ->setPassword('object one password') |
||
| 30 | ->setEmail('object one email') |
||
| 31 | ->setFirstname('object one firstname') |
||
| 32 | ->setSurname('object one surname'); |
||
| 33 | |||
| 34 | $objectTwo->setId(2) |
||
| 35 | ->setUsername('object two username') |
||
| 36 | ->setPassword('object two password') |
||
| 37 | ->setEmail('object two email') |
||
| 38 | ->setFirstname('object two firstname') |
||
| 39 | ->setSurname('object two surname'); |
||
| 40 | |||
| 41 | $result = new ArrayCollection([$objectOne, $objectTwo]); |
||
| 42 | $this->values = $result; |
||
|
|
|||
| 43 | |||
| 44 | $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); |
||
| 45 | $metadata |
||
| 46 | ->expects($this->any()) |
||
| 47 | ->method('getIdentifierValues') |
||
| 48 | ->will( |
||
| 49 | $this->returnCallback( |
||
| 50 | static function () use ($objectOne, $objectTwo) { |
||
| 51 | $input = func_get_args(); |
||
| 52 | $input = array_shift($input); |
||
| 53 | |||
| 54 | if ($input === $objectOne) { |
||
| 55 | return ['id' => 1]; |
||
| 56 | } |
||
| 57 | |||
| 58 | if ($input === $objectTwo) { |
||
| 59 | return ['id' => 2]; |
||
| 60 | } |
||
| 61 | |||
| 62 | return []; |
||
| 63 | } |
||
| 64 | ) |
||
| 65 | ); |
||
| 66 | |||
| 67 | $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); |
||
| 68 | $objectRepository->expects($this->any()) |
||
| 69 | ->method('findAll') |
||
| 70 | ->will($this->returnValue($result)); |
||
| 71 | |||
| 72 | $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); |
||
| 73 | $objectManager->expects($this->any()) |
||
| 74 | ->method('getClassMetadata') |
||
| 75 | ->with($this->equalTo($objectClass)) |
||
| 76 | ->will($this->returnValue($metadata)); |
||
| 77 | |||
| 78 | $objectManager |
||
| 79 | ->expects($this->any()) |
||
| 80 | ->method('getRepository') |
||
| 81 | ->with($this->equalTo($objectClass)) |
||
| 82 | ->will($this->returnValue($objectRepository)); |
||
| 83 | |||
| 84 | $this->element->getProxy()->setOptions([ |
||
| 85 | 'object_manager' => $objectManager, |
||
| 86 | 'target_class' => $objectClass, |
||
| 87 | ]); |
||
| 88 | |||
| 89 | $this->metadata = $metadata; |
||
| 90 | } |
||
| 91 | |||
| 106 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: