| Conditions | 13 |
| Paths | 22 |
| Total Lines | 89 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 208 | public function getTable($tableSelector, $selector = 'StructureID', &$tableColumns = null, $externalHandler = null, $params = array()) |
||
| 209 | { |
||
| 210 | /** @var array $resultTable Collection of collections of field cells */ |
||
| 211 | $resultTable = array(); |
||
| 212 | /** @var array $dbTableFieldsIds Array of table structure column identifiers */ |
||
| 213 | $dbTableFieldsIds = array(); |
||
| 214 | |||
| 215 | // Get structure object if we need to search it by other fields |
||
| 216 | if ($selector != 'StructureID') { |
||
| 217 | $structure = dbQuery('structure')->cond($selector, $tableSelector)->first(); |
||
| 218 | $tableSelector = $structure->id; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** If this table has columns */ |
||
| 222 | if (dbQuery('structurefield') |
||
| 223 | ->cond("StructureID", $tableSelector) |
||
| 224 | ->fields('FieldID', $dbTableFieldsIds) |
||
| 225 | ) { |
||
| 226 | // Get localized and not localized fields |
||
| 227 | $localizedFields = array(); |
||
| 228 | $unlocalizedFields = array(); |
||
| 229 | /** @var \samson\cms\CMSField $dbTableField Table column */ |
||
| 230 | foreach (dbQuery('field')->order_by('priority')->cond('FieldID', $dbTableFieldsIds)->exec() as $field) { |
||
| 231 | /** Add table columns names */ |
||
| 232 | $tableColumns[] = $field->Name; |
||
| 233 | if ($field->local == 1) { |
||
| 234 | $localizedFields[] = $field->id; |
||
| 235 | } else { |
||
| 236 | $unlocalizedFields[] = $field->id; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | // Query to get table rows(table materials) |
||
| 241 | $tableQuery = dbQuery('material') |
||
| 242 | ->cond('parent_id', $this->MaterialID) |
||
| 243 | ->cond('Active', '1') |
||
| 244 | ->join('structurematerial') |
||
| 245 | ->cond('structurematerial_StructureID', $tableSelector) |
||
| 246 | ->order_by('priority'); |
||
| 247 | |||
| 248 | // Call user function if exists |
||
| 249 | if (is_callable($externalHandler)) { |
||
| 250 | // Give it query as parameter |
||
| 251 | call_user_func_array($externalHandler, array_merge(array(&$tableQuery), $params)); |
||
| 252 | } |
||
| 253 | |||
| 254 | // Get table row materials |
||
| 255 | $tableMaterialIds = array(); |
||
| 256 | if ($tableQuery->fields('MaterialID', $tableMaterialIds)) { |
||
| 257 | // Create field condition |
||
| 258 | $localizationFieldCond = new Condition('or'); |
||
| 259 | |||
| 260 | // Create localized condition |
||
| 261 | if (sizeof($localizedFields)) { |
||
| 262 | $localizedFieldCond = new Condition('and'); |
||
| 263 | $localizedFieldCond->add('materialfield_FieldID', $localizedFields) |
||
| 264 | ->add('materialfield_locale', locale()); |
||
| 265 | // Add this condition to condition group |
||
| 266 | $localizationFieldCond->add($localizedFieldCond); |
||
| 267 | } |
||
| 268 | |||
| 269 | // Create not localized condition |
||
| 270 | if (sizeof($unlocalizedFields)) { |
||
| 271 | $localizationFieldCond->add('materialfield_FieldID', $unlocalizedFields); |
||
| 272 | } |
||
| 273 | |||
| 274 | // Create db query |
||
| 275 | $materialFieldQuery = dbQuery('materialfield') |
||
| 276 | ->cond('MaterialID', $tableMaterialIds) |
||
| 277 | ->cond($localizationFieldCond); |
||
| 278 | |||
| 279 | // Flip field identifiers as keys |
||
| 280 | $tableColumnIds = array_flip($dbTableFieldsIds); |
||
| 281 | $resultTable = array_flip($tableMaterialIds); |
||
| 282 | |||
| 283 | /** @var \samson\activerecord\material $dbTableRow Material object (table row) */ |
||
| 284 | foreach ($materialFieldQuery->exec() as $mf) { |
||
| 285 | if (!is_array($resultTable[$mf['MaterialID']])) { |
||
| 286 | $resultTable[$mf['MaterialID']] = array(); |
||
| 287 | } |
||
| 288 | |||
| 289 | $resultTable[$mf['MaterialID']][$tableColumnIds[$mf->FieldID]] = |
||
| 290 | !empty($mf->Value) ? $mf->Value : (!empty($mf->numeric_value) ? $mf->numeric_value : $mf->key_value); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | return array_values($resultTable); |
||
| 296 | } |
||
| 297 | } |
||
| 298 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.