Calculator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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\logic;
12
13
use hipanel\modules\finance\models\CalculableModelInterface;
14
use hipanel\modules\finance\models\Calculation;
15
use yii\base\Model;
16
use yii\web\UnprocessableEntityHttpException;
17
18
/**
19
 * Class Calculator performs prices calculation for the [[models]].
20
 *
21
 * @author Dmytro Naumenko <[email protected]>
22
 */
23
class Calculator implements CalculatorInterface, ServerTariffCalculatorInterface
24
{
25
    /**
26
     * @var Model[]|CalculableModelInterface[]
27
     */
28
    protected $models;
29
30
    /**
31
     * @var Calculation[]
32
     */
33
    protected $calculations;
34
35
    /**
36
     * TariffCalculator constructor.
37
     * @param Model[] $models
38
     */
39
    public function __construct($models)
40
    {
41
        $this->models = $models;
42
    }
43
44
    /**
45
     * Gets [[Calculation]] for the $id.
46
     *
47
     * @param integer $id
48
     * @return Calculation
49
     */
50
    public function getCalculation($id)
51
    {
52
        if ($this->calculations === null) {
53
            $this->execute();
54
        }
55
56
        return $this->calculations[$id] ?? null;
57
    }
58
59
    /**
60
     * @return Calculation[]
61
     */
62
    public function getCalculations()
63
    {
64
        if ($this->calculations === null) {
65
            $this->execute();
66
        }
67
68
        return $this->calculations;
69
    }
70
71
    /**
72
     * @throws UnprocessableEntityHttpException
73
     * @return \hipanel\modules\finance\models\Calculation[]
74
     */
75
    public function execute()
76
    {
77
        $data = $this->collectData();
78
79
        if (empty($data)) {
80
            return [];
81
        }
82
83
        try {
84
            $rows = Calculation::perform('calc-value', $data, ['batch' => true]);
85
        } catch (\Exception $e) {
86
            throw new UnprocessableEntityHttpException('Failed to calculate value: ' . $e->getMessage(), 0, $e);
87
        }
88
89
        $this->calculations = $this->createCalculations($rows);
90
91
        return $this->calculations;
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    protected function collectData()
98
    {
99
        $data = [];
100
        foreach ($this->models as $model) {
101
            $calculation = $model->getCalculationModel();
0 ignored issues
show
Bug introduced by
The method getCalculationModel does only exist in hipanel\modules\finance\...alculableModelInterface, but not in yii\base\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102
            $data[$calculation->calculation_id] = $calculation->toArray();
103
        }
104
105
        return $data;
106
    }
107
108
    /**
109
     * @param $rows
110
     * @return \hipanel\modules\finance\models\Calculation[]
111
     */
112
    private function createCalculations($rows)
113
    {
114
        $query = Calculation::find()
115
            ->joinWith(['value', 'valueConverted'])
116
            ->indexBy('calculation_id');
117
118
        $query->prepare();
119
120
        return $query->populate($rows);
121
    }
122
}
123