Conditions | 10 |
Paths | 135 |
Total Lines | 69 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 2 | Features | 1 |
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 |
||
109 | private function getFixedQueryBuilder(QueryBuilder $queryBuilder) |
||
110 | { |
||
111 | $queryBuilderId = clone $queryBuilder; |
||
112 | $rootAlias = current($queryBuilderId->getRootAliases()); |
||
113 | |||
114 | // step 1 : retrieve the targeted class |
||
115 | $from = $queryBuilderId->getDQLPart('from'); |
||
116 | $class = $from[0]->getFrom(); |
||
117 | $metadata = $queryBuilderId->getEntityManager()->getMetadataFactory()->getMetadataFor($class); |
||
118 | |||
119 | // step 2 : retrieve identifier columns |
||
120 | $idNames = $metadata->getIdentifierFieldNames(); |
||
121 | |||
122 | // step 3 : retrieve the different subjects ids |
||
123 | $selects = array(); |
||
124 | $idxSelect = ''; |
||
125 | foreach ($idNames as $idName) { |
||
126 | $select = sprintf('%s.%s', $rootAlias, $idName); |
||
127 | // Put the ID select on this array to use it on results QB |
||
128 | $selects[$idName] = $select; |
||
129 | // Use IDENTITY if id is a relation too. See: http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html |
||
130 | // Should work only with doctrine/orm: ~2.2 |
||
131 | $idSelect = $select; |
||
132 | if ($metadata->hasAssociation($idName)) { |
||
133 | $idSelect = sprintf('IDENTITY(%s) as %s', $idSelect, $idName); |
||
134 | } |
||
135 | $idxSelect .= ($idxSelect !== '' ? ', ' : '').$idSelect; |
||
136 | } |
||
137 | $queryBuilderId->resetDQLPart('select'); |
||
138 | $queryBuilderId->add('select', 'DISTINCT '.$idxSelect); |
||
|
|||
139 | |||
140 | // for SELECT DISTINCT, ORDER BY expressions must appear in idxSelect list |
||
141 | /* Consider |
||
142 | SELECT DISTINCT x FROM tab ORDER BY y; |
||
143 | For any particular x-value in the table there might be many different y |
||
144 | values. Which one will you use to sort that x-value in the output? |
||
145 | */ |
||
146 | // todo : check how doctrine behave, potential SQL injection here ... |
||
147 | if ($this->getSortBy()) { |
||
148 | $sortBy = $this->getSortBy(); |
||
149 | if (strpos($sortBy, '.') === false) { // add the current alias |
||
150 | $sortBy = $rootAlias.'.'.$sortBy; |
||
151 | } |
||
152 | $sortBy .= ' AS __order_by'; |
||
153 | $queryBuilderId->addSelect($sortBy); |
||
154 | } |
||
155 | |||
156 | $results = $queryBuilderId->getQuery()->execute(array(), Query::HYDRATE_ARRAY); |
||
157 | $idxMatrix = array(); |
||
158 | foreach ($results as $id) { |
||
159 | foreach ($idNames as $idName) { |
||
160 | $idxMatrix[$idName][] = $id[$idName]; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // step 4 : alter the query to match the targeted ids |
||
165 | foreach ($idxMatrix as $idName => $idx) { |
||
166 | if (count($idx) > 0) { |
||
167 | $idxParamName = sprintf('%s_idx', $idName); |
||
168 | $idxParamName = preg_replace('/[^\w]+/', '_', $idxParamName); |
||
169 | $queryBuilder->andWhere(sprintf('%s IN (:%s)', $selects[$idName], $idxParamName)); |
||
170 | $queryBuilder->setParameter($idxParamName, $idx); |
||
171 | $queryBuilder->setMaxResults(null); |
||
172 | $queryBuilder->setFirstResult(null); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | return $queryBuilder; |
||
177 | } |
||
178 | |||
341 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: