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