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