| Conditions | 12 |
| Paths | 17 |
| Total Lines | 64 |
| 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 |
||
| 88 | */ |
||
| 89 | public function onPreSerialize(PreSerializeEvent $event): void |
||
| 90 | { |
||
| 91 | $object = $event->getObject(); |
||
| 92 | if (!\is_object($object)) { |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($object instanceof Proxy && !$object->__isInitialized()) { |
||
| 97 | $object->__load(); |
||
| 98 | } |
||
| 99 | |||
| 100 | $objectUid = \spl_object_hash($object); |
||
| 101 | if (\array_key_exists($objectUid, $this->serializedObjects)) { |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** @var class-string<object> $className */ |
||
| 106 | $className = ClassUtils::getClass($object); |
||
| 107 | |||
| 108 | $classAnnotation = $this->annotationReader->getClassAnnotation( |
||
| 109 | new \ReflectionClass($className), |
||
| 110 | VichSerializableClass::class |
||
| 111 | ); |
||
| 112 | |||
| 113 | if ($classAnnotation instanceof VichSerializableClass) { |
||
| 114 | $reflectionClass = ClassUtils::newReflectionClass(\get_class($object)); |
||
| 115 | $this->logger->debug(\sprintf( |
||
| 116 | 'Found @VichSerializableClass annotation for the class "%s"', |
||
| 117 | $reflectionClass->getName() |
||
| 118 | )); |
||
| 119 | |||
| 120 | foreach ($reflectionClass->getProperties() as $property) { |
||
| 121 | $vichSerializableAnnotation = $this->annotationReader->getPropertyAnnotation($property, VichSerializableField::class); |
||
| 122 | |||
| 123 | if ($vichSerializableAnnotation instanceof VichSerializableField) { |
||
| 124 | $vichUploadableFileAnnotation = $this->annotationReader->getPropertyAnnotation($property, UploadableField::class); |
||
| 125 | |||
| 126 | if ($vichUploadableFileAnnotation instanceof UploadableField) { |
||
| 127 | $exceptionMessage = \sprintf( |
||
| 128 | 'The field "%s" in the class "%s" cannot have @UploadableField and @VichSerializableField annotations at the same moment.', |
||
| 129 | $property->getName(), |
||
| 130 | $reflectionClass->getName() |
||
| 131 | ); |
||
| 132 | |||
| 133 | throw new IncompatibleUploadableAndSerializableFieldAnnotationException($exceptionMessage); |
||
| 134 | } |
||
| 135 | $this->logger->debug(\sprintf( |
||
| 136 | 'Found @VichSerializableField annotation for the field "%s" in the class "%s"', |
||
| 137 | $property->getName(), |
||
| 138 | $reflectionClass->getName() |
||
| 139 | )); |
||
| 140 | |||
| 141 | $uri = null; |
||
| 142 | $property->setAccessible(true); |
||
| 143 | |||
| 144 | if ($property->getValue($event->getObject())) { |
||
| 145 | $uri = $this->storage->resolveUri($object, $vichSerializableAnnotation->getField()); |
||
| 146 | if ($vichSerializableAnnotation->isIncludeHost() && false === \filter_var($uri, FILTER_VALIDATE_URL)) { |
||
| 147 | $uri = $this->getHostUrl().$uri; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | $this->serializedObjects[$objectUid][$property->getName()] = $property->getValue($event->getObject()); |
||
| 151 | $property->setValue($object, $uri); |
||
| 152 | } |
||
| 205 |