Conditions | 8 |
Paths | 42 |
Total Lines | 70 |
Code Lines | 37 |
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 |
||
115 | public function walkSelectStatement(Query\AST\SelectStatement $selectStatement) |
||
116 | { |
||
117 | // Get the DQL aliases of all the classes we want to modify |
||
118 | $dqlAliases = []; |
||
119 | |||
120 | foreach ($this->getQueryComponents() as $dqlAlias => $comp) { |
||
121 | // Hard-coded check just for demonstration: We want to modify the query if |
||
122 | // it involves the CmsUser class. |
||
123 | if ($comp['metadata']->getClassName() == CmsUser::class) { |
||
124 | $dqlAliases[] = $dqlAlias; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // Create our conditions for all involved classes |
||
129 | $factors = []; |
||
130 | foreach ($dqlAliases as $alias) { |
||
131 | $pathExpr = new Query\AST\PathExpression(Query\AST\PathExpression::TYPE_STATE_FIELD, $alias, 'id'); |
||
132 | $pathExpr->type = Query\AST\PathExpression::TYPE_STATE_FIELD; |
||
133 | $comparisonExpr = new Query\AST\ComparisonExpression($pathExpr, '=', 1); |
||
134 | |||
135 | $condPrimary = new Query\AST\ConditionalPrimary; |
||
136 | $condPrimary->simpleConditionalExpression = $comparisonExpr; |
||
137 | |||
138 | $factor = new Query\AST\ConditionalFactor($condPrimary); |
||
139 | $factors[] = $factor; |
||
140 | } |
||
141 | |||
142 | if (($whereClause = $selectStatement->whereClause) !== null) { |
||
143 | // There is already a WHERE clause, so append the conditions |
||
144 | $condExpr = $whereClause->conditionalExpression; |
||
145 | |||
146 | // Since Phase 1 AST optimizations were included, we need to re-add the ConditionalExpression |
||
147 | if ( ! ($condExpr instanceof Query\AST\ConditionalExpression)) { |
||
148 | $condExpr = new Query\AST\ConditionalExpression([$condExpr]); |
||
149 | |||
150 | $whereClause->conditionalExpression = $condExpr; |
||
151 | } |
||
152 | |||
153 | $existingTerms = $whereClause->conditionalExpression->conditionalTerms; |
||
154 | |||
155 | if (count($existingTerms) > 1) { |
||
156 | // More than one term, so we need to wrap all these terms in a single root term |
||
157 | // i.e: "WHERE u.name = :foo or u.other = :bar" => "WHERE (u.name = :foo or u.other = :bar) AND <our condition>" |
||
158 | |||
159 | $primary = new Query\AST\ConditionalPrimary; |
||
160 | $primary->conditionalExpression = new Query\AST\ConditionalExpression($existingTerms); |
||
161 | $existingFactor = new Query\AST\ConditionalFactor($primary); |
||
162 | $term = new Query\AST\ConditionalTerm(array_merge([$existingFactor], $factors)); |
||
163 | |||
164 | $selectStatement->whereClause->conditionalExpression->conditionalTerms = [$term]; |
||
165 | } else { |
||
166 | // Just one term so we can simply append our factors to that term |
||
167 | $singleTerm = $selectStatement->whereClause->conditionalExpression->conditionalTerms[0]; |
||
168 | |||
169 | // Since Phase 1 AST optimizations were included, we need to re-add the ConditionalExpression |
||
170 | if ( ! ($singleTerm instanceof Query\AST\ConditionalTerm)) { |
||
171 | $singleTerm = new Query\AST\ConditionalTerm([$singleTerm]); |
||
172 | |||
173 | $selectStatement->whereClause->conditionalExpression->conditionalTerms[0] = $singleTerm; |
||
174 | } |
||
175 | |||
176 | $singleTerm->conditionalFactors = array_merge($singleTerm->conditionalFactors, $factors); |
||
177 | $selectStatement->whereClause->conditionalExpression->conditionalTerms = [$singleTerm]; |
||
178 | } |
||
179 | } else { |
||
180 | // Create a new WHERE clause with our factors |
||
181 | $term = new Query\AST\ConditionalTerm($factors); |
||
182 | $condExpr = new Query\AST\ConditionalExpression([$term]); |
||
183 | $whereClause = new Query\AST\WhereClause($condExpr); |
||
184 | $selectStatement->whereClause = $whereClause; |
||
185 | } |
||
231 |