Completed
Pull Request — master (#137)
by Alex
01:52
created

EntityGubbins::addAssociation()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 8
nc 4
nop 2
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities;
4
5
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\Association;
6
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationMonomorphic;
7
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubBase;
8
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubMonomorphic;
9
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubPolymorphic;
10
use POData\Providers\Metadata\ResourceEntityType;
11
12
class EntityGubbins
13
{
14
    /**
15
     * @var string
16
     */
17
    private $name;
18
19
    /**
20
     * @var string
21
     */
22
    private $className;
23
24
    /**
25
     * @var EntityField[]
26
     */
27
    private $keyFields;
28
29
    /**
30
     * @var EntityField[]
31
     */
32
    private $fields;
33
34
    /**
35
     * @var AssociationStubBase[]
36
     */
37
    private $stubs;
38
39
    /**
40
     * @var Association[]
41
     */
42
    private $associations;
43
    /**
44
     * @var ResourceEntityType
45
     */
46
    private $odataResourceType;
47
48
    /**
49
     * @return ResourceEntityType
50
     */
51
    public function getOdataResourceType()
52
    {
53
        return $this->odataResourceType;
54
    }
55
56
    /**
57
     * @param ResourceEntityType $odataType
58
     */
59
    public function setOdataResourceType(ResourceEntityType $odataType)
60
    {
61
        if ($odataType->isAbstract()) {
62
            $msg = 'OData resource entity type must be concrete';
63
            throw new \InvalidArgumentException($msg);
64
        }
65
        $this->odataResourceType = $odataType;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getName()
72
    {
73
        return $this->name;
74
    }
75
76
    /**
77
     * @param string $name
78
     */
79
    public function setName($name)
80
    {
81
        $this->name = $name;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getClassName()
88
    {
89
        return $this->className;
90
    }
91
92
    /**
93
     * @param string $className
94
     */
95
    public function setClassName($className)
96
    {
97
        $this->className = $className;
98
    }
99
100
    /**
101
     * @return EntityField[]
102
     */
103
    public function getKeyFields()
104
    {
105
        return $this->keyFields;
106
    }
107
108
    /**
109
     * @return EntityField[]
110
     */
111
    public function getFields()
112
    {
113
        return $this->fields;
114
    }
115
116
    /**
117
     * @param EntityField[] $fields
118
     */
119
    public function setFields(array $fields)
120
    {
121
        if (0 == count($fields)) {
122
            $msg = 'Fields array must not be empty';
123
            throw new \Exception($msg);
124
        }
125
        $keys = [];
126
        foreach ($fields as $propName => $field) {
127
            if (!$field instanceof EntityField) {
128
                $msg = 'Fields array must only have EntityField objects';
129
                throw new \Exception($msg);
130
            }
131
            if ($field->getIsKeyField()) {
132
                $keys[$propName] = $field;
133
            }
134
        }
135
        if (0 == count($keys)) {
136
            $msg = 'No key field supplied in fields array';
137
            throw new \Exception($msg);
138
        }
139
        $this->fields = $fields;
140
        $this->keyFields = $keys;
141
    }
142
143
    /**
144
     * @return AssociationStubBase[]
145
     */
146
    public function getStubs()
147
    {
148
        return $this->stubs;
149
    }
150
151
    /**
152
     * @param AssociationStubBase[] $stubs
153
     */
154
    public function setStubs(array $stubs)
155
    {
156
        foreach ($stubs as $field) {
157
            if (!$field instanceof AssociationStubBase) {
158
                $msg = 'Stubs array must only have AssociationStubBase objects';
159
                throw new \Exception($msg);
160
            }
161
        }
162
        $this->stubs = $stubs;
163
    }
164
165
    /**
166
     * @param Association $association
167
     * @param bool        $isFirst
168
     */
169
    public function addAssociation(Association $association, $isFirst = true)
170
    {
171
        if ($association instanceof AssociationMonomorphic) {
172
            $stub = $isFirst ? $association->getFirst() : $association->getLast();
173
            if (null === $stub || (!in_array($stub, $this->stubs) && !($stub instanceof AssociationStubPolymorphic))) {
174
                throw new \InvalidArgumentException('Association cannot be connected to this entity');
175
            }
176
            $propertyName = $stub->getRelationName();
177
        }
178
        assert(isset($propertyName));
179
        $this->associations[$propertyName] = $association;
0 ignored issues
show
Bug introduced by
The variable $propertyName 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...
180
    }
181
182
    /**
183
     * @return String[]
184
     */
185
    protected function getFieldNames()
186
    {
187
        $fieldNames = [];
188
        foreach ($this->fields as $field) {
189
            $fieldNames[] = $field->getName();
190
        }
191
        return $fieldNames;
192
    }
193
194
    /**
195
     * @return String[]
196
     */
197
    protected function getAssociationNames()
198
    {
199
        if (empty($this->stubs)) {
200
            return [];
201
        }
202
        $assocationNames = [];
203
        foreach ($this->stubs as $stub) {
204
            $assocationNames[] = $stub->getRelationName();
205
        }
206
        return $assocationNames;
207
    }
208
209
    /**
210
     * @return Associations\Association[]
211
     */
212
    public function getAssociations()
213
    {
214
        return $this->associations;
215
    }
216
217
    /**
218
     * @param $relName
219
     * @return Association|null
220
     */
221
    public function resolveAssociation($relName)
222
    {
223
        $this->associations = is_array($this->associations) ? $this->associations : [];
224
        return array_key_exists($relName, $this->associations) ? $this->associations[$relName] : null;
225
    }
226
227
    /**
228
     * @return bool
229
     */
230
    public function isOK()
231
    {
232
        $fieldNames = $this->getFieldNames();
233
        $associationNames = $this->getAssociationNames();
234
        $intersection = array_intersect($fieldNames, $associationNames);
235
        return 0 === count($intersection);
236
    }
237
}
238