| Conditions | 18 |
| Paths | 282 |
| Total Lines | 97 |
| Code Lines | 59 |
| 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 |
||
| 61 | protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null) |
||
| 62 | { |
||
| 63 | // From the table name and the additional tables we want to fetch, let's build a list of all tables |
||
| 64 | // that must be part of the select columns. |
||
| 65 | |||
| 66 | $connection = $this->tdbmService->getConnection(); |
||
| 67 | |||
| 68 | $tableGroups = []; |
||
| 69 | $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable); |
||
| 70 | $tableGroupName = $this->getTableGroupName($allFetchedTables); |
||
| 71 | foreach ($allFetchedTables as $table) { |
||
| 72 | $tableGroups[$table] = $tableGroupName; |
||
| 73 | } |
||
| 74 | |||
| 75 | $columnsList = []; |
||
| 76 | $columnDescList = []; |
||
| 77 | $sortColumn = 0; |
||
| 78 | $reconstructedOrderBy = null; |
||
| 79 | |||
| 80 | if (is_string($orderBy)) { |
||
| 81 | $orderBy = trim($orderBy); |
||
| 82 | if ($orderBy === '') { |
||
| 83 | $orderBy = null; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | // Now, let's deal with "order by columns" |
||
| 88 | if ($orderBy !== null) { |
||
| 89 | if ($orderBy instanceof UncheckedOrderBy) { |
||
| 90 | $securedOrderBy = false; |
||
| 91 | $orderBy = $orderBy->getOrderBy(); |
||
| 92 | $reconstructedOrderBy = $orderBy; |
||
| 93 | } else { |
||
| 94 | $securedOrderBy = true; |
||
| 95 | $reconstructedOrderBys = []; |
||
| 96 | } |
||
| 97 | $orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy); |
||
| 98 | |||
| 99 | // If we sort by a column, there is a high chance we will fetch the bean containing this column. |
||
| 100 | // Hence, we should add the table to the $additionalTablesFetch |
||
| 101 | foreach ($orderByColumns as $orderByColumn) { |
||
| 102 | if ($orderByColumn['type'] === 'colref') { |
||
| 103 | if ($orderByColumn['table'] !== null) { |
||
| 104 | $additionalTablesFetch[] = $orderByColumn['table']; |
||
| 105 | } |
||
| 106 | if ($securedOrderBy) { |
||
| 107 | $reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $orderByColumn['table'].'.' : '').$orderByColumn['column'].' '.$orderByColumn['direction']; |
||
|
|
|||
| 108 | } |
||
| 109 | } elseif ($orderByColumn['type'] === 'expr') { |
||
| 110 | $sortColumnName = 'sort_column_'.$sortColumn; |
||
| 111 | $columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName; |
||
| 112 | $columnDescList[] = [ |
||
| 113 | 'tableGroup' => null, |
||
| 114 | ]; |
||
| 115 | ++$sortColumn; |
||
| 116 | |||
| 117 | if ($securedOrderBy) { |
||
| 118 | throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")'); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($reconstructedOrderBy === null) { |
||
| 124 | $reconstructedOrderBy = implode(', ', $reconstructedOrderBys); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | foreach ($additionalTablesFetch as $additionalTable) { |
||
| 129 | $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable); |
||
| 130 | $tableGroupName = $this->getTableGroupName($relatedTables); |
||
| 131 | foreach ($relatedTables as $table) { |
||
| 132 | $tableGroups[$table] = $tableGroupName; |
||
| 133 | } |
||
| 134 | $allFetchedTables = array_merge($allFetchedTables, $relatedTables); |
||
| 135 | } |
||
| 136 | |||
| 137 | // Let's remove any duplicate |
||
| 138 | $allFetchedTables = array_flip(array_flip($allFetchedTables)); |
||
| 139 | |||
| 140 | // Now, let's build the column list |
||
| 141 | foreach ($allFetchedTables as $table) { |
||
| 142 | foreach ($this->schema->getTable($table)->getColumns() as $column) { |
||
| 143 | $columnName = $column->getName(); |
||
| 144 | $columnDescList[] = [ |
||
| 145 | 'as' => $table.'____'.$columnName, |
||
| 146 | 'table' => $table, |
||
| 147 | 'column' => $columnName, |
||
| 148 | 'type' => $column->getType(), |
||
| 149 | 'tableGroup' => $tableGroups[$table], |
||
| 150 | ]; |
||
| 151 | $columnsList[] = $connection->quoteIdentifier($table).'.'.$connection->quoteIdentifier($columnName).' as '. |
||
| 152 | $connection->quoteIdentifier($table.'____'.$columnName); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | return [$columnDescList, $columnsList, $reconstructedOrderBy]; |
||
| 157 | } |
||
| 158 | |||
| 224 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: