| Conditions | 10 |
| Paths | 16 |
| Total Lines | 60 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 81 | public function onPreSerialize(ObjectEvent $event) |
||
| 82 | { |
||
| 83 | $object = $event->getObject(); |
||
| 84 | |||
| 85 | if ( |
||
| 86 | $object instanceof \Doctrine\Common\Persistence\Proxy |
||
| 87 | && ! $object->__isInitialized() |
||
| 88 | ) { |
||
| 89 | $object->__load(); |
||
| 90 | } |
||
| 91 | |||
| 92 | $objectUid = spl_object_hash($object); |
||
| 93 | if (in_array($objectUid, $this->serializedObjects)) { |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | $classAnnotation = $this->annotationReader->getClassAnnotation( |
||
| 98 | new \ReflectionClass(ClassUtils::getClass($object)), |
||
| 99 | VichSerializableClass::class |
||
| 100 | ); |
||
| 101 | |||
| 102 | if ($classAnnotation instanceof VichSerializableClass) { |
||
| 103 | $reflectionClass = ClassUtils::newReflectionClass(get_class($object)); |
||
| 104 | $this->logger->debug(sprintf('Found @VichSerializableClass annotation for the class "%s"', $reflectionClass->getName())); |
||
|
|
|||
| 105 | |||
| 106 | foreach ($reflectionClass->getProperties() as $property) { |
||
| 107 | $vichSerializableAnnotation = $this->annotationReader->getPropertyAnnotation($property, VichSerializableField::class); |
||
| 108 | |||
| 109 | if ($vichSerializableAnnotation instanceof VichSerializableField) { |
||
| 110 | $vichUploadableFileAnnotation = $this->annotationReader->getPropertyAnnotation($property, UploadableField::class); |
||
| 111 | |||
| 112 | if ($vichUploadableFileAnnotation instanceof UploadableField) { |
||
| 113 | throw new IncompatibleUploadableAndSerializableFieldAnnotationException(sprintf( |
||
| 114 | 'The field "%s" in the class "%s" cannot have @UploadableField and @VichSerializableField annotations at the same moment.', |
||
| 115 | $property->getName(), |
||
| 116 | $reflectionClass->getName() |
||
| 117 | )); |
||
| 118 | } |
||
| 119 | $this->logger->debug(sprintf( |
||
| 120 | 'Found @VichSerializableField annotation for the field "%s" in the class "%s"', |
||
| 121 | $property->getName(), |
||
| 122 | $reflectionClass->getName() |
||
| 123 | )); |
||
| 124 | |||
| 125 | $uri = null; |
||
| 126 | $property->setAccessible(true); |
||
| 127 | |||
| 128 | if ($property->getValue($event->getObject())) { |
||
| 129 | $uri = $this->storage->resolveUri($object, $vichSerializableAnnotation->getField()); |
||
| 130 | if ($vichSerializableAnnotation->isIncludeHost()) { |
||
| 131 | $uri = $this->getHostUrl().$uri; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | $property->setValue($object, $uri); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $this->serializedObjects[$objectUid] = $objectUid; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 160 |