Completed
Push — master ( c2bc12...ef5bc2 )
by Dmitry
04:55
created

src/forms/CertificateTariffForm.php (1 issue)

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\forms;
12
13
use hipanel\modules\finance\logic\IntegrityException;
14
use hipanel\modules\finance\models\CertificateResource;
15
use yii\web\UnprocessableEntityHttpException;
16
17
/**
18
 * Class CertificateTariffForm.
19
 *
20
 * @author Dmytro Naumenko <[email protected]>
21
 */
22
class CertificateTariffForm extends AbstractTariffForm
23
{
24
    protected $certificateTypes = [];
25
26
    public function __construct(array $config = [])
27
    {
28
        parent::__construct($config);
29
    }
30
31 View Code Duplication
    public function load($data, $formName = null)
32
    {
33
        $this->setAttributes($data[$this->formName()]);
34
        $this->setResources($data[(new CertificateResource())->formName()]);
35
36
        $this->initTariff();
37
38
        return true;
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public static function getPeriods()
45
    {
46
        return CertificateResource::getPeriods();
47
    }
48
49
    public function setResources($resources)
50
    {
51
        $result = [];
52
        foreach ($resources as $resource) {
53
            $result[] = $this->createResource($resource);
54
        }
55
56
        $this->_resources = $result;
57
58
        return $this;
59
    }
60
61
    protected function createResource($resource)
62
    {
63
        if ($resource instanceof CertificateResource) {
64
            return $resource;
65
        }
66
67
        $model = new CertificateResource(['scenario' => $this->scenario ?: 'default']);
68
        if ($model->load($resource, '') && $model->validate()) {
69
            return $model;
70
        } else {
71
            throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors()));
72
        }
73
    }
74
75
    public function getCertificateTypes()
76
    {
77
        $result = [];
78
79
        foreach ($this->tariff->resources as $resource) {
80
            if (isset($this->certificateTypes[$resource->object_id])) {
81
                $result[$resource->object_id] = $this->certificateTypes[$resource->object_id];
82
            }
83
        }
84
85
        return $result;
86
    }
87
88
    protected function getCertificateTypeId($type)
89
    {
90
        return array_search($type, $this->certificateTypes, true);
91
    }
92
93
    /**
94
     * @param $type
95
     * @throws IntegrityException
96
     * @return CertificateResource[]
97
     */
98
    public function getTypeResources($type)
99
    {
100
        return $this->extractResources($type, $this->tariff->resources);
101
    }
102
103
    /**
104
     * @param array $certificateTypes
105
     */
106
    public function setCertificateTypes($certificateTypes)
107
    {
108
        $this->certificateTypes = $certificateTypes;
109
    }
110
111
    /**
112
     * @param $type
113
     * @throws IntegrityException
114
     * @return CertificateResource[]
115
     */
116
    public function getTypeParentResources($type)
117
    {
118
        return $this->extractResources($type, $this->parentTariff->resources);
119
    }
120
121
    protected function extractResources($certificateType, $resources)
122
    {
123
        $id = $this->getCertificateTypeId($certificateType);
124
125
        $tmpres = [];
126
127
        foreach ($resources as $resource) {
128
            if (strcmp($resource->object_id, $id) === 0 && $resource->isTypeCorrect()) {
129
                $tmpres[$resource->type] = $resource;
130
            }
131
        }
132
133
        $types = $resource->getTypes();
134
        /* XXX why die? let's try with empty resource
135
         * if (count($tmpres) !== count($types)) {
136
            throw new IntegrityException('Found ' . count($tmpres) . ' resources for certificate "' . $type . '". Must be exactly ' . count($types));
137
        }
138
139
        // sorts $tmpres by order of $resource->getTypes()
140
        $tmpres = array_merge($types, $tmpres);
141
         */
142
143
        foreach (array_keys($types) as $type) {
144
            $res[$type] = isset($tmpres[$type]) ? $tmpres[$type] : $this->createResource([
145
                'object_id' => $id,
146
                'type' => $type,
147
            ]);
148
        }
149
150
        return $res;
0 ignored issues
show
The variable $res does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
151
    }
152
}
153