Conditions | 14 |
Paths | 4 |
Total Lines | 42 |
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 |
||
38 | public function format(Quantity $quantity): string |
||
39 | { |
||
40 | $format = $this->formatter; |
||
41 | $format->sizeFormatBase = 1000; |
||
42 | |||
43 | // TODO: split to different classes |
||
44 | switch ($quantity->getUnit()->getMeasure()) { |
||
45 | case 'bit': |
||
46 | return Yii::t('hiqdev.units', '{quantity, number} {unit}', [ |
||
47 | 'quantity' => $quantity->getQuantity(), |
||
48 | 'unit' => (function (string $unitName) { |
||
49 | switch ($unitName) { |
||
50 | case 'b': return Yii::t('hiqdev.units', 'B'); |
||
51 | case 'kb': return Yii::t('hiqdev.units', 'kB'); |
||
52 | case 'mb': return Yii::t('hiqdev.units', 'MB'); |
||
53 | case 'gb': return Yii::t('hiqdev.units', 'GB'); |
||
54 | case 'tb': return Yii::t('hiqdev.units', 'TB'); |
||
55 | case 'pb': return Yii::t('hiqdev.units', 'PB'); |
||
56 | } |
||
57 | |||
58 | return Yii::t('hiqdev.units', ''); |
||
59 | })($quantity->getUnit()->getName()) |
||
60 | ]); |
||
61 | case 'bps': |
||
62 | return Yii::t('hiqdev.units', '{quantity, number} {unit}', [ |
||
63 | 'quantity' => $quantity->getQuantity(), |
||
64 | 'unit' => (function (string $unitName) { |
||
65 | switch ($unitName) { |
||
66 | case 'bps': return Yii::t('hiqdev.units', 'b/s'); |
||
67 | case 'kbps': return Yii::t('hiqdev.units', 'Kb/s'); |
||
68 | case 'mbps': return Yii::t('hiqdev.units', 'Mb/s'); |
||
69 | case 'gbps': return Yii::t('hiqdev.units', 'Gb/s'); |
||
70 | } |
||
71 | return Yii::t('hiqdev.units', ''); |
||
72 | })($quantity->getUnit()->getName()) |
||
73 | ]); |
||
74 | case 'item': |
||
75 | return Yii::t('hiqdev.units', '{quantity, plural, one{# item} other{# items}}', ['quantity' => $quantity->getQuantity()]); |
||
76 | } |
||
77 | |||
78 | return $this->simpleFormatter->format($quantity); |
||
79 | } |
||
80 | } |
||
81 |