| Conditions | 9 |
| Paths | 13 |
| Total Lines | 109 |
| Code Lines | 71 |
| 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 |
||
| 116 | public function create(TypeToken $phpType): ClassMetadata |
||
| 117 | { |
||
| 118 | $class = $phpType->getRawType(); |
||
| 119 | $key = 'gson.classmetadata.'.\str_replace('\\', '', $class); |
||
| 120 | |||
| 121 | $data = $this->cache->get($key); |
||
| 122 | if ($data !== null) { |
||
| 123 | return $data; |
||
| 124 | } |
||
| 125 | |||
| 126 | $properties = new PropertyCollection(); |
||
| 127 | $classMetadata = new DefaultClassMetadata($class, $this->annotationReader->readClass($class, true), $properties); |
||
| 128 | |||
| 129 | $reflectionClass = new ReflectionClass($class); |
||
| 130 | $reflectionProperties = $this->reflectionPropertySetFactory->create($reflectionClass); |
||
| 131 | |||
| 132 | /** @var ReflectionProperty $reflectionProperty */ |
||
| 133 | foreach ($reflectionProperties as $reflectionProperty) { |
||
| 134 | $annotations = $this->annotationReader->readProperty( |
||
| 135 | $reflectionProperty->getName(), |
||
| 136 | $reflectionProperty->getDeclaringClass()->getName(), |
||
| 137 | false, |
||
| 138 | true |
||
| 139 | ); |
||
| 140 | |||
| 141 | $serializedName = $this->propertyNamer->serializedName($reflectionProperty->getName(), $annotations); |
||
| 142 | $getterMethod = $this->accessorMethodProvider->getterMethod($reflectionClass, $reflectionProperty, $annotations); |
||
| 143 | $setterMethod = $this->accessorMethodProvider->setterMethod($reflectionClass, $reflectionProperty, $annotations); |
||
| 144 | $getterStrategy = $this->accessorStrategyFactory->getterStrategy($reflectionProperty, $getterMethod); |
||
| 145 | $setterStrategy = $this->accessorStrategyFactory->setterStrategy($reflectionProperty, $setterMethod); |
||
| 146 | $type = $this->phpTypeFactory->create($annotations, $getterMethod, $setterMethod, $reflectionProperty); |
||
| 147 | |||
| 148 | $property = new Property( |
||
| 149 | $reflectionProperty->getName(), |
||
| 150 | $serializedName, |
||
| 151 | $type, |
||
| 152 | $getterStrategy, |
||
| 153 | $setterStrategy, |
||
| 154 | $annotations, |
||
| 155 | $reflectionProperty->getModifiers(), |
||
| 156 | false, |
||
| 157 | $classMetadata |
||
| 158 | ); |
||
| 159 | |||
| 160 | $skipSerialize = $this->excluder->excludePropertySerialize($property); |
||
| 161 | $skipDeserialize = $this->excluder->excludePropertyDeserialize($property); |
||
| 162 | |||
| 163 | // if we're skipping serialization and deserialization, we don't need |
||
| 164 | // to add the property to the collection |
||
| 165 | if ($skipSerialize && $skipDeserialize) { |
||
| 166 | continue; |
||
| 167 | } |
||
| 168 | |||
| 169 | $property->setSkipSerialize($skipSerialize); |
||
| 170 | $property->setSkipDeserialize($skipDeserialize); |
||
| 171 | |||
| 172 | $properties->add($property); |
||
| 173 | } |
||
| 174 | |||
| 175 | // add virtual properties |
||
| 176 | foreach ($reflectionClass->getMethods() as $reflectionMethod) { |
||
| 177 | $annotations = $this->annotationReader->readMethod( |
||
| 178 | $reflectionMethod->getName(), |
||
| 179 | $reflectionMethod->getDeclaringClass()->getName(), |
||
| 180 | false, |
||
| 181 | true |
||
| 182 | ); |
||
| 183 | if (null === $annotations->get(VirtualProperty::class)) { |
||
| 184 | continue; |
||
| 185 | } |
||
| 186 | |||
| 187 | $serializedName = $this->propertyNamer->serializedName($reflectionMethod->getName(), $annotations); |
||
| 188 | $type = $this->phpTypeFactory->create($annotations, $reflectionMethod); |
||
| 189 | $getterStrategy = new GetByMethod($reflectionMethod->getName()); |
||
| 190 | $setterStrategy = new SetByNull(); |
||
| 191 | |||
| 192 | $property = new Property( |
||
| 193 | $reflectionMethod->getName(), |
||
| 194 | $serializedName, |
||
| 195 | $type, |
||
| 196 | $getterStrategy, |
||
| 197 | $setterStrategy, |
||
| 198 | $annotations, |
||
| 199 | $reflectionMethod->getModifiers(), |
||
| 200 | true, |
||
| 201 | $classMetadata |
||
| 202 | ); |
||
| 203 | |||
| 204 | $skipSerialize = $this->excluder->excludePropertySerialize($property); |
||
| 205 | $skipDeserialize = $this->excluder->excludePropertyDeserialize($property); |
||
| 206 | |||
| 207 | // if we're skipping serialization and deserialization, we don't need |
||
| 208 | // to add the property to the collection |
||
| 209 | if ($skipSerialize && $skipDeserialize) { |
||
| 210 | continue; |
||
| 211 | } |
||
| 212 | |||
| 213 | $property->setSkipSerialize($skipSerialize); |
||
| 214 | $property->setSkipDeserialize($skipDeserialize); |
||
| 215 | |||
| 216 | $properties->add($property); |
||
| 217 | } |
||
| 218 | |||
| 219 | $classMetadata->setSkipSerialize($this->excluder->excludeClassSerialize($classMetadata)); |
||
| 220 | $classMetadata->setSkipDeserialize($this->excluder->excludeClassDeserialize($classMetadata)); |
||
| 221 | |||
| 222 | $this->cache->set($key, $classMetadata); |
||
| 223 | |||
| 224 | return $classMetadata; |
||
| 225 | } |
||
| 227 |