Conditions | 32 |
Paths | 10226 |
Total Lines | 141 |
Code Lines | 75 |
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 |
||
70 | public function createObject($identifier, $data = null, $fixtures = null) { |
||
71 | // We have to disable validation while we import the fixtures, as the order in |
||
72 | // which they are imported doesnt guarantee valid relations until after the import is complete. |
||
73 | $validationenabled = Config::inst()->get('DataObject', 'validation_enabled'); |
||
74 | Config::inst()->update('DataObject', 'validation_enabled', false); |
||
75 | |||
76 | $this->invokeCallbacks('beforeCreate', array($identifier, &$data, &$fixtures)); |
||
77 | |||
78 | try { |
||
79 | $class = $this->class; |
||
80 | $obj = DataModel::inst()->$class->newObject(); |
||
81 | |||
82 | // If an ID is explicitly passed, then we'll sort out the initial write straight away |
||
83 | // This is just in case field setters triggered by the population code in the next block |
||
84 | // Call $this->write(). (For example, in FileTest) |
||
85 | if(isset($data['ID'])) { |
||
86 | $obj->ID = $data['ID']; |
||
87 | |||
88 | // The database needs to allow inserting values into the foreign key column (ID in our case) |
||
89 | $conn = DB::get_conn(); |
||
90 | if(method_exists($conn, 'allowPrimaryKeyEditing')) { |
||
91 | $conn->allowPrimaryKeyEditing(ClassInfo::baseDataClass($class), true); |
||
92 | } |
||
93 | $obj->write(false, true); |
||
94 | if(method_exists($conn, 'allowPrimaryKeyEditing')) { |
||
95 | $conn->allowPrimaryKeyEditing(ClassInfo::baseDataClass($class), false); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | // Populate defaults |
||
100 | if($this->defaults) foreach($this->defaults as $fieldName => $fieldVal) { |
||
101 | if(isset($data[$fieldName]) && $data[$fieldName] !== false) continue; |
||
102 | |||
103 | if(is_callable($fieldVal)) { |
||
104 | $obj->$fieldName = $fieldVal($obj, $data, $fixtures); |
||
105 | } else { |
||
106 | $obj->$fieldName = $fieldVal; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // Populate overrides |
||
111 | if($data) foreach($data as $fieldName => $fieldVal) { |
||
112 | // Defer relationship processing |
||
113 | if( |
||
114 | $obj->manyManyComponent($fieldName) |
||
115 | || $obj->hasManyComponent($fieldName) |
||
116 | || $obj->hasOneComponent($fieldName) |
||
117 | ) { |
||
118 | continue; |
||
119 | } |
||
120 | |||
121 | $this->setValue($obj, $fieldName, $fieldVal, $fixtures); |
||
122 | } |
||
123 | |||
124 | $obj->write(); |
||
125 | |||
126 | // Save to fixture before relationship processing in case of reflexive relationships |
||
127 | if(!isset($fixtures[$class])) { |
||
128 | $fixtures[$class] = array(); |
||
129 | } |
||
130 | $fixtures[$class][$identifier] = $obj->ID; |
||
131 | |||
132 | // Populate all relations |
||
133 | if($data) foreach($data as $fieldName => $fieldVal) { |
||
134 | if($obj->manyManyComponent($fieldName) || $obj->hasManyComponent($fieldName)) { |
||
135 | $obj->write(); |
||
136 | |||
137 | $parsedItems = array(); |
||
138 | |||
139 | if(is_array($fieldVal)) { |
||
140 | // handle lists of many_many relations. Each item can |
||
141 | // specify the many_many_extraFields against each |
||
142 | // related item. |
||
143 | foreach($fieldVal as $relVal) { |
||
144 | $item = key($relVal); |
||
145 | $id = $this->parseValue($item, $fixtures); |
||
146 | $parsedItems[] = $id; |
||
147 | |||
148 | array_shift($relVal); |
||
149 | |||
150 | $obj->getManyManyComponents($fieldName)->add( |
||
151 | $id, $relVal |
||
152 | ); |
||
153 | } |
||
154 | } else { |
||
155 | $items = preg_split('/ *, */',trim($fieldVal)); |
||
156 | |||
157 | foreach($items as $item) { |
||
158 | // Check for correct format: =><relationname>.<identifier>. |
||
159 | // Ignore if the item has already been replaced with a numeric DB identifier |
||
160 | if(!is_numeric($item) && !preg_match('/^=>[^\.]+\.[^\.]+/', $item)) { |
||
161 | throw new InvalidArgumentException(sprintf( |
||
162 | 'Invalid format for relation "%s" on class "%s" ("%s")', |
||
163 | $fieldName, |
||
164 | $class, |
||
165 | $item |
||
166 | )); |
||
167 | } |
||
168 | |||
169 | $parsedItems[] = $this->parseValue($item, $fixtures); |
||
170 | } |
||
171 | |||
172 | if($obj->hasManyComponent($fieldName)) { |
||
173 | $obj->getComponents($fieldName)->setByIDList($parsedItems); |
||
174 | } elseif($obj->manyManyComponent($fieldName)) { |
||
175 | $obj->getManyManyComponents($fieldName)->setByIDList($parsedItems); |
||
176 | } |
||
177 | } |
||
178 | } else { |
||
179 | $hasOneField = preg_replace('/ID$/', '', $fieldName); |
||
180 | if($className = $obj->hasOneComponent($hasOneField)) { |
||
181 | $obj->{$hasOneField.'ID'} = $this->parseValue($fieldVal, $fixtures, $fieldClass); |
||
182 | // Inject class for polymorphic relation |
||
183 | if($className === 'DataObject') { |
||
184 | $obj->{$hasOneField.'Class'} = $fieldClass; |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | $obj->write(); |
||
190 | |||
191 | // If LastEdited was set in the fixture, set it here |
||
192 | if($data && array_key_exists('LastEdited', $data)) { |
||
193 | $this->overrideField($obj, 'LastEdited', $data['LastEdited'], $fixtures); |
||
194 | } |
||
195 | |||
196 | // Ensure Folder objects exist physically, as otherwise future File fixtures can't detect them |
||
197 | if($obj instanceof Folder) { |
||
198 | Filesystem::makeFolder($obj->getFullPath()); |
||
199 | } |
||
200 | } catch(Exception $e) { |
||
201 | Config::inst()->update('DataObject', 'validation_enabled', $validationenabled); |
||
202 | throw $e; |
||
203 | } |
||
204 | |||
205 | Config::inst()->update('DataObject', 'validation_enabled', $validationenabled); |
||
206 | |||
207 | $this->invokeCallbacks('afterCreate', array($obj, $identifier, &$data, &$fixtures)); |
||
208 | |||
209 | return $obj; |
||
210 | } |
||
211 | |||
313 |
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: