Conditions | 14 |
Paths | 624 |
Total Lines | 72 |
Code Lines | 38 |
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 |
||
58 | protected function prepareModels() |
||
59 | { |
||
60 | if (!$this->platform) { |
||
61 | $this->platform = 'bower,npm'; |
||
62 | } |
||
63 | if (is_array($this->platform)) { |
||
64 | $this->platform = implode(',', $this->platform); |
||
65 | } |
||
66 | |||
67 | $query = [ |
||
68 | 'q' => $this->query, |
||
69 | 'platforms' => $this->platform, |
||
70 | ]; |
||
71 | |||
72 | if (($sort = $this->getSort()) !== false) { |
||
73 | foreach ($sort->getOrders() as $sort => $order) { |
||
74 | $query['sort'] = $sort; |
||
75 | $query['order'] = ($order === SORT_ASC) ? 'asc' : 'desc'; |
||
76 | break; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | if (($pagination = $this->getPagination()) !== false) { |
||
81 | /** |
||
82 | * Not use '$pagination->getPage()' because need define 'totalCount' first |
||
83 | * totalCount is defined after request. |
||
84 | */ |
||
85 | $page = (int) Yii::$app->getRequest()->getQueryParam($pagination->pageParam, 1); |
||
86 | if ($page > 1) { |
||
87 | $query['page'] = $page; |
||
88 | } |
||
89 | |||
90 | $query['per_page'] = $pagination->getPageSize(); |
||
91 | } |
||
92 | |||
93 | $cacheKey = [static::className(), 'query' => $query]; |
||
94 | |||
95 | $items = []; |
||
96 | $totalCount = 0; |
||
97 | |||
98 | if ($this->cache && $this->cache->exists($cacheKey)) { |
||
99 | list($totalCount, $items) = $this->cache->get($cacheKey); |
||
100 | } else { |
||
101 | /* @var $response Response */ |
||
102 | $response = $this->repository->search($query); |
||
103 | |||
104 | if ($response->getStatusCode() !== 200) { |
||
105 | return []; |
||
106 | } |
||
107 | |||
108 | $totalCount = (int) $response->getHeaderLine('total'); |
||
109 | $items = Json::decode($response->getBody()); |
||
110 | |||
111 | if ($this->cache) { |
||
112 | $this->cache->set($cacheKey, [$totalCount, $items], $this->cacheDuration); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | $this->lastTotalCount = $totalCount; |
||
117 | |||
118 | if (($pagination = $this->getPagination()) !== false) { |
||
119 | $pagination->totalCount = $this->lastTotalCount; |
||
120 | } |
||
121 | |||
122 | $models = []; |
||
123 | |||
124 | foreach ($items as $item) { |
||
125 | $models[] = new Project(['attributes' => $item]); |
||
126 | } |
||
127 | |||
128 | return $models; |
||
129 | } |
||
130 | |||
176 |