| Conditions | 70 |
| Paths | > 20000 |
| Total Lines | 219 |
| Code Lines | 165 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 78 | public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata |
||
| 79 | { |
||
| 80 | $configured = false; |
||
| 81 | |||
| 82 | $classMetadata = new ClassMetadata($name = $class->name); |
||
| 83 | $fileResource = $class->getFilename(); |
||
| 84 | |||
| 85 | if (false !== $fileResource) { |
||
| 86 | $classMetadata->fileResources[] = $fileResource; |
||
| 87 | } |
||
| 88 | |||
| 89 | $propertiesMetadata = []; |
||
| 90 | $propertiesAnnotations = []; |
||
| 91 | |||
| 92 | $exclusionPolicy = ExclusionPolicy::NONE; |
||
| 93 | $excludeAll = false; |
||
| 94 | $classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY; |
||
| 95 | $readOnlyClass = false; |
||
| 96 | |||
| 97 | foreach ($this->getClassAnnotations($class) as $annot) { |
||
| 98 | $configured = true; |
||
| 99 | |||
| 100 | if ($annot instanceof ExclusionPolicy) { |
||
| 101 | $exclusionPolicy = $annot->policy; |
||
| 102 | } elseif ($annot instanceof XmlRoot) { |
||
| 103 | $classMetadata->xmlRootName = $annot->name; |
||
| 104 | $classMetadata->xmlRootNamespace = $annot->namespace; |
||
| 105 | $classMetadata->xmlRootPrefix = $annot->prefix; |
||
| 106 | } elseif ($annot instanceof XmlNamespace) { |
||
| 107 | $classMetadata->registerNamespace($annot->uri, $annot->prefix); |
||
|
|
|||
| 108 | } elseif ($annot instanceof Exclude) { |
||
| 109 | if (null !== $annot->if) { |
||
| 110 | $classMetadata->excludeIf = $this->parseExpression($annot->if); |
||
| 111 | } else { |
||
| 112 | $excludeAll = true; |
||
| 113 | } |
||
| 114 | } elseif ($annot instanceof AccessType) { |
||
| 115 | $classAccessType = $annot->type; |
||
| 116 | } elseif ($annot instanceof ReadOnlyProperty) { |
||
| 117 | $readOnlyClass = true; |
||
| 118 | } elseif ($annot instanceof AccessorOrder) { |
||
| 119 | $classMetadata->setAccessorOrder($annot->order, $annot->custom); |
||
| 120 | } elseif ($annot instanceof Discriminator) { |
||
| 121 | if ($annot->disabled) { |
||
| 122 | $classMetadata->discriminatorDisabled = true; |
||
| 123 | } else { |
||
| 124 | $classMetadata->setDiscriminator($annot->field, $annot->map, $annot->groups); |
||
| 125 | } |
||
| 126 | } elseif ($annot instanceof XmlDiscriminator) { |
||
| 127 | $classMetadata->xmlDiscriminatorAttribute = (bool) $annot->attribute; |
||
| 128 | $classMetadata->xmlDiscriminatorCData = (bool) $annot->cdata; |
||
| 129 | $classMetadata->xmlDiscriminatorNamespace = $annot->namespace ? (string) $annot->namespace : null; |
||
| 130 | } elseif ($annot instanceof VirtualProperty) { |
||
| 131 | $virtualPropertyMetadata = new ExpressionPropertyMetadata( |
||
| 132 | $name, |
||
| 133 | $annot->name, |
||
| 134 | $this->parseExpression($annot->exp) |
||
| 135 | ); |
||
| 136 | $propertiesMetadata[] = $virtualPropertyMetadata; |
||
| 137 | $propertiesAnnotations[] = $annot->options; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | foreach ($class->getMethods() as $method) { |
||
| 142 | if ($method->class !== $name) { |
||
| 143 | continue; |
||
| 144 | } |
||
| 145 | |||
| 146 | $methodAnnotations = $this->getMethodAnnotations($method); |
||
| 147 | |||
| 148 | foreach ($methodAnnotations as $annot) { |
||
| 149 | $configured = true; |
||
| 150 | |||
| 151 | if ($annot instanceof PreSerialize) { |
||
| 152 | $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->name)); |
||
| 153 | continue 2; |
||
| 154 | } elseif ($annot instanceof PostDeserialize) { |
||
| 155 | $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->name)); |
||
| 156 | continue 2; |
||
| 157 | } elseif ($annot instanceof PostSerialize) { |
||
| 158 | $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->name)); |
||
| 159 | continue 2; |
||
| 160 | } elseif ($annot instanceof VirtualProperty) { |
||
| 161 | $virtualPropertyMetadata = new VirtualPropertyMetadata($name, $method->name); |
||
| 162 | $propertiesMetadata[] = $virtualPropertyMetadata; |
||
| 163 | $propertiesAnnotations[] = $methodAnnotations; |
||
| 164 | continue 2; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | if (!$excludeAll) { |
||
| 170 | foreach ($class->getProperties() as $property) { |
||
| 171 | if ($property->class !== $name || (isset($property->info) && $property->info['class'] !== $name)) { |
||
| 172 | continue; |
||
| 173 | } |
||
| 174 | |||
| 175 | $propertiesMetadata[] = new PropertyMetadata($name, $property->getName()); |
||
| 176 | $propertiesAnnotations[] = $this->getPropertyAnnotations($property); |
||
| 177 | } |
||
| 178 | |||
| 179 | foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) { |
||
| 180 | $isExclude = false; |
||
| 181 | $isExpose = $propertyMetadata instanceof VirtualPropertyMetadata |
||
| 182 | || $propertyMetadata instanceof ExpressionPropertyMetadata; |
||
| 183 | $propertyMetadata->readOnly = $propertyMetadata->readOnly || $readOnlyClass; |
||
| 184 | $accessType = $classAccessType; |
||
| 185 | $accessor = [null, null]; |
||
| 186 | |||
| 187 | $propertyAnnotations = $propertiesAnnotations[$propertyKey]; |
||
| 188 | |||
| 189 | foreach ($propertyAnnotations as $annot) { |
||
| 190 | $configured = true; |
||
| 191 | |||
| 192 | if ($annot instanceof Since) { |
||
| 193 | $propertyMetadata->sinceVersion = $annot->version; |
||
| 194 | } elseif ($annot instanceof Until) { |
||
| 195 | $propertyMetadata->untilVersion = $annot->version; |
||
| 196 | } elseif ($annot instanceof SerializedName) { |
||
| 197 | $propertyMetadata->serializedName = $annot->name; |
||
| 198 | } elseif ($annot instanceof SkipWhenEmpty) { |
||
| 199 | $propertyMetadata->skipWhenEmpty = true; |
||
| 200 | } elseif ($annot instanceof Expose) { |
||
| 201 | $isExpose = true; |
||
| 202 | if (null !== $annot->if) { |
||
| 203 | $propertyMetadata->excludeIf = $this->parseExpression('!(' . $annot->if . ')'); |
||
| 204 | } |
||
| 205 | } elseif ($annot instanceof Exclude) { |
||
| 206 | if (null !== $annot->if) { |
||
| 207 | $propertyMetadata->excludeIf = $this->parseExpression($annot->if); |
||
| 208 | } else { |
||
| 209 | $isExclude = true; |
||
| 210 | } |
||
| 211 | } elseif ($annot instanceof Type) { |
||
| 212 | $propertyMetadata->setType($this->typeParser->parse($annot->name)); |
||
| 213 | } elseif ($annot instanceof XmlElement) { |
||
| 214 | $propertyMetadata->xmlAttribute = false; |
||
| 215 | $propertyMetadata->xmlElementCData = $annot->cdata; |
||
| 216 | $propertyMetadata->xmlNamespace = $annot->namespace; |
||
| 217 | } elseif ($annot instanceof XmlList) { |
||
| 218 | $propertyMetadata->xmlCollection = true; |
||
| 219 | $propertyMetadata->xmlCollectionInline = $annot->inline; |
||
| 220 | $propertyMetadata->xmlEntryName = $annot->entry; |
||
| 221 | $propertyMetadata->xmlEntryNamespace = $annot->namespace; |
||
| 222 | $propertyMetadata->xmlCollectionSkipWhenEmpty = $annot->skipWhenEmpty; |
||
| 223 | } elseif ($annot instanceof XmlMap) { |
||
| 224 | $propertyMetadata->xmlCollection = true; |
||
| 225 | $propertyMetadata->xmlCollectionInline = $annot->inline; |
||
| 226 | $propertyMetadata->xmlEntryName = $annot->entry; |
||
| 227 | $propertyMetadata->xmlEntryNamespace = $annot->namespace; |
||
| 228 | $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute; |
||
| 229 | } elseif ($annot instanceof XmlKeyValuePairs) { |
||
| 230 | $propertyMetadata->xmlKeyValuePairs = true; |
||
| 231 | } elseif ($annot instanceof XmlAttribute) { |
||
| 232 | $propertyMetadata->xmlAttribute = true; |
||
| 233 | $propertyMetadata->xmlNamespace = $annot->namespace; |
||
| 234 | } elseif ($annot instanceof XmlValue) { |
||
| 235 | $propertyMetadata->xmlValue = true; |
||
| 236 | $propertyMetadata->xmlElementCData = $annot->cdata; |
||
| 237 | } elseif ($annot instanceof AccessType) { |
||
| 238 | $accessType = $annot->type; |
||
| 239 | } elseif ($annot instanceof ReadOnlyProperty) { |
||
| 240 | $propertyMetadata->readOnly = $annot->readOnly; |
||
| 241 | } elseif ($annot instanceof Accessor) { |
||
| 242 | $accessor = [$annot->getter, $annot->setter]; |
||
| 243 | } elseif ($annot instanceof Groups) { |
||
| 244 | $propertyMetadata->groups = $annot->groups; |
||
| 245 | foreach ((array) $propertyMetadata->groups as $groupName) { |
||
| 246 | if (false !== strpos($groupName, ',')) { |
||
| 247 | throw new InvalidMetadataException(sprintf( |
||
| 248 | 'Invalid group name "%s" on "%s", did you mean to create multiple groups?', |
||
| 249 | implode(', ', $propertyMetadata->groups), |
||
| 250 | $propertyMetadata->class . '->' . $propertyMetadata->name |
||
| 251 | )); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } elseif ($annot instanceof Inline) { |
||
| 255 | $propertyMetadata->inline = true; |
||
| 256 | } elseif ($annot instanceof XmlAttributeMap) { |
||
| 257 | $propertyMetadata->xmlAttributeMap = true; |
||
| 258 | } elseif ($annot instanceof MaxDepth) { |
||
| 259 | $propertyMetadata->maxDepth = $annot->depth; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | if ($propertyMetadata->inline) { |
||
| 264 | $classMetadata->isList = $classMetadata->isList || PropertyMetadata::isCollectionList($propertyMetadata->type); |
||
| 265 | $classMetadata->isMap = $classMetadata->isMap || PropertyMetadata::isCollectionMap($propertyMetadata->type); |
||
| 266 | |||
| 267 | if ($classMetadata->isMap && $classMetadata->isList) { |
||
| 268 | throw new InvalidMetadataException('Can not have an inline map and and inline map on the same class'); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | if (!$propertyMetadata->serializedName) { |
||
| 273 | $propertyMetadata->serializedName = $this->namingStrategy->translateName($propertyMetadata); |
||
| 274 | } |
||
| 275 | |||
| 276 | foreach ($propertyAnnotations as $annot) { |
||
| 277 | if ($annot instanceof VirtualProperty && null !== $annot->name) { |
||
| 278 | $propertyMetadata->name = $annot->name; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | if ( |
||
| 283 | (ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude) |
||
| 284 | || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose) |
||
| 285 | ) { |
||
| 286 | $propertyMetadata->setAccessor($accessType, $accessor[0], $accessor[1]); |
||
| 287 | $classMetadata->addPropertyMetadata($propertyMetadata); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | if (!$configured) { |
||
| 293 | return null; |
||
| 294 | } |
||
| 295 | |||
| 296 | return $classMetadata; |
||
| 297 | } |
||
| 368 |