Completed
Pull Request — master (#99)
by Christopher
17:35 queued 09:53
created

MetadataManager::getLastError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace AlgoWeb\ODataMetadata;
4
5
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType;
6
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType\EndAnonymousType;
7
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType;
8
use AlgoWeb\ODataMetadata\MetadataV3\edm\TAssociationEndType;
9
use AlgoWeb\ODataMetadata\MetadataV3\edm\TAssociationType;
10
use AlgoWeb\ODataMetadata\MetadataV3\edm\TComplexTypeType;
11
use AlgoWeb\ODataMetadata\MetadataV3\edm\TConstraintType;
12
use AlgoWeb\ODataMetadata\MetadataV3\edm\TDocumentationType;
13
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType;
14
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType;
15
use AlgoWeb\ODataMetadata\MetadataV3\edm\TNavigationPropertyType;
16
use AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyRefType;
17
use AlgoWeb\ODataMetadata\MetadataV3\edm\TReferentialConstraintRoleElementType;
18
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx;
19
use Illuminate\Support\Str;
20
use JMS\Serializer\SerializerBuilder;
21
22
class MetadataManager
23
{
24
    private $V3Edmx = null;
25
    private $oldEdmx = null;
0 ignored issues
show
Unused Code introduced by
The property $oldEdmx is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
    private $lastError = null;
27
    private $serializer = null;
28
29
    public function __construct($namespaceName = "Data", $containerName = "DefaultContainer")
30
    {
31
        $this->V3Edmx = new Edmx($namespaceName, $containerName);
32
        if (!$this->V3Edmx->isOK($msg)) {
33
            throw new \Exception($msg);
34
        }
35
        $ymlDir = __DIR__ . DIRECTORY_SEPARATOR . "MetadataV3" . DIRECTORY_SEPARATOR . "JMSmetadata";
36
        $this->serializer =
37
            SerializerBuilder::create()
38
                ->addMetadataDir($ymlDir)
39
                ->build();
40
    }
41
42
    public function getEdmx()
43
    {
44
        $msg = null;
45
        assert($this->V3Edmx->isOk($msg), $msg);
46
        return $this->V3Edmx;
47
    }
48
49
    public function getEdmxXML()
50
    {
51
        return $this->serializer->serialize($this->getEdmx(), "xml");
52
    }
53
54
    public function addEntityType($name, $accessType = "Public", $summary = null, $longDescription = null)
55
    {
56
        $this->startEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...:startEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
57
        $NewEntity = new TEntityTypeType();
58
        $NewEntity->setName($name);
59 View Code Duplication
        if (null != $summary || null != $longDescription) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
60
            $documentation = new TDocumentationType();
61
            $documentation->setSummary($summary);
62
            $documentation->setLongDescription($longDescription);
63
            $NewEntity->setDocumentation($documentation);
64
        }
65
66
        $entitySet = new EntitySetAnonymousType();
67
        $entitySet->setName(Str::plural($NewEntity->getName(), 2));
68
        $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace();
69 View Code Duplication
        if (0 == strlen(trim($namespace))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70
            $entityTypeName = $NewEntity->getName();
71
        } else {
72
            $entityTypeName = $namespace . "." . $NewEntity->getName();
73
        }
74
        $entitySet->setEntityType($entityTypeName);
75
        $entitySet->setGetterAccess($accessType);
76
77
        $this->V3Edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
78
        $this->V3Edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
79
        if (!$this->V3Edmx->isok($this->lastError)) {
80
            $this->revertEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...revertEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
81
            return false;
82
        }
83
        $this->commitEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...commitEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
84
        return $NewEntity;
85
    }
86
87
    public function addComplexType($name, $accessType = "Public", $summary = null, $longDescription = null)
88
    {
89
        $NewEntity = new TComplexTypeType();
90
        $NewEntity->setName($name);
91
        $NewEntity->setTypeAccess($accessType);
92 View Code Duplication
        if (null != $summary || null != $longDescription) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
93
            $documentation = new TDocumentationType();
94
            $documentation->setSummary($summary);
95
            $documentation->setLongDescription($longDescription);
96
            $NewEntity->setDocumentation($documentation);
97
        }
98
        $this->V3Edmx->getDataServiceType()->getSchema()[0]->addToComplexType($NewEntity);
99
100
        return $NewEntity;
101
    }
102
103
    private function startEdmxTransaction()
104
    {
105
        //$this->oldEdmx = serialize($this->V3Edmx);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
    }
107
108
    /**
109
     * Pluralizes a word if quantity is not one.
110
     *
111
     * @param int $quantity Number of items
112
     * @param string $singular Singular form of word
113
     * @param string $plural Plural form of word; function will attempt to deduce plural
114
     * form from singular if not provided
115
     * @return string Pluralized word if quantity is not one, otherwise singular
116
     */
117
    public static function pluralize($quantity, $singular, $plural = null)
118
    {
119
        if ($quantity == 1 || !strlen($singular)) {
120
            return $singular;
121
        }
122
        if ($plural !== null) {
123
            return $plural;
124
        }
125
126
        $last_letter = strtolower($singular[strlen($singular) - 1]);
127
        switch ($last_letter) {
128
            case 'y':
129
                return substr($singular, 0, -1) . 'ies';
130
            case 's':
131
                return $singular . 'es';
132
            default:
133
                return $singular . 's';
134
        }
135
    }
136
137
    private function revertEdmxTransaction()
138
    {
139
        //$this->V3Edmx = unserialize($this->oldEdmx);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
140
    }
141
142
    private function commitEdmxTransaction()
143
    {
144
        //$this->oldEdmx = null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
145
    }
146
147
    public function addPropertyToComplexType(
148
        \AlgoWeb\ODataMetadata\MetadataV3\edm\TComplexTypeType $complexType,
149
        $name,
150
        $type,
151
        $defaultValue = null,
152
        $nullable = false,
153
        $storeGeneratedPattern = null,
154
        $summary = null,
155
        $longDescription = null
156
    ) {
157
        $NewProperty = new TComplexTypePropertyType();
158
        $NewProperty->setName($name);
159
        $NewProperty->setType($type);
160
        $NewProperty->setStoreGeneratedPattern($storeGeneratedPattern);
161
        $NewProperty->setNullable($nullable);
162 View Code Duplication
        if (null != $summary || null != $longDescription) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
163
            $documentation = new TDocumentationType();
164
            $documentation->setSummary($summary);
165
            $documentation->setLongDescription($longDescription);
166
            $NewProperty->addToDocumentation($documentation);
167
        }
168
        if (null != $defaultValue) {
169
            $NewProperty->setDefaultValue($defaultValue);
170
        }
171
        $complexType->addToProperty($NewProperty);
172
        return $NewProperty;
173
    }
174
175
176
    public function addPropertyToEntityType(
177
        $entityType,
178
        $name,
179
        $type,
180
        $defaultValue = null,
181
        $nullable = false,
182
        $isKey = false,
183
        $storeGeneratedPattern = null,
184
        $summary = null,
185
        $longDescription = null
186
    ) {
187
        $this->startEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...:startEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
188
        $NewProperty = new TEntityPropertyType();
189
        $NewProperty->setName($name);
190
        $NewProperty->setType($type);
191
        $NewProperty->setStoreGeneratedPattern($storeGeneratedPattern);
192
        $NewProperty->setNullable($nullable);
193 View Code Duplication
        if (null != $summary || null != $longDescription) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
194
            $documentation = new TDocumentationType();
195
            $documentation->setSummary($summary);
196
            $documentation->setLongDescription($longDescription);
197
            $NewProperty->addToDocumentation($documentation);
198
        }
199
        if (null != $defaultValue) {
200
            $NewProperty->setDefaultValue($defaultValue);
201
        }
202
        $entityType->addToProperty($NewProperty);
203
        if ($isKey) {
204
            $Key = new TPropertyRefType();
205
            $Key->setName($name);
206
            $entityType->addToKey($Key);
207
        }
208
        if (!$this->V3Edmx->isok($this->lastError)) {
209
            $this->revertEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...revertEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
210
            return false;
211
        }
212
        $this->commitEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...commitEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
213
        return $NewProperty;
214
    }
215
216
    public function addNavigationPropertyToEntityType(
217
        TEntityTypeType $principalType,
218
        $principalMultiplicity,
219
        $principalProperty,
220
        TEntityTypeType $dependentType,
221
        $dependentMultiplicity,
222
        $dependentProperty = "",
223
        array $principalConstraintProperty = null,
224
        array $dependentConstraintProperty = null,
225
        $principalGetterAccess = "Public",
226
        $principalSetterAccess = "Public",
227
        $dependentGetterAccess = "Public",
228
        $dependentSetterAccess = "Public",
229
        $principalSummery = null,
230
        $principalLongDescription = null,
231
        $dependentSummery = null,
232
        $dependentLongDescription = null
233
    ) {
234
        $this->startEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...:startEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
235
        $principalEntitySetName = Str::plural($principalType->getName(), 2);
236
        $dependentEntitySetName = Str::plural($dependentType->getName(), 2);
237
        $relationName = $principalType->getName() . "_" . $principalProperty . "_"
238
                        . $dependentType->getName() . "_" . $dependentProperty;
239
        $relationName = trim($relationName, "_");
240
241
        $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace();
242
        if (0 == strlen(trim($namespace))) {
243
            $relationFQName = $relationName;
244
        } else {
245
            $relationFQName = $namespace . "." . $relationName;
246
        }
247
248
        $principalNavigationProperty = new TNavigationPropertyType();
249
        $principalNavigationProperty->setName($principalProperty);
250
        $principalNavigationProperty->setToRole(trim($dependentEntitySetName . "_" . $dependentProperty, "_"));
251
        $principalNavigationProperty->setFromRole($principalEntitySetName . "_" . $principalProperty);
252
        $principalNavigationProperty->setRelationship($relationFQName);
253
        $principalNavigationProperty->setGetterAccess($principalGetterAccess);
254
        $principalNavigationProperty->setSetterAccess($principalSetterAccess);
255
        if (null != $principalSummery || null != $principalLongDescription) {
256
            $principalDocumentation = new TDocumentationType();
257
            $principalDocumentation->setSummary($principalSummery);
258
            $principalDocumentation->setLongDescription($principalLongDescription);
259
            $principalNavigationProperty->setDocumentation($principalDocumentation);
260
        }
261
        $principalType->addToNavigationProperty($principalNavigationProperty);
262
        $dependentNavigationProperty = null;
263
        if (!empty($dependentProperty)) {
264
            $dependentNavigationProperty = new TNavigationPropertyType();
265
            $dependentNavigationProperty->setName($dependentProperty);
266
            $dependentNavigationProperty->setToRole($principalEntitySetName . "_" . $principalProperty);
267
            $dependentNavigationProperty->setFromRole($dependentEntitySetName . "_" . $dependentProperty);
268
            $dependentNavigationProperty->setRelationship($relationFQName);
269
            $dependentNavigationProperty->setGetterAccess($dependentGetterAccess);
270
            $dependentNavigationProperty->setSetterAccess($dependentSetterAccess);
271
            if (null != $dependentSummery || null != $dependentLongDescription) {
272
                $dependentDocumentation = new TDocumentationType();
273
                $dependentDocumentation->setSummary($dependentSummery);
274
                $dependentDocumentation->setLongDescription($dependentLongDescription);
275
                $dependentNavigationProperty->setDocumentation($dependentDocumentation);
276
            }
277
            $dependentType->addToNavigationProperty($dependentNavigationProperty);
278
        }
279
280
        $assocation = $this->createAssocationFromNavigationProperty(
281
            $principalType,
282
            $dependentType,
283
            $principalNavigationProperty,
284
            $dependentNavigationProperty,
285
            $principalMultiplicity,
286
            $dependentMultiplicity,
287
            $principalConstraintProperty,
288
            $dependentConstraintProperty
289
        );
290
291
        $this->V3Edmx->getDataServiceType()->getSchema()[0]->addToAssociation($assocation);
292
293
        $associationSet = $this->createAssocationSetForAssocation(
294
            $assocation,
295
            $principalEntitySetName,
296
            $dependentEntitySetName
297
        );
298
299
        $this->V3Edmx->getDataServiceType()->getSchema()[0]
300
            ->getEntityContainer()[0]->addToAssociationSet($associationSet);
301
302
        if (!$this->V3Edmx->isok($this->lastError)) {
303
            $this->revertEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...revertEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
304
            return false;
305
        }
306
        $this->commitEdmxTransaction();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...commitEdmxTransaction() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
307
        return [$principalNavigationProperty, $dependentNavigationProperty];
308
    }
309
310
    protected function createAssocationFromNavigationProperty(
311
        TEntityTypeType $principalType,
312
        TEntityTypeType $dependentType,
313
        TNavigationPropertyType $principalNavigationProperty,
314
        TNavigationPropertyType $dependentNavigationProperty = null,
315
        $principalMultiplicity,
316
        $dependentMultiplicity,
317
        array $principalConstraintProperty = null,
318
        array $dependentConstraintProperty = null
319
    ) {
320
        if (null != $dependentNavigationProperty) {
321
            if ($dependentNavigationProperty->getRelationship() != $principalNavigationProperty->getRelationship()) {
322
                $msg = "if you have both a dependent property and a principal property,"
323
                       ." they should both have the same relationship";
324
                throw new \Exception($msg);
325
            }
326
            if ($dependentNavigationProperty->getFromRole() != $principalNavigationProperty->getToRole() ||
327
                $dependentNavigationProperty->getToRole() != $principalNavigationProperty->getFromRole()
328
            ) {
329
                throw new \Exception("The from roles and two roles from matching properties should match");
330
            }
331
        }
332
        $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace();
333
334
        if (0 == strlen(trim($namespace))) {
335
            $principalTypeFQName = $principalType->getName();
336
            $dependentTypeFQName = $dependentType->getName();
337
        } else {
338
            $principalTypeFQName = $namespace . "." . $principalType->getName();
339
            $dependentTypeFQName = $namespace . "." . $dependentType->getName();
340
        }
341
        $association = new TAssociationType();
342
        $relationship = $principalNavigationProperty->getRelationship();
343
        if (strpos($relationship, '.') !== false) {
344
            $relationship = substr($relationship, strpos($relationship, '.') + 1);
345
        }
346
347
        $association->setName($relationship);
348
        $principalEnd = new TAssociationEndType();
349
        $principalEnd->setType($principalTypeFQName);
350
        $principalEnd->setRole($principalNavigationProperty->getFromRole());
351
        $principalEnd->setMultiplicity($principalMultiplicity);
352
        $association->addToEnd($principalEnd);
353
        $dependentEnd = new TAssociationEndType();
354
        $dependentEnd->setType($dependentTypeFQName);
355
        $dependentEnd->setMultiplicity($dependentMultiplicity);
356
        $association->addToEnd($dependentEnd);
357
358
        if (null != $dependentNavigationProperty) {
359
            $dependentEnd->setRole($dependentNavigationProperty->getFromRole());
360
        } else {
361
            $dependentEnd->setRole($principalNavigationProperty->getToRole());
362
        }
363
364
        $principalReferralConstraint = null;
365
        $dependentReferralConstraint = null;
366
367 View Code Duplication
        if (null != $principalConstraintProperty && 0 < count($principalConstraintProperty)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
368
            $principalReferralConstraint = new TReferentialConstraintRoleElementType();
369
            $principalReferralConstraint->setRole($principalNavigationProperty->getFromRole());
370
            foreach ($principalConstraintProperty as $propertyRef) {
371
                $TpropertyRef = new TPropertyRefType();
372
                $TpropertyRef->setName($propertyRef);
373
                $principalReferralConstraint->addToPropertyRef($TpropertyRef);
374
            }
375
        }
376 View Code Duplication
        if (null != $dependentConstraintProperty && 0 < count($dependentConstraintProperty)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
377
            $dependentReferralConstraint = new TReferentialConstraintRoleElementType();
378
            $dependentReferralConstraint->setRole($dependentNavigationProperty->getFromRole());
0 ignored issues
show
Bug introduced by
It seems like $dependentNavigationProperty is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
379
            foreach ($dependentConstraintProperty as $propertyRef) {
380
                $TpropertyRef = new TPropertyRefType();
381
                $TpropertyRef->setName($propertyRef);
382
                $dependentReferralConstraint->addToPropertyRef($TpropertyRef);
383
            }
384
        }
385
386
        if (null != $dependentReferralConstraint || null != $principalReferralConstraint) {
387
            $constraint = new TConstraintType();
388
            $constraint->setPrincipal($principalReferralConstraint);
0 ignored issues
show
Bug introduced by
It seems like $principalReferralConstraint defined by null on line 364 can be null; however, AlgoWeb\ODataMetadata\Me...intType::setPrincipal() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
389
            $constraint->setDependent($dependentReferralConstraint);
0 ignored issues
show
Bug introduced by
It seems like $dependentReferralConstraint defined by null on line 365 can be null; however, AlgoWeb\ODataMetadata\Me...intType::setDependent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
390
            $association->setReferentialConstraint($constraint);
391
        }
392
        return $association;
393
    }
394
395
    protected function createAssocationSetForAssocation(
396
        TAssociationType $association,
397
        $principalEntitySetName,
398
        $dependentEntitySetName
399
    ) {
400
        $as = new AssociationSetAnonymousType();
401
        $name = $association->getName();
402
        $as->setName($name);
403
        $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace();
404 View Code Duplication
        if (0 == strlen(trim($namespace))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
405
            $associationSetName = $association->getName();
406
        } else {
407
            $associationSetName = $namespace . "." . $association->getName();
408
        }
409
        $as->setAssociation($associationSetName);
410
        $end1 = new EndAnonymousType();
411
        $end1->setRole($association->getEnd()[0]->getRole());
412
        $end1->setEntitySet($principalEntitySetName);
413
        $end2 = new EndAnonymousType();
414
        $end2->setRole($association->getEnd()[1]->getRole());
415
        $end2->setEntitySet($dependentEntitySetName);
416
        $as->addToEnd($end1);
417
        $as->addToEnd($end2);
418
        return $as;
419
    }
420
421
    public function getLastError()
422
    {
423
        return $this->lastError;
424
    }
425
}
426