Conditions | 9 |
Paths | 80 |
Total Lines | 54 |
Code Lines | 33 |
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 |
||
36 | public function groupServerPrices() |
||
37 | { |
||
38 | $model = $this->plan; |
||
39 | /** @var Sale[] $salesByObject */ |
||
40 | $salesByObject = []; |
||
41 | /** @var Price[][] $pricesByMainObject */ |
||
42 | $pricesByMainObject = []; |
||
43 | |||
44 | foreach ($model->prices as $price) { |
||
45 | $pricesByMainObject[$price->main_object_id ?? $model->id][$price->id] = $price; |
||
46 | } |
||
47 | |||
48 | if (isset($pricesByMainObject[null])) { |
||
49 | $salesByObject[null] = new FakeSale([ |
||
50 | 'object' => Yii::t('hipanel.finance.price', 'Applicable for all objects'), |
||
51 | 'tariff_id' => $model->id, |
||
52 | ]); |
||
53 | } |
||
54 | if (isset($pricesByMainObject[$model->id])) { |
||
55 | $salesByObject[$model->id] = new FakeSale([ |
||
56 | 'object' => Yii::t('hipanel.finance.price', 'For the whole tariff'), |
||
57 | 'tariff_id' => $model->id, |
||
58 | 'object_id' => $model->id, |
||
59 | ]); |
||
60 | } |
||
61 | foreach ($model->sales as $sale) { |
||
62 | $salesByObject[$sale->object_id] = $sale; |
||
63 | } |
||
64 | |||
65 | foreach ($pricesByMainObject as $id => $prices) { |
||
66 | if (!isset($salesByObject[$id])) { |
||
67 | foreach ($prices as $price) { |
||
68 | if ((int)$price->object_id === (int)$id) { |
||
69 | $salesByObject[$id] = new FakeSale([ |
||
70 | 'object' => $price->object->name, |
||
71 | 'tariff_id' => $model->id, |
||
72 | 'object_id' => $price->object_id, |
||
73 | 'tariff_type' => $model->type, |
||
74 | ]); |
||
75 | continue 2; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | $salesByObject[$id] = new FakeSale([ |
||
80 | 'object' => Yii::t('hipanel.finance.price', 'Unknown object name - no direct object prices exist'), |
||
81 | 'tariff_id' => $model->id, |
||
82 | 'object_id' => $id, |
||
83 | 'tariff_type' => $model->type, |
||
84 | ]); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | return [$salesByObject, $pricesByMainObject]; |
||
89 | } |
||
90 | } |
||
91 |