Conditions | 11 |
Paths | 8 |
Total Lines | 58 |
Code Lines | 35 |
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 |
||
45 | public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass = null, string $operationName = null, array $context = []) |
||
46 | { |
||
47 | if (null === $resourceClass) { |
||
48 | throw new InvalidArgumentException('The "$resourceClass" parameter must not be null'); |
||
49 | } |
||
50 | |||
51 | $em = $queryBuilder->getEntityManager(); |
||
52 | $classMetadata = $em->getClassMetadata($resourceClass); |
||
53 | |||
54 | if (!$this->shouldOperationForceEager($resourceClass, ['collection_operation_name' => $operationName]) && !$this->hasFetchEagerAssociation($em, $classMetadata)) { |
||
55 | return; |
||
56 | } |
||
57 | |||
58 | //If no where part, nothing to do |
||
59 | $wherePart = $queryBuilder->getDQLPart('where'); |
||
60 | |||
61 | if (!$wherePart) { |
||
62 | return; |
||
63 | } |
||
64 | |||
65 | $joinParts = $queryBuilder->getDQLPart('join'); |
||
66 | $originAlias = $queryBuilder->getRootAliases()[0]; |
||
67 | |||
68 | if (!$joinParts || !isset($joinParts[$originAlias])) { |
||
69 | return; |
||
70 | } |
||
71 | |||
72 | $queryBuilderClone = clone $queryBuilder; |
||
73 | $queryBuilderClone->resetDQLPart('where'); |
||
74 | $changedWhereClause = false; |
||
75 | |||
76 | if (!$classMetadata->isIdentifierComposite) { |
||
77 | $replacementAlias = $queryNameGenerator->generateJoinAlias($originAlias); |
||
78 | $in = $this->getQueryBuilderWithNewAliases($queryBuilder, $queryNameGenerator, $originAlias, $replacementAlias); |
||
79 | $in->select($replacementAlias); |
||
80 | $queryBuilderClone->andWhere($queryBuilderClone->expr()->in($originAlias, $in->getDQL())); |
||
81 | $changedWhereClause = true; |
||
82 | } else { |
||
83 | // Because Doctrine doesn't support WHERE ( foo, bar ) IN () (https://github.com/doctrine/doctrine2/issues/5238), we are building as many subqueries as they are identifiers |
||
84 | foreach ($classMetadata->getIdentifier() as $identifier) { |
||
85 | if (!$classMetadata->hasAssociation($identifier)) { |
||
86 | continue; |
||
87 | } |
||
88 | |||
89 | $replacementAlias = $queryNameGenerator->generateJoinAlias($originAlias); |
||
90 | $in = $this->getQueryBuilderWithNewAliases($queryBuilder, $queryNameGenerator, $originAlias, $replacementAlias); |
||
91 | $in->select("IDENTITY($replacementAlias.$identifier)"); |
||
92 | $queryBuilderClone->andWhere($queryBuilderClone->expr()->in("$originAlias.$identifier", $in->getDQL())); |
||
93 | $changedWhereClause = true; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | if (false === $changedWhereClause) { |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | $queryBuilder->resetDQLPart('where'); |
||
102 | $queryBuilder->add('where', $queryBuilderClone->getDQLPart('where')); |
||
103 | } |
||
164 |