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