| Conditions | 5 |
| Paths | 16 |
| Total Lines | 92 |
| Code Lines | 52 |
| 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 |
||
| 137 | protected function getMomentData( |
||
| 138 | array $ownerIds, |
||
| 139 | \DateTime $moment, |
||
| 140 | \DateTime $start = null, |
||
| 141 | \DateTime $end = null, |
||
| 142 | array $filters = [] |
||
| 143 | ) { |
||
| 144 | // clone datetimes as doctrine modifies their timezone which breaks stuff |
||
| 145 | $moment = clone $moment; |
||
| 146 | $start = $start ? clone $start : null; |
||
| 147 | $end = $end ? clone $end : null; |
||
| 148 | |||
| 149 | $qb = $this->getAuditRepository()->createQueryBuilder('a'); |
||
| 150 | $qb |
||
| 151 | ->select(<<<SELECT |
||
| 152 | (SELECT afps.newFloat FROM OroDataAuditBundle:AuditField afps WHERE afps.id = MAX(afp.id)) AS probability, |
||
| 153 | (SELECT afpb.newFloat FROM OroDataAuditBundle:AuditField afpb WHERE afpb.id = MAX(afb.id)) AS budgetAmount |
||
| 154 | SELECT |
||
| 155 | ) |
||
| 156 | ->leftJoin('a.fields', 'afca', Join::WITH, 'afca.field = :closedAtField') |
||
| 157 | ->leftJoin('a.fields', 'afc', Join::WITH, 'afc.field = :closeDateField') |
||
| 158 | ->leftJoin('a.fields', 'afp', Join::WITH, 'afp.field = :probabilityField') |
||
| 159 | ->leftJoin('a.fields', 'afb', Join::WITH, 'afb.field = :budgetAmountField') |
||
| 160 | ->where('a.objectClass = :objectClass AND a.loggedAt < :moment') |
||
| 161 | ->groupBy('a.objectId') |
||
| 162 | ->having(<<<HAVING |
||
| 163 | NOT EXISTS( |
||
| 164 | SELECT |
||
| 165 | afcah.newDatetime |
||
| 166 | FROM OroDataAuditBundle:AuditField afcah |
||
| 167 | WHERE |
||
| 168 | afcah.id = MAX(afca.id) |
||
| 169 | AND afcah.newDatetime IS NOT NULL |
||
| 170 | ) |
||
| 171 | AND EXISTS( |
||
| 172 | SELECT |
||
| 173 | afph.newFloat |
||
| 174 | FROM OroDataAuditBundle:AuditField afph |
||
| 175 | WHERE |
||
| 176 | afph.id = MAX(afp.id) |
||
| 177 | ) |
||
| 178 | HAVING |
||
| 179 | ) |
||
| 180 | ->setParameters([ |
||
| 181 | 'objectClass' => 'OroCRM\Bundle\SalesBundle\Entity\Opportunity', |
||
| 182 | 'closedAtField' => 'closedAt', |
||
| 183 | 'closeDateField' => 'closeDate', |
||
| 184 | 'probabilityField' => 'probability', |
||
| 185 | 'budgetAmountField' => 'budgetAmount', |
||
| 186 | 'moment' => $moment, |
||
| 187 | ]); |
||
| 188 | |||
| 189 | $this->applyHistoryDateFiltering($qb, $start, $end); |
||
| 190 | |||
| 191 | if ($ownerIds) { |
||
|
|
|||
| 192 | $qb |
||
| 193 | ->join('a.fields', 'afo', Join::WITH, 'afo.field = :ownerField') |
||
| 194 | ->andHaving(<<<HAVING |
||
| 195 | EXISTS( |
||
| 196 | SELECT |
||
| 197 | afoh.newText |
||
| 198 | FROM OroDataAuditBundle:AuditField afoh |
||
| 199 | WHERE |
||
| 200 | afoh.id = MAX(afo.id) |
||
| 201 | AND afoh.newText IN (SELECT u.username FROM OroUserBundle:User u WHERE u.id IN (:ownerIds)) |
||
| 202 | ) |
||
| 203 | HAVING |
||
| 204 | ) |
||
| 205 | ->setParameter('ownerField', 'owner') |
||
| 206 | ->setParameter('ownerIds', $ownerIds); |
||
| 207 | } |
||
| 208 | // need to join opportunity to properly apply acl permissions |
||
| 209 | $qb->join('OroCRMSalesBundle:Opportunity', 'o', Join::WITH, 'a.objectId = o.id'); |
||
| 210 | if ($filters) { |
||
| 211 | $this->filterProcessor |
||
| 212 | ->process($qb, 'OroCRM\Bundle\SalesBundle\Entity\Opportunity', $filters, 'o'); |
||
| 213 | } |
||
| 214 | |||
| 215 | $result = $this->aclHelper->apply($qb)->getArrayResult(); |
||
| 216 | |||
| 217 | return array_reduce( |
||
| 218 | $result, |
||
| 219 | function ($result, $row) { |
||
| 220 | $result['inProgressCount']++; |
||
| 221 | $result['budgetAmount'] += $row['budgetAmount']; |
||
| 222 | $result['weightedForecast'] += $row['budgetAmount'] * $row['probability']; |
||
| 223 | |||
| 224 | return $result; |
||
| 225 | }, |
||
| 226 | ['inProgressCount' => 0, 'budgetAmount' => 0, 'weightedForecast' => 0] |
||
| 227 | ); |
||
| 228 | } |
||
| 229 | |||
| 355 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.