| Total Complexity | 63 |
| Total Lines | 504 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ManyManyList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ManyManyList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ManyManyList extends RelationList |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string $joinTable |
||
| 21 | */ |
||
| 22 | protected $joinTable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string $localKey |
||
| 26 | */ |
||
| 27 | protected $localKey; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string $foreignKey |
||
| 31 | */ |
||
| 32 | protected $foreignKey; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array $extraFields |
||
| 36 | */ |
||
| 37 | protected $extraFields; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array $_compositeExtraFields |
||
| 41 | */ |
||
| 42 | protected $_compositeExtraFields = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Create a new ManyManyList object. |
||
| 46 | * |
||
| 47 | * A ManyManyList object represents a list of {@link DataObject} records |
||
| 48 | * that correspond to a many-many relationship. |
||
| 49 | * |
||
| 50 | * Generation of the appropriate record set is left up to the caller, using |
||
| 51 | * the normal {@link DataList} methods. Addition arguments are used to |
||
| 52 | * support {@@link add()} and {@link remove()} methods. |
||
| 53 | * |
||
| 54 | * @param string $dataClass The class of the DataObjects that this will list. |
||
| 55 | * @param string $joinTable The name of the table whose entries define the content of this many_many relation. |
||
| 56 | * @param string $localKey The key in the join table that maps to the dataClass' PK. |
||
| 57 | * @param string $foreignKey The key in the join table that maps to joined class' PK. |
||
| 58 | * @param array $extraFields A map of field => fieldtype of extra fields on the join table. |
||
| 59 | * |
||
| 60 | * @example new ManyManyList('Group','Group_Members', 'GroupID', 'MemberID'); |
||
| 61 | */ |
||
| 62 | public function __construct($dataClass, $joinTable, $localKey, $foreignKey, $extraFields = []) |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Setup the join between this dataobject and the necessary mapping table |
||
| 76 | */ |
||
| 77 | protected function linkJoinTable() |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Adds the many_many_extraFields to the select of the underlying |
||
| 94 | * {@link DataQuery}. |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | protected function appendExtraFieldsToQuery() |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Create a DataObject from the given SQL row. |
||
| 126 | * |
||
| 127 | * @param array $row |
||
| 128 | * @return DataObject |
||
| 129 | */ |
||
| 130 | public function createDataObject($row) |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Return a filter expression for when getting the contents of the |
||
| 166 | * relationship for some foreign ID |
||
| 167 | * |
||
| 168 | * @param int|null|string|array $id |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | protected function foreignIDFilter($id = null) |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return a filter expression for the join table when writing to the join table |
||
| 191 | * |
||
| 192 | * When writing (add, remove, removeByID), we need to filter the join table to just the relevant |
||
| 193 | * entries. However some subclasses of ManyManyList (Member_GroupSet) modify foreignIDFilter to |
||
| 194 | * include additional calculated entries, so we need different filters when reading and when writing |
||
| 195 | * |
||
| 196 | * @param array|int|null $id (optional) An ID or an array of IDs - if not provided, will use the current ids |
||
| 197 | * as per getForeignID |
||
| 198 | * @return array Condition In array(SQL => parameters format) |
||
| 199 | */ |
||
| 200 | protected function foreignIDWriteFilter($id = null) |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Add an item to this many_many relationship |
||
| 207 | * Does so by adding an entry to the joinTable. |
||
| 208 | * |
||
| 209 | * Can also be used to update an already existing joinTable entry: |
||
| 210 | * |
||
| 211 | * $manyManyList->add($recordID,["ExtraField" => "value"]); |
||
| 212 | * |
||
| 213 | * @throws InvalidArgumentException |
||
| 214 | * @throws Exception |
||
| 215 | * |
||
| 216 | * @param DataObject|int $item |
||
| 217 | * @param array $extraFields A map of additional columns to insert into the joinTable. |
||
| 218 | * Column names should be ANSI quoted. |
||
| 219 | * @throws Exception |
||
| 220 | */ |
||
| 221 | public function add($item, $extraFields = []) |
||
| 222 | { |
||
| 223 | // Ensure nulls or empty strings are correctly treated as empty arrays |
||
| 224 | if (empty($extraFields)) { |
||
| 225 | $extraFields = []; |
||
| 226 | } |
||
| 227 | |||
| 228 | // Determine ID of new record |
||
| 229 | $itemID = null; |
||
| 230 | if (is_numeric($item)) { |
||
| 231 | $itemID = $item; |
||
| 232 | } elseif ($item instanceof $this->dataClass) { |
||
| 233 | // Ensure record is saved |
||
| 234 | if (!$item->isInDB()) { |
||
| 235 | $item->write(); |
||
| 236 | } |
||
| 237 | $itemID = $item->ID; |
||
| 238 | } else { |
||
| 239 | throw new InvalidArgumentException( |
||
| 240 | "ManyManyList::add() expecting a $this->dataClass object, or ID value" |
||
| 241 | ); |
||
| 242 | } |
||
| 243 | if (empty($itemID)) { |
||
| 244 | throw new InvalidArgumentException("ManyManyList::add() couldn't add this record"); |
||
| 245 | } |
||
| 246 | |||
| 247 | // Validate foreignID |
||
| 248 | $foreignIDs = $this->getForeignID(); |
||
| 249 | if (empty($foreignIDs)) { |
||
| 250 | throw new BadMethodCallException("ManyManyList::add() can't be called until a foreign ID is set"); |
||
| 251 | } |
||
| 252 | |||
| 253 | // Apply this item to each given foreign ID record |
||
| 254 | if (!is_array($foreignIDs)) { |
||
| 255 | $foreignIDs = [$foreignIDs]; |
||
| 256 | } |
||
| 257 | foreach ($foreignIDs as $foreignID) { |
||
| 258 | // Check for existing records for this item |
||
| 259 | if ($foreignFilter = $this->foreignIDWriteFilter($foreignID)) { |
||
| 260 | // With the current query, simply add the foreign and local conditions |
||
| 261 | // The query can be a bit odd, especially if custom relation classes |
||
| 262 | // don't join expected tables (@see Member_GroupSet for example). |
||
| 263 | $query = SQLSelect::create("*", "\"{$this->joinTable}\""); |
||
| 264 | $query->addWhere($foreignFilter); |
||
| 265 | $query->addWhere([ |
||
| 266 | "\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID |
||
| 267 | ]); |
||
| 268 | $hasExisting = ($query->count() > 0); |
||
| 269 | } else { |
||
| 270 | $hasExisting = false; |
||
| 271 | } |
||
| 272 | |||
| 273 | // Blank manipulation |
||
| 274 | $manipulation = [ |
||
| 275 | $this->joinTable => [ |
||
| 276 | 'command' => $hasExisting ? 'update' : 'insert', |
||
| 277 | 'fields' => [], |
||
| 278 | ], |
||
| 279 | ]; |
||
| 280 | if ($hasExisting) { |
||
| 281 | $manipulation[$this->joinTable]['where'] = [ |
||
| 282 | "\"{$this->joinTable}\".\"{$this->foreignKey}\"" => $foreignID, |
||
| 283 | "\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID |
||
| 284 | ]; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** @var DBField[] $fieldObjects */ |
||
| 288 | $fieldObjects = []; |
||
| 289 | if ($extraFields && $this->extraFields) { |
||
| 290 | // Write extra field to manipluation in the same way |
||
| 291 | // that DataObject::prepareManipulationTable writes fields |
||
| 292 | foreach ($this->extraFields as $fieldName => $fieldSpec) { |
||
| 293 | // Skip fields without an assignment |
||
| 294 | if (array_key_exists($fieldName, $extraFields)) { |
||
| 295 | $fieldObject = Injector::inst()->create($fieldSpec, $fieldName); |
||
| 296 | $fieldObject->setValue($extraFields[$fieldName]); |
||
| 297 | $fieldObject->writeToManipulation($manipulation[$this->joinTable]); |
||
| 298 | $fieldObjects[$fieldName] = $fieldObject; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | $manipulation[$this->joinTable]['fields'][$this->localKey] = $itemID; |
||
| 304 | $manipulation[$this->joinTable]['fields'][$this->foreignKey] = $foreignID; |
||
| 305 | |||
| 306 | // Make sure none of our field assignments are arrays |
||
| 307 | foreach ($manipulation as $tableManipulation) { |
||
| 308 | if (!isset($tableManipulation['fields'])) { |
||
| 309 | continue; |
||
| 310 | } |
||
| 311 | foreach ($tableManipulation['fields'] as $fieldName => $fieldValue) { |
||
| 312 | if (is_array($fieldValue)) { |
||
| 313 | // If the field allows non-scalar values we'll let it do dynamic assignments |
||
| 314 | if (isset($fieldObjects[$fieldName]) && $fieldObjects[$fieldName]->scalarValueOnly()) { |
||
| 315 | throw new InvalidArgumentException( |
||
| 316 | 'ManyManyList::add: parameterised field assignments are disallowed' |
||
| 317 | ); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | DB::manipulate($manipulation); |
||
| 324 | } |
||
| 325 | |||
| 326 | if ($this->addCallbacks) { |
||
| 327 | $this->addCallbacks->call($this, $item, $extraFields); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Remove the given item from this list. |
||
| 333 | * |
||
| 334 | * Note that for a ManyManyList, the item is never actually deleted, only |
||
| 335 | * the join table is affected. |
||
| 336 | * |
||
| 337 | * @param DataObject $item |
||
| 338 | */ |
||
| 339 | public function remove($item) |
||
| 340 | { |
||
| 341 | if (!($item instanceof $this->dataClass)) { |
||
| 342 | throw new InvalidArgumentException("ManyManyList::remove() expecting a $this->dataClass object"); |
||
| 343 | } |
||
| 344 | |||
| 345 | $result = $this->removeByID($item->ID); |
||
| 346 | |||
| 347 | return $result; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Remove the given item from this list. |
||
| 352 | * |
||
| 353 | * Note that for a ManyManyList, the item is never actually deleted, only |
||
| 354 | * the join table is affected |
||
| 355 | * |
||
| 356 | * @param int $itemID The item ID |
||
| 357 | */ |
||
| 358 | public function removeByID($itemID) |
||
| 359 | { |
||
| 360 | if (!is_numeric($itemID)) { |
||
| 361 | throw new InvalidArgumentException("ManyManyList::removeById() expecting an ID"); |
||
| 362 | } |
||
| 363 | |||
| 364 | $query = SQLDelete::create("\"{$this->joinTable}\""); |
||
| 365 | |||
| 366 | if ($filter = $this->foreignIDWriteFilter($this->getForeignID())) { |
||
| 367 | $query->setWhere($filter); |
||
| 368 | } else { |
||
| 369 | user_error("Can't call ManyManyList::remove() until a foreign ID is set"); |
||
| 370 | } |
||
| 371 | |||
| 372 | $query->addWhere([ |
||
| 373 | "\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID |
||
| 374 | ]); |
||
| 375 | |||
| 376 | // Perform the deletion |
||
| 377 | $query->execute(); |
||
| 378 | |||
| 379 | if ($this->removeCallbacks) { |
||
| 380 | $this->removeCallbacks->call($this, [$itemID]); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Remove all items from this many-many join. To remove a subset of items, |
||
| 386 | * filter it first. |
||
| 387 | * |
||
| 388 | * @return void |
||
| 389 | */ |
||
| 390 | public function removeAll() |
||
| 391 | { |
||
| 392 | |||
| 393 | // Remove the join to the join table to avoid MySQL row locking issues. |
||
| 394 | $query = $this->dataQuery(); |
||
| 395 | $foreignFilter = $query->getQueryParam('Foreign.Filter'); |
||
| 396 | $query->removeFilterOn($foreignFilter); |
||
| 397 | |||
| 398 | // Select ID column |
||
| 399 | $selectQuery = $query->query(); |
||
| 400 | $dataClassIDColumn = DataObject::getSchema()->sqlColumnForField($this->dataClass(), 'ID'); |
||
| 401 | $selectQuery->setSelect($dataClassIDColumn); |
||
| 402 | |||
| 403 | $from = $selectQuery->getFrom(); |
||
| 404 | unset($from[$this->joinTable]); |
||
| 405 | $selectQuery->setFrom($from); |
||
| 406 | $selectQuery->setOrderBy(); // ORDER BY in subselects breaks MS SQL Server and is not necessary here |
||
| 407 | $selectQuery->setDistinct(false); |
||
| 408 | |||
| 409 | // Use a sub-query as SQLite does not support setting delete targets in |
||
| 410 | // joined queries. |
||
| 411 | $delete = SQLDelete::create(); |
||
| 412 | $delete->setFrom("\"{$this->joinTable}\""); |
||
| 413 | $delete->addWhere($this->foreignIDFilter()); |
||
| 414 | $subSelect = $selectQuery->sql($parameters); |
||
| 415 | $delete->addWhere([ |
||
| 416 | "\"{$this->joinTable}\".\"{$this->localKey}\" IN ($subSelect)" => $parameters |
||
| 417 | ]); |
||
| 418 | |||
| 419 | $affectedIds = []; |
||
| 420 | if ($this->removeCallbacks) { |
||
| 421 | $affectedIds = $delete |
||
| 422 | ->toSelect() |
||
| 423 | ->setSelect("\"{$this->joinTable}\".\"{$this->localKey}\"") |
||
| 424 | ->execute() |
||
| 425 | ->column(); |
||
| 426 | } |
||
| 427 | |||
| 428 | // Perform the deletion |
||
| 429 | $delete->execute(); |
||
| 430 | |||
| 431 | if ($this->removeCallbacks && $affectedIds) { |
||
| 432 | $this->removeCallbacks->call($this, $affectedIds); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Find the extra field data for a single row of the relationship join |
||
| 438 | * table, given the known child ID. |
||
| 439 | * |
||
| 440 | * @param string $componentName The name of the component |
||
| 441 | * @param int $itemID The ID of the child for the relationship |
||
| 442 | * |
||
| 443 | * @return array Map of fieldName => fieldValue |
||
| 444 | */ |
||
| 445 | public function getExtraData($componentName, $itemID) |
||
| 446 | { |
||
| 447 | $result = []; |
||
| 448 | |||
| 449 | // Skip if no extrafields or unsaved record |
||
| 450 | if (empty($this->extraFields) || empty($itemID)) { |
||
| 451 | return $result; |
||
| 452 | } |
||
| 453 | |||
| 454 | if (!is_numeric($itemID)) { |
||
| 455 | throw new InvalidArgumentException('ManyManyList::getExtraData() passed a non-numeric child ID'); |
||
| 456 | } |
||
| 457 | |||
| 458 | $cleanExtraFields = []; |
||
| 459 | foreach ($this->extraFields as $fieldName => $dbFieldSpec) { |
||
| 460 | $cleanExtraFields[] = "\"{$fieldName}\""; |
||
| 461 | } |
||
| 462 | $query = SQLSelect::create($cleanExtraFields, "\"{$this->joinTable}\""); |
||
| 463 | $filter = $this->foreignIDWriteFilter($this->getForeignID()); |
||
| 464 | if ($filter) { |
||
| 465 | $query->setWhere($filter); |
||
| 466 | } else { |
||
| 467 | throw new BadMethodCallException("Can't call ManyManyList::getExtraData() until a foreign ID is set"); |
||
| 468 | } |
||
| 469 | $query->addWhere([ |
||
| 470 | "\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID |
||
| 471 | ]); |
||
| 472 | $queryResult = $query->execute()->current(); |
||
| 473 | if ($queryResult) { |
||
| 474 | foreach ($queryResult as $fieldName => $value) { |
||
| 475 | $result[$fieldName] = $value; |
||
| 476 | } |
||
| 477 | } |
||
| 478 | |||
| 479 | return $result; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Gets the join table used for the relationship. |
||
| 484 | * |
||
| 485 | * @return string the name of the table |
||
| 486 | */ |
||
| 487 | public function getJoinTable() |
||
| 488 | { |
||
| 489 | return $this->joinTable; |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Gets the key used to store the ID of the local/parent object. |
||
| 494 | * |
||
| 495 | * @return string the field name |
||
| 496 | */ |
||
| 497 | public function getLocalKey() |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Gets the key used to store the ID of the foreign/child object. |
||
| 504 | * |
||
| 505 | * @return string the field name |
||
| 506 | */ |
||
| 507 | public function getForeignKey() |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Gets the extra fields included in the relationship. |
||
| 514 | * |
||
| 515 | * @return array a map of field names to types |
||
| 516 | */ |
||
| 517 | public function getExtraFields() |
||
| 520 | } |
||
| 521 | } |
||
| 522 |
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.