| Conditions | 10 |
| Paths | 22 |
| Total Lines | 41 |
| Code Lines | 23 |
| 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 |
||
| 49 | public function convert(string $data, string $class, array $context = []): array |
||
| 50 | { |
||
| 51 | if (null !== $this->resourceMetadataFactory) { |
||
| 52 | $resourceMetadata = $this->resourceMetadataFactory->create($class); |
||
| 53 | $class = $resourceMetadata->getOperationAttribute($context, 'output', ['class' => $class], true)['class']; |
||
| 54 | } |
||
| 55 | |||
| 56 | $keys = $this->identifiersExtractor->getIdentifiersFromResourceClass($class); |
||
| 57 | |||
| 58 | if (($numIdentifiers = \count($keys)) > 1) { |
||
| 59 | $identifiers = CompositeIdentifierParser::parse($data); |
||
| 60 | } elseif (0 === $numIdentifiers) { |
||
| 61 | throw new InvalidIdentifierException(sprintf('Resource "%s" has no identifiers.', $class)); |
||
| 62 | } else { |
||
| 63 | $identifiers = [$keys[0] => $data]; |
||
| 64 | } |
||
| 65 | |||
| 66 | // Normalize every identifier (DateTime, UUID etc.) |
||
| 67 | foreach ($keys as $key) { |
||
| 68 | if (!isset($identifiers[$key])) { |
||
| 69 | throw new InvalidIdentifierException(sprintf('Invalid identifier "%1$s", "%1$s" was not found.', $key)); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (null === $type = $this->getIdentifierType($class, $key)) { |
||
| 73 | continue; |
||
| 74 | } |
||
| 75 | |||
| 76 | foreach ($this->identifierDenormalizers as $identifierDenormalizer) { |
||
| 77 | if (!$identifierDenormalizer->supportsDenormalization($identifiers[$key], $type)) { |
||
| 78 | continue; |
||
| 79 | } |
||
| 80 | |||
| 81 | try { |
||
| 82 | $identifiers[$key] = $identifierDenormalizer->denormalize($identifiers[$key], $type); |
||
| 83 | } catch (InvalidIdentifierException $e) { |
||
| 84 | throw new InvalidIdentifierException(sprintf('Identifier "%s" could not be denormalized.', $key), $e->getCode(), $e); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return $identifiers; |
||
| 90 | } |
||
| 101 |