| Conditions | 7 |
| Paths | 16 |
| Total Lines | 67 |
| Code Lines | 41 |
| 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 |
||
| 36 | protected function compute() |
||
| 37 | { |
||
| 38 | // We quote in MySQL because of MagicQuery that will be applied. |
||
| 39 | $mySqlPlatform = new MySqlPlatform(); |
||
| 40 | |||
| 41 | $columnsList = null; |
||
| 42 | |||
| 43 | $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable); |
||
| 44 | |||
| 45 | list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy, false); |
||
| 46 | |||
| 47 | $sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM '.$this->from; |
||
| 48 | |||
| 49 | // Let's compute the COUNT. |
||
| 50 | $pkColumnNames = $this->tdbmService->getPrimaryKeyColumns($this->mainTable); |
||
| 51 | $pkColumnNames = array_map(function ($pkColumn) use ($mySqlPlatform) { |
||
| 52 | return $mySqlPlatform->quoteIdentifier($this->mainTable).'.'.$mySqlPlatform->quoteIdentifier($pkColumn); |
||
| 53 | }, $pkColumnNames); |
||
| 54 | |||
| 55 | $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from; |
||
| 56 | |||
| 57 | // Add joins on inherited tables if necessary |
||
| 58 | if (count($allFetchedTables) > 1) { |
||
| 59 | $joinSql = ''; |
||
| 60 | $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable); |
||
| 61 | foreach ($parentFks as $fk) { |
||
| 62 | $joinSql .= sprintf( |
||
| 63 | ' JOIN %s ON (%s.%s = %s.%s)', |
||
| 64 | $mySqlPlatform->quoteIdentifier($fk->getForeignTableName()), |
||
| 65 | $mySqlPlatform->quoteIdentifier($fk->getLocalTableName()), |
||
| 66 | $mySqlPlatform->quoteIdentifier($fk->getUnquotedLocalColumns()[0]), |
||
| 67 | $mySqlPlatform->quoteIdentifier($fk->getForeignTableName()), |
||
| 68 | $mySqlPlatform->quoteIdentifier($fk->getUnquotedForeignColumns()[0]) |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | $childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable); |
||
| 73 | foreach ($childrenFks as $fk) { |
||
| 74 | $joinSql .= sprintf( |
||
| 75 | ' LEFT JOIN %s ON (%s.%s = %s.%s)', |
||
| 76 | $mySqlPlatform->quoteIdentifier($fk->getLocalTableName()), |
||
| 77 | $mySqlPlatform->quoteIdentifier($fk->getForeignTableName()), |
||
| 78 | $mySqlPlatform->quoteIdentifier($fk->getUnquotedForeignColumns()[0]), |
||
| 79 | $mySqlPlatform->quoteIdentifier($fk->getLocalTableName()), |
||
| 80 | $mySqlPlatform->quoteIdentifier($fk->getUnquotedLocalColumns()[0]) |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | |||
| 84 | $sql .= $joinSql; |
||
| 85 | } |
||
| 86 | |||
| 87 | if (!empty($this->filterString)) { |
||
| 88 | $sql .= ' WHERE '.$this->filterString; |
||
| 89 | $countSql .= ' WHERE '.$this->filterString; |
||
| 90 | } |
||
| 91 | |||
| 92 | if (!empty($orderString)) { |
||
| 93 | $sql .= ' ORDER BY '.$orderString; |
||
| 94 | } |
||
| 95 | |||
| 96 | if (stripos($countSql, 'GROUP BY') !== false) { |
||
| 97 | throw new TDBMException('Unsupported use of GROUP BY in SQL request.'); |
||
| 98 | } |
||
| 99 | |||
| 100 | $this->magicSql = $sql; |
||
| 101 | $this->magicSqlCount = $countSql; |
||
| 102 | $this->columnDescList = $columnDescList; |
||
| 103 | } |
||
| 187 |