| Conditions | 13 |
| Paths | 18 |
| Total Lines | 58 |
| Code Lines | 30 |
| 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 |
||
| 58 | protected function preUpdateOrPersistCollectionsManager($object) |
||
| 59 | { |
||
| 60 | // global configuration |
||
| 61 | $this->configureCollectionsManager(); |
||
| 62 | |||
| 63 | // for each given collection |
||
| 64 | foreach ($this->managedCollections as $coll) { |
||
| 65 | if (isset($this->formFieldDescriptions[$coll])) { |
||
| 66 | if ($coll == 'packagings') { |
||
| 67 | dump($this->formFieldDescriptions[$coll]); |
||
|
|
|||
| 68 | } |
||
| 69 | // preparing stuff |
||
| 70 | if ($admin_code = $this->formFieldDescriptions[$coll]->getOption('admin_code')) { |
||
| 71 | $targetAdmin = $this->getConfigurationPool()->getAdminByAdminCode($admin_code); |
||
| 72 | } else { |
||
| 73 | $target = $this->getModelManager() |
||
| 74 | ->getEntityManager($object) |
||
| 75 | ->getClassMetadata($this->getClass()) |
||
| 76 | ->associationMappings[$coll]['targetEntity'] |
||
| 77 | ; |
||
| 78 | |||
| 79 | $rctarget = new \ReflectionClass($target); |
||
| 80 | $targetAdmin = $this->getConfigurationPool()->getAdminByClass($rctarget->getName()); |
||
| 81 | } |
||
| 82 | |||
| 83 | $rcentity = new \ReflectionClass($this->getClass()); |
||
| 84 | $method = 'get' . ucfirst($coll); |
||
| 85 | |||
| 86 | $subObjects = $object->$method(); |
||
| 87 | |||
| 88 | // insert/update (forcing the foreign key to be set to $this->getId(), for instance) |
||
| 89 | if ($subObjects != null) { |
||
| 90 | foreach ($subObjects as $subobj) { |
||
| 91 | if ($this->formFieldDescriptions[$coll]->getMappingType() != ClassMetadataInfo::MANY_TO_MANY) { |
||
| 92 | $subobj->{'set' . ucfirst($rcentity->getShortName())}($object); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($targetAdmin != null) { |
||
| 96 | $targetAdmin->prePersist($subobj); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | if (!$subObjects instanceof Doctrine\ORM\PersitentCollection || $subObjects->count() == 0) { |
||
| 102 | continue; |
||
| 103 | } |
||
| 104 | |||
| 105 | // delete |
||
| 106 | foreach ($subObjects->getSnapshot() as $subobj) { |
||
| 107 | if (!$subObjects->contains($subobj)) { |
||
| 108 | $this->getModelManager()->delete($subobj); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | return $this; |
||
| 115 | } |
||
| 116 | |||
| 134 |
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: