| Conditions | 11 |
| Paths | 3 |
| Total Lines | 63 |
| Code Lines | 31 |
| 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 |
||
| 129 | private function getIndex($indexedFieldName) |
||
| 130 | { |
||
| 131 | // build index |
||
| 132 | if ($this->index === null) { |
||
| 133 | // init empty index |
||
| 134 | $this->index = []; |
||
| 135 | |||
| 136 | // get index definition |
||
| 137 | $indexedFields = $this->getIndexDefinition(); |
||
| 138 | |||
| 139 | // build index for database |
||
| 140 | if (!empty($indexedFields)) { |
||
| 141 | // init all defined indexes |
||
| 142 | foreach ($this->clusterIndex as &$entry) { |
||
| 143 | foreach ($indexedFields as $indexName => $indexDefinition) { |
||
| 144 | if (is_array($indexDefinition)) { |
||
| 145 | $reference = &$this->index[$indexName]; |
||
| 146 | // compound index |
||
| 147 | foreach ($indexDefinition as $indexDefinitionPart) { |
||
| 148 | // limited length of field |
||
| 149 | if (is_array($indexDefinitionPart)) { |
||
| 150 | $indexDefinitionPartValue = substr( |
||
| 151 | $entry[$indexDefinitionPart[0]], |
||
| 152 | 0, |
||
| 153 | $indexDefinitionPart[1] |
||
| 154 | ); |
||
| 155 | } else { |
||
| 156 | $indexDefinitionPartValue = $entry[$indexDefinitionPart]; |
||
| 157 | } |
||
| 158 | if (!isset($reference[$indexDefinitionPartValue])) { |
||
| 159 | $reference[$indexDefinitionPartValue] = []; |
||
| 160 | } |
||
| 161 | $reference = &$reference[$indexDefinitionPartValue]; |
||
| 162 | } |
||
| 163 | |||
| 164 | $reference = $this->arrayToEntry($entry); |
||
| 165 | } else { |
||
| 166 | // single index |
||
| 167 | $indexName = $indexDefinition; |
||
| 168 | // skip empty field |
||
| 169 | if (empty($entry[$indexDefinition])) { |
||
| 170 | continue; |
||
| 171 | } |
||
| 172 | // add to index |
||
| 173 | $this->index[$indexName][$entry[$indexDefinition]] = $this->arrayToEntry($entry); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | // get index |
||
| 181 | if (!isset($this->index[$indexedFieldName])) { |
||
| 182 | throw new \InvalidArgumentException( |
||
| 183 | sprintf( |
||
| 184 | 'Unknown index "%s" in database "%s"', |
||
| 185 | $indexedFieldName, |
||
| 186 | get_class() |
||
| 187 | ) |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | return $this->index[$indexedFieldName]; |
||
| 192 | } |
||
| 263 |