Conditions | 14 |
Paths | 77 |
Total Lines | 127 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 5 |
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 |
||
46 | public function fillMetadata($filter = null) |
||
47 | { |
||
48 | // Iterate all metadata types |
||
49 | foreach (Metadata::$types as $type) { |
||
|
|||
50 | |||
51 | // Iterate all structures, parents first |
||
52 | foreach ($this->entityNavigations($type) as $structureRow) { |
||
53 | |||
54 | // If filter is the function and filter return false then skip this structure |
||
55 | if (is_callable($filter) && (false === $filter($structureRow))) { |
||
56 | continue; |
||
57 | } |
||
58 | |||
59 | // Fill in entity metadata |
||
60 | $metadata = new Metadata($type); |
||
61 | |||
62 | // Get CapsCase and transliterated entity name |
||
63 | $metadata->entity = $this->entityName($structureRow['Name']); |
||
64 | // Store entity original data |
||
65 | $metadata->entityRealName = $structureRow['Name']; |
||
66 | $metadata->entityID = $structureRow['StructureID']; |
||
67 | |||
68 | // Append entity name suffix dependening on its type |
||
69 | if ($type === Metadata::TYPE_TABLE) { |
||
70 | $metadata->entity .= 'Table'; |
||
71 | } |
||
72 | |||
73 | // Try to find entity parent identifier for building future relations |
||
74 | $metadata->parentID = $this->entityParent($structureRow['StructureID']); |
||
75 | // Generate application from current entity |
||
76 | $metadata->generateApplication = $structureRow['applicationGenerate']; |
||
77 | // Show application from current entity |
||
78 | $metadata->showApplication = $structureRow['applicationOutput']; |
||
79 | // Icon for application from current entity |
||
80 | $metadata->iconApplication = $structureRow['applicationIcon']; |
||
81 | // Render application on main page |
||
82 | $metadata->renderMainApplication = $structureRow['applicationRenderMain']; |
||
83 | |||
84 | // TODO: Add multiple parent and fetching their data in a loop |
||
85 | |||
86 | // Set pointer to parent entity |
||
87 | if (null !== $metadata->parentID && $metadata->type !== Metadata::TYPE_TABLE) { |
||
88 | if (array_key_exists($metadata->parentID, $this->metadata)) { |
||
89 | $metadata->parent = $this->metadata[$metadata->parentID]; |
||
90 | // Add all parent metadata to current object |
||
91 | $metadata->defaultValues = $metadata->parent->defaultValues; |
||
92 | $metadata->realNames = $metadata->parent->realNames; |
||
93 | $metadata->allFieldIDs = $metadata->parent->allFieldIDs; |
||
94 | $metadata->allFieldNames = $metadata->parent->allFieldNames; |
||
95 | $metadata->allFieldValueColumns = $metadata->parent->allFieldValueColumns; |
||
96 | $metadata->allFieldTypes = $metadata->parent->allFieldTypes; |
||
97 | $metadata->fieldDescriptions = $metadata->parent->fieldDescriptions; |
||
98 | $metadata->localizedFieldIDs = $metadata->parent->localizedFieldIDs; |
||
99 | $metadata->notLocalizedFieldIDs = $metadata->parent->notLocalizedFieldIDs; |
||
100 | } else { |
||
101 | throw new ParentEntityNotFound($metadata->parentID); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | // Get old AR collections of metadata |
||
106 | $metadata->arSelect = \samson\activerecord\material::$_sql_select; |
||
107 | $metadata->arAttributes = \samson\activerecord\material::$_attributes; |
||
108 | $metadata->arMap = \samson\activerecord\material::$_map; |
||
109 | $metadata->arFrom = \samson\activerecord\material::$_sql_from; |
||
110 | $metadata->arGroup = \samson\activerecord\material::$_own_group; |
||
111 | $metadata->arRelationAlias = \samson\activerecord\material::$_relation_alias; |
||
112 | $metadata->arRelationType = \samson\activerecord\material::$_relation_type; |
||
113 | $metadata->arRelations = \samson\activerecord\material::$_relations; |
||
114 | |||
115 | // Add SamsonCMS material needed data |
||
116 | $metadata->arSelect['this'] = ' STRAIGHT_JOIN ' . $metadata->arSelect['this']; |
||
117 | $metadata->arFrom['this'] .= "\n" . |
||
118 | 'LEFT JOIN ' . dbMySQLConnector::$prefix . 'materialfield as _mf |
||
119 | ON ' . dbMySQLConnector::$prefix . 'material.MaterialID = _mf.MaterialID'; |
||
120 | $metadata->arGroup[] = dbMySQLConnector::$prefix . 'material.MaterialID'; |
||
121 | |||
122 | // Iterate entity fields |
||
123 | foreach ($this->navigationFields($structureRow['StructureID']) as $fieldID => $fieldRow) { |
||
124 | // Get camelCase and transliterated field name |
||
125 | $fieldName = $this->fieldName($fieldRow['Name']); |
||
126 | |||
127 | // TODO: Set default for additional field storing type accordingly. |
||
128 | |||
129 | // Store field metadata |
||
130 | $metadata->realNames[$fieldRow['Name']] = $fieldName; |
||
131 | $metadata->allFieldIDs[$fieldID] = $fieldName; |
||
132 | $metadata->allFieldNames[$fieldName] = $fieldID; |
||
133 | $metadata->allFieldValueColumns[$fieldID] = Field::valueColumn($fieldRow[Field::F_TYPE]); |
||
134 | $metadata->allFieldTypes[$fieldID] = Field::phpType($fieldRow['Type']); |
||
135 | $metadata->allFieldCmsTypes[$fieldID] = (int)$fieldRow['Type']; |
||
136 | $metadata->fieldDescriptions[$fieldID] = $fieldRow['Description'] . ', ' . $fieldRow['Name'] . '#' . $fieldID; |
||
137 | $metadata->fieldRawDescriptions[$fieldID] = $fieldRow['Description']; |
||
138 | |||
139 | // Fill localization fields collections |
||
140 | if ($fieldRow[Field::F_LOCALIZED] == 1) { |
||
141 | $metadata->localizedFieldIDs[$fieldID] = $fieldName; |
||
142 | } else { |
||
143 | $metadata->notLocalizedFieldIDs[$fieldID] = $fieldName; |
||
144 | } |
||
145 | |||
146 | // Fill all fields which should display in list |
||
147 | if ($fieldRow['showInList'] == 1) { |
||
148 | $metadata->showFieldsInList[] = $fieldID; |
||
149 | } |
||
150 | |||
151 | // Save custom type |
||
152 | $metadata->customTypeFields[$fieldID] = $fieldRow['customTypeName']; |
||
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 | |||
392 |