| Conditions | 11 |
| Paths | 48 |
| Total Lines | 52 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 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 |
||
| 62 | public function completeFields(ObjectManager $em, $entity) |
||
| 63 | { |
||
| 64 | $accessor = PropertyAccess::getPropertyAccessor(); |
||
| 65 | |||
| 66 | $metadata = $this->resolver->getMetadataFromObject($em, $entity); |
||
| 67 | |||
| 68 | foreach ($metadata->getColumnNames() as $columnName) { |
||
| 69 | $property = $metadata->getFieldName($columnName); |
||
| 70 | if (false === $metadata->isNullable($property)) { |
||
| 71 | try { |
||
| 72 | if (null === $accessor->getValue($entity, $property)) { |
||
| 73 | $accessor->setValue( |
||
| 74 | $entity, |
||
| 75 | $property, |
||
| 76 | $this->complete($metadata->getFieldMapping($property), $metadata->getName()) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | } catch (\Exception $ex) { |
||
| 80 | unset($ex); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | // Parse associations |
||
| 86 | foreach ($metadata->getAssociationNames() as $associationName) { |
||
| 87 | $mapping = $metadata->getAssociationMapping($associationName); |
||
| 88 | // Ignore if association is a collection (ManyToMany, OneToMany), nullable, or already has a value |
||
| 89 | if ($metadata->isCollectionValuedAssociation($associationName) || |
||
| 90 | !isset($mapping['joinColumns'][0]['nullable']) || |
||
| 91 | $mapping['joinColumns'][0]['nullable'] === true || |
||
| 92 | $accessor->getValue($entity, $associationName) !== null |
||
| 93 | ) { |
||
| 94 | continue; |
||
| 95 | } |
||
| 96 | try { |
||
| 97 | // Create association object |
||
| 98 | $relatedClass = $metadata->getAssociationTargetClass($associationName); |
||
| 99 | $property = new $relatedClass; |
||
| 100 | // Complete required fields |
||
| 101 | $this->completeRequired($em, $property); |
||
| 102 | // Persist association object (prevent cascade persist forgetfulness) |
||
| 103 | $em->persist($property); |
||
| 104 | |||
| 105 | // Set entity association value |
||
| 106 | $accessor->setValue($entity, $associationName, $property); |
||
| 107 | } catch (\Exception $ex) { |
||
| 108 | unset($ex); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | return $this; |
||
| 113 | } |
||
| 114 | |||
| 160 |
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: