Conditions | 6 |
Paths | 4 |
Total Lines | 51 |
Code Lines | 34 |
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 |
||
111 | private function getQueryBuilderWithNewAliases(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $originAlias = 'o', string $replacement = 'o_2'): QueryBuilder |
||
112 | { |
||
113 | $queryBuilderClone = clone $queryBuilder; |
||
114 | |||
115 | $joinParts = $queryBuilder->getDQLPart('join'); |
||
116 | $wherePart = $queryBuilder->getDQLPart('where'); |
||
117 | |||
118 | //reset parts |
||
119 | $queryBuilderClone->resetDQLPart('join'); |
||
120 | $queryBuilderClone->resetDQLPart('where'); |
||
121 | $queryBuilderClone->resetDQLPart('orderBy'); |
||
122 | $queryBuilderClone->resetDQLPart('groupBy'); |
||
123 | $queryBuilderClone->resetDQLPart('having'); |
||
124 | |||
125 | //Change from alias |
||
126 | $from = $queryBuilderClone->getDQLPart('from')[0]; |
||
127 | $queryBuilderClone->resetDQLPart('from'); |
||
128 | $queryBuilderClone->from($from->getFrom(), $replacement); |
||
129 | |||
130 | $aliases = ["$originAlias."]; |
||
131 | $replacements = ["$replacement."]; |
||
132 | |||
133 | //Change join aliases |
||
134 | foreach ($joinParts[$originAlias] as $joinPart) { |
||
135 | /** @var Join $joinPart */ |
||
136 | $joinString = str_replace($aliases, $replacements, $joinPart->getJoin()); |
||
137 | $pos = strpos($joinString, '.'); |
||
138 | if (false === $pos) { |
||
139 | if (null !== $joinPart->getCondition() && null !== $this->resourceClassResolver && $this->resourceClassResolver->isResourceClass($joinString)) { |
||
140 | $newAlias = $queryNameGenerator->generateJoinAlias($joinPart->getAlias()); |
||
141 | $aliases[] = "{$joinPart->getAlias()}."; |
||
142 | $replacements[] = "$newAlias."; |
||
143 | $condition = str_replace($aliases, $replacements, $joinPart->getCondition()); |
||
144 | $join = new Join($joinPart->getJoinType(), $joinPart->getJoin(), $newAlias, $joinPart->getConditionType(), $condition); |
||
145 | $queryBuilderClone->add('join', [$replacement => $join], true); |
||
146 | } |
||
147 | |||
148 | continue; |
||
149 | } |
||
150 | $alias = substr($joinString, 0, $pos); |
||
151 | $association = substr($joinString, $pos + 1); |
||
152 | $newAlias = $queryNameGenerator->generateJoinAlias($association); |
||
153 | $aliases[] = "{$joinPart->getAlias()}."; |
||
154 | $replacements[] = "$newAlias."; |
||
155 | $condition = str_replace($aliases, $replacements, $joinPart->getCondition()); |
||
156 | QueryBuilderHelper::addJoinOnce($queryBuilderClone, $queryNameGenerator, $alias, $association, $joinPart->getJoinType(), $joinPart->getConditionType(), $condition, $originAlias, $newAlias); |
||
157 | } |
||
158 | |||
159 | $queryBuilderClone->add('where', str_replace($aliases, $replacements, (string) $wherePart)); |
||
160 | |||
161 | return $queryBuilderClone; |
||
162 | } |
||
164 |