Completed
Push — master ( db50e7...7354ed )
by Dmitry
15:54 queued 13:20
created

src/helpers/ChargeSort.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\helpers;
12
13
use hipanel\modules\finance\models\Charge;
14
use Tuck\Sort\Sort;
15
use Tuck\Sort\SortChain;
16
17
/**
18
 * Class PriceSort provides sorting functions for prices.
19
 *
20
 * @author Dmytro Naumenko <[email protected]>
21
 */
22
class ChargeSort
23
{
24
    public static function anyCharges(): SortChain
25
    {
26
        return Sort::chain()
27
            ->asc(self::byType())
28
            ->asc(self::byHardwareType())
29
            ->compare(self::byName())
30
            ->asc(self::keepDiscountsWithParents());
31
    }
32
33 View Code Duplication
    private static function byHardwareType(): \Closure
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $order = ['SERVER', 'CHASSIS', 'MOTHERBOARD', 'CPU', 'RAM', 'HDD', 'SSD'];
36
37
        return function (Charge $charge) use ($order) {
38
            if ($charge->class === 'part') {
0 ignored issues
show
The property class does not exist on object<hipanel\modules\finance\models\Charge>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
39
                $type = substr($charge->name, 0, strpos($charge->name, ':'));
40
                if (($key = array_search($type, $order, true)) !== false) {
41
                    return $key;
42
                }
43
            }
44
45
            return INF;
46
        };
47
    }
48
49 View Code Duplication
    private static function byType(): \Closure
50
    {
51
        $order = [
52
            'rack',
53
            'rack_unit',
54
            'ip_num',
55
            'support_time',
56
            'backup_du',
57
            'server_traf_max',
58
            'server_traf95_max',
59
            'server_du',
60
            'server_ssd',
61
            'server_sata',
62
            'win_license',
63
        ];
64
65
        return function (Charge $charge) use ($order) {
66
            if (($key = array_search($charge->type, $order, true)) !== false) {
67
                return $key;
68
            }
69
70
            return INF;
71
        };
72
    }
73
74
    private static function keepDiscountsWithParents(): \Closure
75
    {
76
        return function (Charge $charge) {
77
            $a = $charge->parent_id !== null
78
                ? $charge->parent_id * 10 + 2
79
                : $charge->id * 10 + 1;
80
81
            return $a;
82
        };
83
    }
84
85
    private static function byName(): \Closure
86
    {
87
        return function (Charge $a, Charge $b) {
88
            return strnatcasecmp($a->name, $b->name);
89
        };
90
    }
91
}
92