Completed
Push — master ( d9c24b...dc8c4b )
by Dmitry
12:22
created

Calculator::collectData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace hipanel\modules\finance\logic;
4
5
use hipanel\modules\finance\models\Calculation;
6
use hipanel\modules\finance\models\CalculableModelInterface;
7
use yii\base\Model;
8
use yii\web\UnprocessableEntityHttpException;
9
10
class Calculator
11
{
12
    /**
13
     * @var Model[]|CalculableModelInterface[]
14
     */
15
    protected $models;
16
17
    /**
18
     * @var Calculation[]
19
     */
20
    protected $calculations;
21
22
    /**
23
     * TariffCalculator constructor.
24
     * @param Model[] $models
25
     */
26
    public function __construct($models)
27
    {
28
        $this->models = $models;
29
    }
30
31
    /**
32
     * Gets [[Calculation]] for the $id
33
     *
34
     * @param integer $id
35
     * @return Calculation
36
     */
37
    public function getCalculation($id)
38
    {
39
        if ($this->calculations === null) {
40
            $this->execute();
41
        }
42
43
        return isset($this->calculations[$id]) ? $this->calculations[$id] : null;
44
    }
45
46
    /**
47
     * @return Calculation[]
48
     */
49
    public function getCalculations()
50
    {
51
        if ($this->calculations === null) {
52
            $this->execute();
53
        }
54
55
        return $this->calculations;
56
    }
57
58
    /**
59
     * @return \hipanel\modules\finance\models\Calculation[]
60
     * @throws UnprocessableEntityHttpException
61
     */
62
    public function execute()
63
    {
64
        $data = $this->collectData();
65
66
        if (empty($data)) {
67
            return [];
68
        }
69
70
        try {
71
            $rows = Calculation::perform('CalcValue', $data, true);
72
        } catch (\Exception $e) {
73
            throw new UnprocessableEntityHttpException('Failed to calculate value: ' . $e->getMessage(), 0, $e);
74
        }
75
76
        $this->calculations = $this->createCalculations($rows);
77
78
        return $this->calculations;
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    protected function collectData()
85
    {
86
        $data = [];
87
        foreach ($this->models as $model) {
88
            $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...
89
            $data[$calculation->calculation_id] = $calculation->toArray();
90
        }
91
92
        return $data;
93
    }
94
95
    /**
96
     * @param $rows
97
     * @return \hipanel\modules\finance\models\Calculation[]
98
     */
99
    private function createCalculations($rows)
100
    {
101
        $query = Calculation::find()->joinWith(['value'])->indexBy('calculation_id');
102
        $query->prepare();
103
104
        return $query->populate($rows);
105
    }
106
}
107