Conditions | 15 |
Paths | 9 |
Total Lines | 55 |
Code Lines | 33 |
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 |
||
51 | protected function addLookupsForNestedProperty(string $property, Builder $aggregationBuilder, string $resourceClass): array |
||
52 | { |
||
53 | $propertyParts = $this->splitPropertyParts($property, $resourceClass); |
||
54 | $alias = ''; |
||
55 | |||
56 | foreach ($propertyParts['associations'] as $association) { |
||
57 | $classMetadata = $this->getClassMetadata($resourceClass); |
||
58 | |||
59 | if (!$classMetadata instanceof MongoDbOdmClassMetadata) { |
||
60 | break; |
||
61 | } |
||
62 | |||
63 | if ($classMetadata->hasReference($association)) { |
||
64 | $propertyAlias = "${association}_lkup"; |
||
65 | // previous_association_lkup.association |
||
66 | $localField = "$alias$association"; |
||
67 | // previous_association_lkup.association_lkup |
||
68 | $alias .= $propertyAlias; |
||
69 | $referenceMapping = $classMetadata->getFieldMapping($association); |
||
70 | |||
71 | if (($isOwningSide = $referenceMapping['isOwningSide']) && MongoDbOdmClassMetadata::REFERENCE_STORE_AS_ID !== $referenceMapping['storeAs']) { |
||
72 | throw MappingException::cannotLookupDbRefReference($classMetadata->getReflectionClass()->getShortName(), $association); |
||
73 | } |
||
74 | if (!$isOwningSide) { |
||
75 | if (isset($referenceMapping['repositoryMethod']) || !isset($referenceMapping['mappedBy'])) { |
||
76 | throw MappingException::repositoryMethodLookupNotAllowed($classMetadata->getReflectionClass()->getShortName(), $association); |
||
77 | } |
||
78 | |||
79 | $targetClassMetadata = $this->getClassMetadata($referenceMapping['targetDocument']); |
||
80 | if ($targetClassMetadata instanceof MongoDbOdmClassMetadata && MongoDbOdmClassMetadata::REFERENCE_STORE_AS_ID !== $targetClassMetadata->getFieldMapping($referenceMapping['mappedBy'])['storeAs']) { |
||
81 | throw MappingException::cannotLookupDbRefReference($classMetadata->getReflectionClass()->getShortName(), $association); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $aggregationBuilder->lookup($classMetadata->getAssociationTargetClass($association)) |
||
86 | ->localField($isOwningSide ? $localField : '_id') |
||
87 | ->foreignField($isOwningSide ? '_id' : $referenceMapping['mappedBy']) |
||
88 | ->alias($alias); |
||
89 | $aggregationBuilder->unwind("\$$alias"); |
||
90 | |||
91 | // association.property => association_lkup.property |
||
92 | $property = substr_replace($property, $propertyAlias, strpos($property, $association), \strlen($association)); |
||
93 | $resourceClass = $classMetadata->getAssociationTargetClass($association); |
||
94 | $alias .= '.'; |
||
95 | } elseif ($classMetadata->hasEmbed($association)) { |
||
96 | $alias = "$association."; |
||
97 | $resourceClass = $classMetadata->getAssociationTargetClass($association); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | if ('' === $alias) { |
||
102 | throw new InvalidArgumentException(sprintf('Cannot add lookups for property "%s" - property is not nested.', $property)); |
||
103 | } |
||
104 | |||
105 | return [$property, $propertyParts['field'], $propertyParts['associations']]; |
||
106 | } |
||
108 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.