| Conditions | 12 |
| Paths | 40 |
| Total Lines | 116 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 57 | public function fillMetadata($filter = null) |
||
| 58 | { |
||
| 59 | // Iterate all metadata types |
||
| 60 | foreach (Metadata::$types as $type) { |
||
| 61 | |||
| 62 | // Iterate all structures, parents first |
||
| 63 | foreach ($this->entityNavigations($type) as $structureRow) { |
||
| 64 | |||
| 65 | // If filter is the function and filter return false then skip this structure |
||
| 66 | if (is_callable($filter) && (false === $filter($structureRow))) { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | |||
| 70 | // Fill in entity metadata |
||
| 71 | $metadata = new Metadata($type); |
||
| 72 | |||
| 73 | // Get CapsCase and transliterated entity name |
||
| 74 | $metadata->entity = $this->entityName($structureRow['Name']); |
||
| 75 | // Try to find entity parent identifier for building future relations |
||
| 76 | $metadata->parentID = $this->entityParent($structureRow['StructureID']); |
||
| 77 | // Generate application from current entity |
||
| 78 | $metadata->generateApplication = $structureRow['applicationGenerate']; |
||
| 79 | // Show application from current entity |
||
| 80 | $metadata->showApplication = $structureRow['applicationOutput']; |
||
| 81 | // Icon for application from current entity |
||
| 82 | $metadata->iconApplication = $structureRow['applicationIcon']; |
||
| 83 | // Render application on main page |
||
| 84 | $metadata->renderMainApplication = $structureRow['applicationRenderMain']; |
||
| 85 | |||
| 86 | // TODO: Add multiple parent and fetching their data in a loop |
||
| 87 | |||
| 88 | // Set pointer to parent entity |
||
| 89 | if (null !== $metadata->parentID) { |
||
| 90 | if (array_key_exists($metadata->parentID, $this->metadata)) { |
||
| 91 | $metadata->parent = $this->metadata[$metadata->parentID]; |
||
| 92 | // Add all parent metadata to current object |
||
| 93 | $metadata->realNames = $metadata->parent->realNames; |
||
| 94 | $metadata->allFieldIDs = $metadata->parent->allFieldIDs; |
||
| 95 | $metadata->allFieldNames = $metadata->parent->allFieldNames; |
||
| 96 | $metadata->allFieldValueColumns = $metadata->parent->allFieldValueColumns; |
||
| 97 | $metadata->allFieldTypes = $metadata->parent->allFieldTypes; |
||
| 98 | $metadata->fieldDescriptions = $metadata->parent->fieldDescriptions; |
||
| 99 | $metadata->localizedFieldIDs = $metadata->parent->localizedFieldIDs; |
||
| 100 | $metadata->notLocalizedFieldIDs = $metadata->parent->notLocalizedFieldIDs; |
||
| 101 | } else { |
||
| 102 | throw new ParentEntityNotFound($metadata->parentID); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | // Store entity original data |
||
| 107 | $metadata->entityRealName = $structureRow['Name']; |
||
| 108 | $metadata->entityID = $structureRow['StructureID']; |
||
| 109 | |||
| 110 | // Get old AR collections of metadata |
||
| 111 | $metadata->arSelect = \samson\activerecord\material::$_sql_select; |
||
| 112 | $metadata->arAttributes = \samson\activerecord\material::$_attributes; |
||
| 113 | $metadata->arMap = \samson\activerecord\material::$_map; |
||
| 114 | $metadata->arFrom = \samson\activerecord\material::$_sql_from; |
||
| 115 | $metadata->arGroup = \samson\activerecord\material::$_own_group; |
||
| 116 | $metadata->arRelationAlias = \samson\activerecord\material::$_relation_alias; |
||
| 117 | $metadata->arRelationType = \samson\activerecord\material::$_relation_type; |
||
| 118 | $metadata->arRelations = \samson\activerecord\material::$_relations; |
||
| 119 | |||
| 120 | // Add SamsonCMS material needed data |
||
| 121 | $metadata->arSelect['this'] = ' STRAIGHT_JOIN ' . $metadata->arSelect['this']; |
||
| 122 | $metadata->arFrom['this'] .= "\n" . |
||
| 123 | 'LEFT JOIN ' . dbMySQLConnector::$prefix . 'materialfield as _mf |
||
| 124 | ON ' . dbMySQLConnector::$prefix . 'material.MaterialID = _mf.MaterialID'; |
||
| 125 | $metadata->arGroup[] = dbMySQLConnector::$prefix . 'material.MaterialID'; |
||
| 126 | |||
| 127 | // Iterate entity fields |
||
| 128 | foreach ($this->navigationFields($structureRow['StructureID']) as $fieldID => $fieldRow) { |
||
| 129 | // Get camelCase and transliterated field name |
||
| 130 | $fieldName = $this->fieldName($fieldRow['Name']); |
||
| 131 | |||
| 132 | // Store field metadata |
||
| 133 | $metadata->realNames[$fieldRow['Name']] = $fieldName; |
||
| 134 | $metadata->allFieldIDs[$fieldID] = $fieldName; |
||
| 135 | $metadata->allFieldNames[$fieldName] = $fieldID; |
||
| 136 | $metadata->allFieldValueColumns[$fieldID] = Field::valueColumn($fieldRow[Field::F_TYPE]); |
||
| 137 | $metadata->allFieldTypes[$fieldID] = Field::phpType($fieldRow['Type']); |
||
| 138 | $metadata->allFieldCmsTypes[$fieldID] = $fieldRow['Type']; |
||
| 139 | $metadata->fieldDescriptions[$fieldID] = $fieldRow['Description'] . ', ' . $fieldRow['Name'] . '#' . $fieldID; |
||
| 140 | $metadata->fieldRawDescriptions[$fieldID] = $fieldRow['Description']; |
||
| 141 | |||
| 142 | // Fill localization fields collections |
||
| 143 | if ($fieldRow[Field::F_LOCALIZED] === 1) { |
||
| 144 | $metadata->localizedFieldIDs[$fieldID] = $fieldName; |
||
| 145 | } else { |
||
| 146 | $metadata->notLocalizedFieldIDs[$fieldID] = $fieldName; |
||
| 147 | } |
||
| 148 | |||
| 149 | // Fill all fields which should display in list |
||
| 150 | if ($fieldRow['showInList'] == 1) { |
||
| 151 | $metadata->showFieldsInList[] = $fieldID; |
||
| 152 | } |
||
| 153 | |||
| 154 | // Set old AR collections of metadata |
||
| 155 | $metadata->arAttributes[$fieldName] = $fieldName; |
||
| 156 | $metadata->arMap[$fieldName] = dbMySQLConnector::$prefix . 'material.' . $fieldName; |
||
| 157 | |||
| 158 | // Add additional field column to entity query |
||
| 159 | $equal = '((_mf.FieldID = ' . $fieldID . ')&&(_mf.locale ' . ($fieldRow['local'] ? ' = "@locale"' : 'IS NULL') . '))'; |
||
| 160 | $metadata->arSelect['this'] .= "\n\t\t" . ',MAX(IF(' . $equal . ', _mf.`' . Field::valueColumn($fieldRow['Type']) . '`, NULL)) as `' . $fieldName . '`'; |
||
| 161 | } |
||
| 162 | |||
| 163 | // Get id of child navigation |
||
| 164 | foreach ($this->entityChildNavigation($structureRow['StructureID']) as $childNavigation) { |
||
| 165 | $metadata->childNavigationIDs[] = $childNavigation['StructureID']; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Store metadata by entity identifier |
||
| 169 | $this->metadata[$structureRow['StructureID']] = $metadata; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 377 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.