| Conditions | 13 |
| Paths | 86 |
| Total Lines | 67 |
| Code Lines | 37 |
| 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 |
||
| 153 | public function add($item, $extraFields = []) |
||
| 154 | { |
||
| 155 | // Ensure nulls or empty strings are correctly treated as empty arrays |
||
| 156 | if (empty($extraFields)) { |
||
| 157 | $extraFields = array(); |
||
| 158 | } |
||
| 159 | |||
| 160 | // Determine ID of new record |
||
| 161 | $itemID = null; |
||
| 162 | if (is_numeric($item)) { |
||
| 163 | $itemID = $item; |
||
| 164 | } elseif ($item instanceof $this->dataClass) { |
||
| 165 | /** @var DataObject $item */ |
||
| 166 | if (!$item->isInDB()) { |
||
| 167 | $item->write(); |
||
| 168 | } |
||
| 169 | $itemID = $item->ID; |
||
| 170 | } else { |
||
| 171 | throw new InvalidArgumentException( |
||
| 172 | "ManyManyThroughList::add() expecting a $this->dataClass object, or ID value" |
||
| 173 | ); |
||
| 174 | } |
||
| 175 | if (empty($itemID)) { |
||
| 176 | throw new InvalidArgumentException("ManyManyThroughList::add() could not add record without ID"); |
||
| 177 | } |
||
| 178 | |||
| 179 | // Validate foreignID |
||
| 180 | $foreignIDs = $this->getForeignID(); |
||
| 181 | if (empty($foreignIDs)) { |
||
| 182 | throw new BadMethodCallException("ManyManyList::add() can't be called until a foreign ID is set"); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Apply this item to each given foreign ID record |
||
| 186 | if (!is_array($foreignIDs)) { |
||
| 187 | $foreignIDs = [$foreignIDs]; |
||
| 188 | } |
||
| 189 | $foreignIDsToAdd = array_combine($foreignIDs, $foreignIDs); |
||
| 190 | |||
| 191 | // Update existing records |
||
| 192 | $localKey = $this->manipulator->getLocalKey(); |
||
| 193 | // Foreign key (or key for ID field if polymorphic) |
||
| 194 | $foreignKey = $this->manipulator->getForeignIDKey(); |
||
| 195 | $hasManyList = $this->manipulator->getParentRelationship($this->dataQuery()); |
||
| 196 | $records = $hasManyList->filter($localKey, $itemID); |
||
| 197 | /** @var DataObject $record */ |
||
| 198 | foreach ($records as $record) { |
||
| 199 | if ($extraFields) { |
||
| 200 | foreach ($extraFields as $field => $value) { |
||
| 201 | $record->$field = $value; |
||
| 202 | } |
||
| 203 | $record->write(); |
||
| 204 | } |
||
| 205 | // |
||
| 206 | $foreignID = $record->$foreignKey; |
||
| 207 | unset($foreignIDsToAdd[$foreignID]); |
||
| 208 | } |
||
| 209 | |||
| 210 | // Check if any records remain to add |
||
| 211 | if (empty($foreignIDsToAdd)) { |
||
| 212 | return; |
||
| 213 | } |
||
| 214 | |||
| 215 | // Add item to relation |
||
| 216 | $hasManyList = $hasManyList->forForeignID($foreignIDsToAdd); |
||
| 217 | $record = $hasManyList->createDataObject($extraFields ?: []); |
||
| 218 | $record->$localKey = $itemID; |
||
| 219 | $hasManyList->add($record); |
||
| 220 | } |
||
| 243 |