Conditions | 16 |
Paths | 64 |
Total Lines | 83 |
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 |
||
62 | protected function _run() { |
||
63 | /** |
||
64 | * @var ActiveRecord $filter |
||
65 | */ |
||
66 | $filter = $this->model; |
||
67 | // if ($filter->getBehavior('relationsSaver')) { |
||
68 | // $filter->detachBehavior('relationsSaver'); |
||
69 | // } |
||
70 | |||
71 | if ($this->scenario !== ActiveRecord::SCENARIO_DEFAULT) { |
||
72 | $filter->scenario = $this->scenario; |
||
73 | } |
||
74 | |||
75 | parent::_run(); |
||
76 | |||
77 | $isValid = $filter->validate(); |
||
78 | /** |
||
79 | * @var ArrayDataProvider $dataProvider |
||
80 | */ |
||
81 | $dataProvider = $filter->search(); |
||
82 | if (!$isValid) { |
||
83 | $dataProvider->query->andWhere('false'); |
||
84 | } |
||
85 | |||
86 | if (!empty($this->actionParams->get['handle'])) { |
||
87 | $handlerKey = $this->actionParams->get['handle']; |
||
88 | $handlers = $this->handlers; |
||
89 | if (!empty($handlers[$handlerKey])) { |
||
90 | $handler = $handlers[$handlerKey]; |
||
91 | $handler->dataProvider = $dataProvider; |
||
92 | if (($result = $handler->run()) !== null) { |
||
93 | return $result; |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | $actionParams = $this->actionParams; |
||
99 | $response = $this->getResponse(); |
||
100 | if ($actionParams->isAjax && !$actionParams->isPjax && !$this->isDisableAjax && $dataProvider) { |
||
101 | if (isset($this->actionParams->get['depdrop_parents'])) { |
||
102 | $key = 'output'; |
||
103 | $dataProvider->pagination->pageSize = 500; |
||
104 | } else { |
||
105 | $key = 'results'; |
||
106 | } |
||
107 | |||
108 | $result = []; |
||
109 | foreach ($dataProvider->models as $row) { |
||
110 | $attributes = $this->getAttributes(); |
||
111 | $modelAttributes = array_values($attributes); |
||
112 | if ($row instanceof Model) { |
||
113 | $row = $row->getAttributes($modelAttributes); |
||
114 | } |
||
115 | $res = []; |
||
116 | foreach ($attributes as $targetKey => $attribute) { |
||
117 | if (is_int($targetKey)) { |
||
118 | $targetKey = $attribute; |
||
119 | } |
||
120 | |||
121 | $res[$targetKey] = $row[$attribute]; |
||
122 | } |
||
123 | |||
124 | $result[] = $res; |
||
125 | } |
||
126 | |||
127 | $pagination = $dataProvider->pagination; |
||
128 | $response->content = [ |
||
129 | $key => $result, |
||
130 | 'pagination' => [ |
||
131 | 'more' => $pagination ? ($pagination->page < $pagination->pageCount - 1) : false, |
||
132 | ], |
||
133 | ]; |
||
134 | |||
135 | $response->format = Response::FORMAT_JSON; |
||
136 | } else { |
||
137 | $response->content = [ |
||
138 | 'filter' => $filter, |
||
139 | 'dataProvider' => $dataProvider, |
||
140 | ]; |
||
141 | } |
||
142 | |||
143 | return $response; |
||
144 | } |
||
145 | |||
154 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: