Conditions | 39 |
Paths | 10634 |
Total Lines | 165 |
Code Lines | 92 |
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 |
||
83 | public function createObject($identifier, $data = null, $fixtures = null) |
||
84 | { |
||
85 | // We have to disable validation while we import the fixtures, as the order in |
||
86 | // which they are imported doesnt guarantee valid relations until after the import is complete. |
||
87 | // Also disable filesystem manipulations |
||
88 | Config::nest(); |
||
89 | Config::modify()->set(DataObject::class, 'validation_enabled', false); |
||
90 | Config::modify()->set(File::class, 'update_filesystem', false); |
||
91 | |||
92 | $this->invokeCallbacks('beforeCreate', array($identifier, &$data, &$fixtures)); |
||
93 | |||
94 | try { |
||
95 | $class = $this->class; |
||
96 | $schema = DataObject::getSchema(); |
||
97 | $obj = Injector::inst()->create($class); |
||
98 | |||
99 | // If an ID is explicitly passed, then we'll sort out the initial write straight away |
||
100 | // This is just in case field setters triggered by the population code in the next block |
||
101 | // Call $this->write(). (For example, in FileTest) |
||
102 | if (isset($data['ID'])) { |
||
103 | $obj->ID = $data['ID']; |
||
104 | |||
105 | // The database needs to allow inserting values into the foreign key column (ID in our case) |
||
106 | $conn = DB::get_conn(); |
||
107 | $baseTable = DataObject::getSchema()->baseDataTable($class); |
||
108 | if (method_exists($conn, 'allowPrimaryKeyEditing')) { |
||
109 | $conn->allowPrimaryKeyEditing($baseTable, true); |
||
110 | } |
||
111 | $obj->write(false, true); |
||
112 | if (method_exists($conn, 'allowPrimaryKeyEditing')) { |
||
113 | $conn->allowPrimaryKeyEditing($baseTable, false); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | // Populate defaults |
||
118 | if ($this->defaults) { |
||
|
|||
119 | foreach ($this->defaults as $fieldName => $fieldVal) { |
||
120 | if (isset($data[$fieldName]) && $data[$fieldName] !== false) { |
||
121 | continue; |
||
122 | } |
||
123 | |||
124 | if (!is_string($fieldVal) && is_callable($fieldVal)) { |
||
125 | $obj->$fieldName = $fieldVal($obj, $data, $fixtures); |
||
126 | } else { |
||
127 | $obj->$fieldName = $fieldVal; |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | // Populate overrides |
||
133 | if ($data) { |
||
134 | foreach ($data as $fieldName => $fieldVal) { |
||
135 | if ($schema->manyManyComponent($class, $fieldName) |
||
136 | || $schema->hasManyComponent($class, $fieldName) |
||
137 | || $schema->hasOneComponent($class, $fieldName) |
||
138 | ) { |
||
139 | continue; |
||
140 | } |
||
141 | |||
142 | $this->setValue($obj, $fieldName, $fieldVal, $fixtures); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | $obj->write(); |
||
147 | |||
148 | // Save to fixture before relationship processing in case of reflexive relationships |
||
149 | if (!isset($fixtures[$class])) { |
||
150 | $fixtures[$class] = array(); |
||
151 | } |
||
152 | $fixtures[$class][$identifier] = $obj->ID; |
||
153 | |||
154 | // Populate all relations |
||
155 | if ($data) { |
||
156 | foreach ($data as $fieldName => $fieldVal) { |
||
157 | $isManyMany = $schema->manyManyComponent($class, $fieldName); |
||
158 | $isHasMany = $schema->hasManyComponent($class, $fieldName); |
||
159 | if ($isManyMany && $isHasMany) { |
||
160 | throw new InvalidArgumentException("$fieldName is both many_many and has_many"); |
||
161 | } |
||
162 | if ($isManyMany || $isHasMany) { |
||
163 | $obj->write(); |
||
164 | |||
165 | // Many many components need a little extra work to extract extrafields |
||
166 | if (is_array($fieldVal) && $isManyMany) { |
||
167 | // handle lists of many_many relations. Each item can |
||
168 | // specify the many_many_extraFields against each |
||
169 | // related item. |
||
170 | foreach ($fieldVal as $relVal) { |
||
171 | // Check for many_many_extrafields |
||
172 | $extrafields = []; |
||
173 | if (is_array($relVal)) { |
||
174 | // Item is either first row, or key in yet another nested array |
||
175 | $item = key($relVal); |
||
176 | if (is_array($relVal[$item]) && count($relVal) === 1) { |
||
177 | // Extra fields from nested array |
||
178 | $extrafields = $relVal[$item]; |
||
179 | } else { |
||
180 | // Extra fields from subsequent items |
||
181 | array_shift($relVal); |
||
182 | $extrafields = $relVal; |
||
183 | } |
||
184 | } else { |
||
185 | $item = $relVal; |
||
186 | } |
||
187 | $id = $this->parseValue($item, $fixtures); |
||
188 | |||
189 | $obj->getManyManyComponents($fieldName)->add( |
||
190 | $id, |
||
191 | $extrafields |
||
192 | ); |
||
193 | } |
||
194 | } else { |
||
195 | $items = is_array($fieldVal) |
||
196 | ? $fieldVal |
||
197 | : preg_split('/ *, */', trim($fieldVal)); |
||
198 | |||
199 | $parsedItems = []; |
||
200 | foreach ($items as $item) { |
||
201 | // Check for correct format: =><relationname>.<identifier>. |
||
202 | // Ignore if the item has already been replaced with a numeric DB identifier |
||
203 | if (!is_numeric($item) && !preg_match('/^=>[^\.]+\.[^\.]+/', $item)) { |
||
204 | throw new InvalidArgumentException(sprintf( |
||
205 | 'Invalid format for relation "%s" on class "%s" ("%s")', |
||
206 | $fieldName, |
||
207 | $class, |
||
208 | $item |
||
209 | )); |
||
210 | } |
||
211 | |||
212 | $parsedItems[] = $this->parseValue($item, $fixtures); |
||
213 | } |
||
214 | |||
215 | if ($isHasMany) { |
||
216 | $obj->getComponents($fieldName)->setByIDList($parsedItems); |
||
217 | } elseif ($isManyMany) { |
||
218 | $obj->getManyManyComponents($fieldName)->setByIDList($parsedItems); |
||
219 | } |
||
220 | } |
||
221 | } else { |
||
222 | $hasOneField = preg_replace('/ID$/', '', $fieldName); |
||
223 | if ($className = $schema->hasOneComponent($class, $hasOneField)) { |
||
224 | $obj->{$hasOneField . 'ID'} = $this->parseValue($fieldVal, $fixtures, $fieldClass); |
||
225 | // Inject class for polymorphic relation |
||
226 | if ($className === 'SilverStripe\\ORM\\DataObject') { |
||
227 | $obj->{$hasOneField . 'Class'} = $fieldClass; |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | $obj->write(); |
||
234 | |||
235 | // If LastEdited was set in the fixture, set it here |
||
236 | if ($data && array_key_exists('LastEdited', $data)) { |
||
237 | $this->overrideField($obj, 'LastEdited', $data['LastEdited'], $fixtures); |
||
238 | } |
||
239 | } catch (Exception $e) { |
||
240 | Config::unnest(); |
||
241 | throw $e; |
||
242 | } |
||
243 | |||
244 | Config::unnest(); |
||
245 | $this->invokeCallbacks('afterCreate', array($obj, $identifier, &$data, &$fixtures)); |
||
246 | |||
247 | return $obj; |
||
248 | } |
||
367 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.