| Conditions | 11 |
| Paths | 49 |
| Total Lines | 66 |
| Code Lines | 30 |
| 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 |
||
| 73 | public function execute() |
||
| 74 | { |
||
| 75 | if ($this->getAdapter() === null || !$this->getAdapter() instanceof \Zend\Db\Sql\Sql) { |
||
| 76 | throw new \Exception('Object "Zend\Db\Sql\Sql" is missing, please call setAdapter() first!'); |
||
| 77 | } |
||
| 78 | |||
| 79 | $platform = $this->getAdapter() |
||
| 80 | ->getAdapter() |
||
| 81 | ->getPlatform(); |
||
| 82 | |||
| 83 | $select = $this->getData(); |
||
| 84 | |||
| 85 | /* |
||
| 86 | * Step 1) Apply needed columns |
||
| 87 | */ |
||
| 88 | $selectColumns = []; |
||
| 89 | foreach ($this->getColumns() as $col) { |
||
| 90 | if (!$col instanceof Column\Select) { |
||
| 91 | continue; |
||
| 92 | } |
||
| 93 | |||
| 94 | $colString = $col->getSelectPart1(); |
||
| 95 | if ($col->getSelectPart2() != '') { |
||
| 96 | $colString = new Expression($platform->quoteIdentifier($colString).$platform->getIdentifierSeparator().$platform->quoteIdentifier($col->getSelectPart2())); |
||
| 97 | } |
||
| 98 | |||
| 99 | $selectColumns[$col->getUniqueId()] = $colString; |
||
| 100 | } |
||
| 101 | $select->columns($selectColumns, false); |
||
| 102 | |||
| 103 | $joins = $select->getRawState('joins'); |
||
| 104 | $select->reset('joins'); |
||
| 105 | foreach ($joins as $join) { |
||
|
|
|||
| 106 | $select->join($join['name'], $join['on'], [], $join['type']); |
||
| 107 | } |
||
| 108 | |||
| 109 | /* |
||
| 110 | * Step 2) Apply sorting |
||
| 111 | */ |
||
| 112 | if (!empty($this->getSortConditions())) { |
||
| 113 | // Minimum one sort condition given -> so reset the default orderBy |
||
| 114 | $select->reset(Sql\Select::ORDER); |
||
| 115 | |||
| 116 | foreach ($this->getSortConditions() as $sortCondition) { |
||
| 117 | /** @var \ZfcDataGrid\Column\AbstractColumn $col */ |
||
| 118 | $col = $sortCondition['column']; |
||
| 119 | $select->order($col->getUniqueId().' '.$sortCondition['sortDirection']); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /* |
||
| 124 | * Step 3) Apply filters |
||
| 125 | */ |
||
| 126 | $filterColumn = new ZendSelect\Filter($this->getAdapter(), $select); |
||
| 127 | foreach ($this->getFilters() as $filter) { |
||
| 128 | /* @var $filter \ZfcDatagrid\Filter */ |
||
| 129 | if ($filter->isColumnFilter() === true) { |
||
| 130 | $filterColumn->applyFilter($filter); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | /* |
||
| 135 | * Step 4) Pagination |
||
| 136 | */ |
||
| 137 | $this->setPaginatorAdapter(new PaginatorAdapter($select, $this->getAdapter())); |
||
| 138 | } |
||
| 139 | } |
||
| 140 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.