| Conditions | 50 |
| Paths | > 20000 |
| Total Lines | 199 |
| Code Lines | 112 |
| Lines | 5 |
| Ratio | 2.51 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 434 | public function execute(array &$param_pool = null) |
||
| 435 | { |
||
| 436 | $result = new XMLElement($this->dsParamROOTELEMENT); |
||
| 437 | $this->_param_pool = $param_pool; |
||
| 438 | $where = null; |
||
| 439 | $joins = null; |
||
| 440 | $group = false; |
||
| 441 | |||
| 442 | if (!$section = SectionManager::fetch((int)$this->getSource())) { |
||
| 443 | $about = $this->about(); |
||
| 444 | trigger_error(__('The Section, %s, associated with the Data source, %s, could not be found.', array($this->getSource(), '<code>' . $about['name'] . '</code>')), E_USER_ERROR); |
||
| 445 | } |
||
| 446 | |||
| 447 | $sectioninfo = new XMLElement('section', General::sanitize($section->get('name')), array( |
||
| 448 | 'id' => $section->get('id'), |
||
| 449 | 'handle' => $section->get('handle') |
||
| 450 | )); |
||
| 451 | |||
| 452 | if ($this->_force_empty_result === true) { |
||
| 453 | if ($this->dsParamREDIRECTONREQUIRED === 'yes') { |
||
| 454 | throw new FrontendPageNotFoundException; |
||
| 455 | } |
||
| 456 | |||
| 457 | $this->_force_empty_result = false; //this is so the section info element doesn't disappear. |
||
| 458 | $error = new XMLElement('error', __("Data source not executed, required parameter is missing."), array( |
||
| 459 | 'required-param' => $this->dsParamREQUIREDPARAM |
||
| 460 | )); |
||
| 461 | $result->appendChild($error); |
||
| 462 | $result->prependChild($sectioninfo); |
||
| 463 | |||
| 464 | return $result; |
||
| 465 | } |
||
| 466 | |||
| 467 | if ($this->_negate_result === true) { |
||
| 468 | if ($this->dsParamREDIRECTONFORBIDDEN === 'yes') { |
||
| 469 | throw new FrontendPageNotFoundException; |
||
| 470 | } |
||
| 471 | |||
| 472 | $this->_negate_result = false; //this is so the section info element doesn't disappear. |
||
| 473 | $result = $this->negateXMLSet(); |
||
| 474 | $result->prependChild($sectioninfo); |
||
| 475 | |||
| 476 | return $result; |
||
| 477 | } |
||
| 478 | |||
| 479 | if (is_array($this->dsParamINCLUDEDELEMENTS)) { |
||
| 480 | $include_pagination_element = in_array('system:pagination', $this->dsParamINCLUDEDELEMENTS); |
||
| 481 | } else { |
||
| 482 | $this->dsParamINCLUDEDELEMENTS = array(); |
||
| 483 | } |
||
| 484 | |||
| 485 | if (isset($this->dsParamPARAMOUTPUT) && !is_array($this->dsParamPARAMOUTPUT)) { |
||
| 486 | $this->dsParamPARAMOUTPUT = array($this->dsParamPARAMOUTPUT); |
||
| 487 | } |
||
| 488 | |||
| 489 | $this->_can_process_system_parameters = $this->canProcessSystemParameters(); |
||
| 490 | |||
| 491 | if (!isset($this->dsParamPAGINATERESULTS)) { |
||
| 492 | $this->dsParamPAGINATERESULTS = 'yes'; |
||
| 493 | } |
||
| 494 | |||
| 495 | // Process Filters |
||
| 496 | $this->processFilters($where, $joins, $group); |
||
| 497 | |||
| 498 | // Process Sorting |
||
| 499 | if ($this->dsParamSORT === 'system:id') { |
||
| 500 | EntryManager::setFetchSorting('system:id', $this->dsParamORDER); |
||
| 501 | } elseif ($this->dsParamSORT === 'system:date' || $this->dsParamSORT === 'system:creation-date') { |
||
| 502 | EntryManager::setFetchSorting('system:creation-date', $this->dsParamORDER); |
||
| 503 | } elseif ($this->dsParamSORT === 'system:modification-date') { |
||
| 504 | EntryManager::setFetchSorting('system:modification-date', $this->dsParamORDER); |
||
| 505 | } else { |
||
| 506 | EntryManager::setFetchSorting( |
||
| 507 | FieldManager::fetchFieldIDFromElementName($this->dsParamSORT, $this->getSource()), |
||
| 508 | $this->dsParamORDER |
||
| 509 | ); |
||
| 510 | } |
||
| 511 | |||
| 512 | // combine `INCLUDEDELEMENTS`, `PARAMOUTPUT` and `GROUP` into an |
||
| 513 | // array of field handles to optimise the `EntryManager` queries |
||
| 514 | $datasource_schema = $this->dsParamINCLUDEDELEMENTS; |
||
| 515 | |||
| 516 | if (is_array($this->dsParamPARAMOUTPUT)) { |
||
| 517 | $datasource_schema = array_merge($datasource_schema, $this->dsParamPARAMOUTPUT); |
||
| 518 | } |
||
| 519 | |||
| 520 | if ($this->dsParamGROUP) { |
||
| 521 | $datasource_schema[] = FieldManager::fetchHandleFromID($this->dsParamGROUP); |
||
| 522 | } |
||
| 523 | |||
| 524 | $entries = EntryManager::fetchByPage( |
||
| 525 | ($this->dsParamPAGINATERESULTS === 'yes' && $this->dsParamSTARTPAGE > 0 ? $this->dsParamSTARTPAGE : 1), |
||
| 526 | $this->getSource(), |
||
| 527 | ($this->dsParamPAGINATERESULTS === 'yes' && $this->dsParamLIMIT >= 0 ? $this->dsParamLIMIT : null), |
||
| 528 | $where, |
||
| 529 | $joins, |
||
| 530 | $group, |
||
| 531 | (!$include_pagination_element ? true : false), |
||
| 532 | true, |
||
| 533 | array_unique($datasource_schema) |
||
| 534 | ); |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Immediately after building entries allow modification of the Data Source entries array |
||
| 538 | * |
||
| 539 | * @delegate DataSourceEntriesBuilt |
||
| 540 | * @param string $context |
||
| 541 | * '/frontend/' |
||
| 542 | * @param Datasource $datasource |
||
| 543 | * @param array $entries |
||
| 544 | * @param array $filters |
||
| 545 | */ |
||
| 546 | Symphony::ExtensionManager()->notifyMembers('DataSourceEntriesBuilt', '/frontend/', array( |
||
| 547 | 'datasource' => &$this, |
||
| 548 | 'entries' => &$entries, |
||
| 549 | 'filters' => $this->dsParamFILTERS |
||
| 550 | )); |
||
| 551 | |||
| 552 | if (($entries['total-entries'] <= 0 || $include_pagination_element === true) && (!is_array($entries['records']) || empty($entries['records'])) || (int)$this->dsParamSTARTPAGE === 0) { |
||
| 553 | if ($this->dsParamREDIRECTONEMPTY === 'yes') { |
||
| 554 | throw new FrontendPageNotFoundException; |
||
| 555 | } |
||
| 556 | |||
| 557 | $this->_force_empty_result = false; |
||
| 558 | $result = $this->emptyXMLSet(); |
||
| 559 | $result->prependChild($sectioninfo); |
||
| 560 | |||
| 561 | if ($include_pagination_element) { |
||
| 562 | $pagination_element = General::buildPaginationElement(); |
||
| 563 | |||
| 564 | if ($pagination_element instanceof XMLElement && $result instanceof XMLElement) { |
||
| 565 | $result->prependChild($pagination_element); |
||
| 566 | } |
||
| 567 | } |
||
| 568 | } else { |
||
| 569 | if (!$this->_param_output_only) { |
||
| 570 | $result->appendChild($sectioninfo); |
||
| 571 | |||
| 572 | if ($include_pagination_element) { |
||
| 573 | $t = ($this->dsParamPAGINATERESULTS === 'yes' && isset($this->dsParamLIMIT) && $this->dsParamLIMIT >= 0 ? $this->dsParamLIMIT : $entries['total-entries']); |
||
| 574 | |||
| 575 | $pagination_element = General::buildPaginationElement( |
||
| 576 | $entries['total-entries'], |
||
| 577 | $entries['total-pages'], |
||
| 578 | $t, |
||
| 579 | ($this->dsParamPAGINATERESULTS === 'yes' && $this->dsParamSTARTPAGE > 0 ? $this->dsParamSTARTPAGE : 1) |
||
| 580 | ); |
||
| 581 | |||
| 582 | if ($pagination_element instanceof XMLElement && $result instanceof XMLElement) { |
||
| 583 | $result->prependChild($pagination_element); |
||
| 584 | } |
||
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | // If this datasource has a Limit greater than 0 or the Limit is not set |
||
| 589 | if (!isset($this->dsParamLIMIT) || $this->dsParamLIMIT > 0) { |
||
| 590 | if (!isset($this->dsParamASSOCIATEDENTRYCOUNTS) || $this->dsParamASSOCIATEDENTRYCOUNTS === 'yes') { |
||
| 591 | $this->_associated_sections = $section->fetchChildAssociations(); |
||
| 592 | } |
||
| 593 | |||
| 594 | // If the datasource require's GROUPING |
||
| 595 | if (isset($this->dsParamGROUP)) { |
||
| 596 | self::$_fieldPool[$this->dsParamGROUP] = FieldManager::fetch($this->dsParamGROUP); |
||
| 597 | |||
| 598 | if (self::$_fieldPool[$this->dsParamGROUP] === null) { |
||
| 599 | throw new SymphonyErrorPage(vsprintf("The field used for grouping '%s' cannot be found.", $this->dsParamGROUP)); |
||
| 600 | } |
||
| 601 | |||
| 602 | $groups = self::$_fieldPool[$this->dsParamGROUP]->groupRecords($entries['records']); |
||
| 603 | |||
| 604 | foreach ($groups as $element => $group) { |
||
| 605 | foreach ($group as $g) { |
||
| 606 | $result->appendChild( |
||
| 607 | $this->processRecordGroup($element, $g) |
||
| 608 | ); |
||
| 609 | } |
||
| 610 | } |
||
| 611 | } else { |
||
| 612 | View Code Duplication | if (isset($entries['records'][0])) { |
|
| 613 | $data = $entries['records'][0]->getData(); |
||
| 614 | $pool = FieldManager::fetch(array_keys($data)); |
||
| 615 | self::$_fieldPool += $pool; |
||
| 616 | } |
||
| 617 | |||
| 618 | foreach ($entries['records'] as $entry) { |
||
| 619 | $xEntry = $this->processEntry($entry); |
||
| 620 | |||
| 621 | if ($xEntry instanceof XMLElement) { |
||
| 622 | $result->appendChild($xEntry); |
||
| 623 | } |
||
| 624 | } |
||
| 625 | } |
||
| 626 | } |
||
| 627 | } |
||
| 628 | |||
| 629 | $param_pool = $this->_param_pool; |
||
| 630 | |||
| 631 | return $result; |
||
| 632 | } |
||
| 633 | } |
||
| 634 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: