Conditions | 15 |
Paths | 48 |
Total Lines | 53 |
Code Lines | 31 |
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 |
||
38 | private function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array |
||
39 | { |
||
40 | $identifierValues = [$id]; |
||
41 | $doctrineClassMetadata = $manager->getClassMetadata($resourceClass); |
||
42 | $doctrineIdentifierFields = $doctrineClassMetadata->getIdentifier(); |
||
43 | $isOrm = $manager instanceof EntityManagerInterface; |
||
44 | $isOdm = $manager instanceof DocumentManager; |
||
45 | $platform = $isOrm ? $manager->getConnection()->getDatabasePlatform() : null; |
||
46 | $identifiersMap = null; |
||
47 | |||
48 | if (\count($doctrineIdentifierFields) > 1) { |
||
49 | $identifiersMap = []; |
||
50 | |||
51 | // first transform identifiers to a proper key/value array |
||
52 | foreach (explode(';', (string) $id) as $identifier) { |
||
53 | if (!$identifier) { |
||
54 | continue; |
||
55 | } |
||
56 | |||
57 | $identifierPair = explode('=', $identifier); |
||
58 | $identifiersMap[$identifierPair[0]] = $identifierPair[1]; |
||
59 | } |
||
60 | } |
||
61 | |||
62 | $identifiers = []; |
||
63 | $i = 0; |
||
64 | |||
65 | foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
||
66 | $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
||
67 | |||
68 | if (!$propertyMetadata->isIdentifier()) { |
||
69 | continue; |
||
70 | } |
||
71 | |||
72 | $identifier = null === $identifiersMap ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null; |
||
73 | if (null === $identifier) { |
||
74 | throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" was not found.', $id, $propertyName)); |
||
75 | } |
||
76 | |||
77 | $doctrineTypeName = $doctrineClassMetadata->getTypeOfField($propertyName); |
||
78 | |||
79 | if ($isOrm && null !== $doctrineTypeName && DBALType::hasType($doctrineTypeName)) { |
||
80 | $identifier = DBALType::getType($doctrineTypeName)->convertToPHPValue($identifier, $platform); |
||
|
|||
81 | } |
||
82 | if ($isOdm && null !== $doctrineTypeName && MongoDbType::hasType($doctrineTypeName)) { |
||
83 | $identifier = MongoDbType::getType($doctrineTypeName)->convertToPHPValue($identifier); |
||
84 | } |
||
85 | |||
86 | $identifiers[$propertyName] = $identifier; |
||
87 | ++$i; |
||
88 | } |
||
89 | |||
90 | return $identifiers; |
||
91 | } |
||
93 |