Completed
Pull Request — master (#95)
by Alex
04:34
created

MetadataManager   D

Complexity

Total Complexity 51

Size/Duplication

Total Lines 359
Duplicated Lines 15.32 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 51
c 5
b 0
f 1
lcom 1
cbo 18
dl 55
loc 359
rs 4.6563

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getEdmx() 0 6 1
A getEdmxXML() 0 4 1
B addEntityType() 11 32 5
A startEdmxTransaction() 0 4 1
B pluralize() 15 19 6
A revertEdmxTransaction() 0 4 1
A commitEdmxTransaction() 0 4 1
B addPropertyToEntityType() 6 39 6
C addNavigationPropertyToEntityType() 0 93 8
D createAssocationFromNavigationProperty() 18 84 16
B createAssocationSetForAssocation() 5 25 2
A getLastError() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like MetadataManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MetadataManager, and based on these observations, apply Extract Interface, too.

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\TConstraintType;
11
use AlgoWeb\ODataMetadata\MetadataV3\edm\TDocumentationType;
12
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType;
13
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType;
14
use AlgoWeb\ODataMetadata\MetadataV3\edm\TNavigationPropertyType;
15
use AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyRefType;
16
use AlgoWeb\ODataMetadata\MetadataV3\edm\TReferentialConstraintRoleElementType;
17
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx;
18
use JMS\Serializer\SerializerBuilder;
19
20
class MetadataManager
21
{
22
    private $V3Edmx = null;
23
    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...
24
    private $lastError = null;
25
    private $serializer = null;
26
27
    public function __construct($namespaceName = "Data", $containerName = "DefaultContainer")
28
    {
29
        $this->V3Edmx = new Edmx($namespaceName, $containerName);
30
        if (!$this->V3Edmx->isOK($msg)) {
31
            throw new \Exception($msg);
32
        }
33
        $ymlDir = __DIR__ . DIRECTORY_SEPARATOR . "MetadataV3" . DIRECTORY_SEPARATOR . "JMSmetadata";
34
        $this->serializer =
35
            SerializerBuilder::create()
36
                ->addMetadataDir($ymlDir)
37
                ->build();
38
    }
39
40
    public function getEdmx()
41
    {
42
        $msg = null;
43
        assert($this->V3Edmx->isOk($msg), $msg);
44
        return $this->V3Edmx;
45
    }
46
47
    public function getEdmxXML()
48
    {
49
        return $this->serializer->serialize($this->getEdmx(), "xml");
50
    }
51
52
    public function addEntityType($name, $accessType = "Public", $summary = null, $longDescription = null)
53
    {
54
        $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...
55
        $NewEntity = new TEntityTypeType();
56
        $NewEntity->setName($name);
57 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...
58
            $documentation = new TDocumentationType();
59
            $documentation->setSummary($summary);
60
            $documentation->setLongDescription($longDescription);
61
            $NewEntity->setDocumentation($documentation);
62
        }
63
64
        $entitySet = new EntitySetAnonymousType();
65
        $entitySet->setName($this->pluralize(2, $NewEntity->getName()));
66
        $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace();
67 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...
68
            $entityTypeName = $NewEntity->getName();
69
        } else {
70
            $entityTypeName = $namespace . "." . $NewEntity->getName();
71
        }
72
        $entitySet->setEntityType($entityTypeName);
73
        $entitySet->setGetterAccess($accessType);
74
75
        $this->V3Edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity);
76
        $this->V3Edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet);
77
        if (!$this->V3Edmx->isok($this->lastError)) {
78
            $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...
79
            return false;
80
        }
81
        $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...
82
        return $NewEntity;
83
    }
84
85
    private function startEdmxTransaction()
86
    {
87
        //$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...
88
    }
89
90
    /**
91
     * Pluralizes a word if quantity is not one.
92
     *
93
     * @param int $quantity Number of items
94
     * @param string $singular Singular form of word
95
     * @param string $plural Plural form of word; function will attempt to deduce plural
96
     * form from singular if not provided
97
     * @return string Pluralized word if quantity is not one, otherwise singular
98
     */
99 View Code Duplication
    public static function pluralize($quantity, $singular, $plural = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
100
    {
101
        if ($quantity == 1 || !strlen($singular)) {
102
            return $singular;
103
        }
104
        if ($plural !== null) {
105
            return $plural;
106
        }
107
108
        $last_letter = strtolower($singular[strlen($singular) - 1]);
109
        switch ($last_letter) {
110
            case 'y':
111
                return substr($singular, 0, -1) . 'ies';
112
            case 's':
113
                return $singular . 'es';
114
            default:
115
                return $singular . 's';
116
        }
117
    }
118
119
    private function revertEdmxTransaction()
120
    {
121
        //$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...
122
    }
123
124
    private function commitEdmxTransaction()
125
    {
126
        //$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...
127
    }
128
129
    public function addPropertyToEntityType(
130
        $entityType,
131
        $name,
132
        $type,
133
        $defaultValue = null,
134
        $nullable = false,
135
        $isKey = false,
136
        $storeGeneratedPattern = null,
137
        $summary = null,
138
        $longDescription = null
139
    ) {
140
        $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...
141
        $NewProperty = new TEntityPropertyType();
142
        $NewProperty->setName($name);
143
        $NewProperty->setType($type);
144
        $NewProperty->setStoreGeneratedPattern($storeGeneratedPattern);
145
        $NewProperty->setNullable($nullable);
146 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...
147
            $documentation = new TDocumentationType();
148
            $documentation->setSummary($summary);
149
            $documentation->setLongDescription($longDescription);
150
            $NewProperty->addToDocumentation($documentation);
151
        }
152
        if (null != $defaultValue) {
153
            $NewProperty->setDefaultValue($defaultValue);
154
        }
155
        $entityType->addToProperty($NewProperty);
156
        if ($isKey) {
157
            $Key = new TPropertyRefType();
158
            $Key->setName($name);
159
            $entityType->addToKey($Key);
160
        }
161
        if (!$this->V3Edmx->isok($this->lastError)) {
162
            $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...
163
            return false;
164
        }
165
        $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...
166
        return $NewProperty;
167
    }
168
169
    public function addNavigationPropertyToEntityType(
170
        TEntityTypeType $principalType,
171
        $principalMultiplicity,
172
        $principalProperty,
173
        TEntityTypeType $dependentType,
174
        $dependentMultiplicity,
175
        $dependentProperty = "",
176
        array $principalConstraintProperty = null,
177
        array $dependentConstraintProperty = null,
178
        $principalGetterAccess = "Public",
179
        $principalSetterAccess = "Public",
180
        $dependentGetterAccess = "Public",
181
        $dependentSetterAccess = "Public",
182
        $principalSummery = null,
183
        $principalLongDescription = null,
184
        $dependentSummery = null,
185
        $dependentLongDescription = 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
        $principalEntitySetName = $this->pluralize(2, $principalType->getName());
189
        $dependentEntitySetName = $this->pluralize(2, $dependentType->getName());
190
        $relationName = $principalType->getName() . "_" . $principalProperty . "_"
191
                        . $dependentType->getName() . "_" . $dependentProperty;
192
        $relationName = trim($relationName, "_");
193
194
        $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace();
195
        if (0 == strlen(trim($namespace))) {
196
            $relationFQName = $relationName;
197
        } else {
198
            $relationFQName = $namespace . "." . $relationName;
199
        }
200
201
        $principalNavigationProperty = new TNavigationPropertyType();
202
        $principalNavigationProperty->setName($principalProperty);
203
        $principalNavigationProperty->setToRole(trim($dependentEntitySetName . "_" . $dependentProperty, "_"));
204
        $principalNavigationProperty->setFromRole($principalEntitySetName . "_" . $principalProperty);
205
        $principalNavigationProperty->setRelationship($relationFQName);
206
        $principalNavigationProperty->setGetterAccess($principalGetterAccess);
207
        $principalNavigationProperty->setSetterAccess($principalSetterAccess);
208
        if (null != $principalSummery || null != $principalLongDescription) {
209
            $principalDocumentation = new TDocumentationType();
210
            $principalDocumentation->setSummary($principalSummery);
211
            $principalDocumentation->setLongDescription($principalLongDescription);
212
            $principalNavigationProperty->setDocumentation($principalDocumentation);
213
        }
214
        $principalType->addToNavigationProperty($principalNavigationProperty);
215
        $dependentNavigationProperty = null;
216
        if (!empty($dependentProperty)) {
217
            $dependentNavigationProperty = new TNavigationPropertyType();
218
            $dependentNavigationProperty->setName($dependentProperty);
219
            $dependentNavigationProperty->setToRole($principalEntitySetName . "_" . $principalProperty);
220
            $dependentNavigationProperty->setFromRole($dependentEntitySetName . "_" . $dependentProperty);
221
            $dependentNavigationProperty->setRelationship($relationFQName);
222
            $dependentNavigationProperty->setGetterAccess($dependentGetterAccess);
223
            $dependentNavigationProperty->setSetterAccess($dependentSetterAccess);
224
            if (null != $dependentSummery || null != $dependentLongDescription) {
225
                $dependentDocumentation = new TDocumentationType();
226
                $dependentDocumentation->setSummary($dependentSummery);
227
                $dependentDocumentation->setLongDescription($dependentLongDescription);
228
                $dependentNavigationProperty->setDocumentation($dependentDocumentation);
229
            }
230
            $dependentType->addToNavigationProperty($dependentNavigationProperty);
231
        }
232
233
        $assocation = $this->createAssocationFromNavigationProperty(
234
            $principalType,
235
            $dependentType,
236
            $principalNavigationProperty,
237
            $dependentNavigationProperty,
238
            $principalMultiplicity,
239
            $dependentMultiplicity,
240
            $principalConstraintProperty,
241
            $dependentConstraintProperty
242
        );
243
244
        $this->V3Edmx->getDataServiceType()->getSchema()[0]->addToAssociation($assocation);
245
246
        $associationSet = $this->createAssocationSetForAssocation(
247
            $assocation,
248
            $principalEntitySetName,
249
            $dependentEntitySetName
250
        );
251
252
        $this->V3Edmx->getDataServiceType()->getSchema()[0]
253
            ->getEntityContainer()[0]->addToAssociationSet($associationSet);
254
255
        if (!$this->V3Edmx->isok($this->lastError)) {
256
            $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...
257
            return false;
258
        }
259
        $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...
260
        return [$principalNavigationProperty, $dependentNavigationProperty];
261
    }
262
263
    protected function createAssocationFromNavigationProperty(
264
        TEntityTypeType $principalType,
265
        TEntityTypeType $dependentType,
266
        TNavigationPropertyType $principalNavigationProperty,
267
        TNavigationPropertyType $dependentNavigationProperty = null,
268
        $principalMultiplicity,
269
        $dependentMultiplicity,
270
        array $principalConstraintProperty = null,
271
        array $dependentConstraintProperty = null
272
    ) {
273
        if (null != $dependentNavigationProperty) {
274
            if ($dependentNavigationProperty->getRelationship() != $principalNavigationProperty->getRelationship()) {
275
                $msg = "if you have both a dependent property and a principal property,"
276
                       ." they should both have the same relationship";
277
                throw new \Exception($msg);
278
            }
279
            if ($dependentNavigationProperty->getFromRole() != $principalNavigationProperty->getToRole() ||
280
                $dependentNavigationProperty->getToRole() != $principalNavigationProperty->getFromRole()
281
            ) {
282
                throw new \Exception("The from roles and two roles from matching properties should match");
283
            }
284
        }
285
        $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace();
286
287
        if (0 == strlen(trim($namespace))) {
288
            $principalTypeFQName = $principalType->getName();
289
            $dependentTypeFQName = $dependentType->getName();
290
        } else {
291
            $principalTypeFQName = $namespace . "." . $principalType->getName();
292
            $dependentTypeFQName = $namespace . "." . $dependentType->getName();
293
        }
294
        $association = new TAssociationType();
295
        $relationship = $principalNavigationProperty->getRelationship();
296
        if (strpos($relationship, '.') !== false) {
297
            $relationship = substr($relationship, strpos($relationship, '.') + 1);
298
        }
299
300
        $association->setName($relationship);
301
        $principalEnd = new TAssociationEndType();
302
        $principalEnd->setType($principalTypeFQName);
303
        $principalEnd->setRole($principalNavigationProperty->getFromRole());
304
        $principalEnd->setMultiplicity($principalMultiplicity);
305
        $association->addToEnd($principalEnd);
306
        $dependentEnd = new TAssociationEndType();
307
        $dependentEnd->setType($dependentTypeFQName);
308
        $dependentEnd->setMultiplicity($dependentMultiplicity);
309
        $association->addToEnd($dependentEnd);
310
311
        if (null != $dependentNavigationProperty) {
312
            $dependentEnd->setRole($dependentNavigationProperty->getFromRole());
313
        } else {
314
            $dependentEnd->setRole($principalNavigationProperty->getToRole());
315
        }
316
317
        $principalReferralConstraint = null;
318
        $dependentReferralConstraint = null;
319
320 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...
321
            $principalReferralConstraint = new TReferentialConstraintRoleElementType();
322
            $principalReferralConstraint->setRole($principalNavigationProperty->getFromRole());
323
            foreach ($principalConstraintProperty as $propertyRef) {
324
                $TpropertyRef = new TPropertyRefType();
325
                $TpropertyRef->setName($propertyRef);
326
                $principalReferralConstraint->addToPropertyRef($TpropertyRef);
327
            }
328
        }
329 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...
330
            $dependentReferralConstraint = new TReferentialConstraintRoleElementType();
331
            $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...
332
            foreach ($dependentConstraintProperty as $propertyRef) {
333
                $TpropertyRef = new TPropertyRefType();
334
                $TpropertyRef->setName($propertyRef);
335
                $dependentReferralConstraint->addToPropertyRef($TpropertyRef);
336
            }
337
        }
338
339
        if (null != $dependentReferralConstraint || null != $principalReferralConstraint) {
340
            $constraint = new TConstraintType();
341
            $constraint->setPrincipal($principalReferralConstraint);
0 ignored issues
show
Bug introduced by
It seems like $principalReferralConstraint defined by null on line 317 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...
342
            $constraint->setDependent($dependentReferralConstraint);
0 ignored issues
show
Bug introduced by
It seems like $dependentReferralConstraint defined by null on line 318 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...
343
            $association->setReferentialConstraint($constraint);
344
        }
345
        return $association;
346
    }
347
348
    protected function createAssocationSetForAssocation(
349
        TAssociationType $association,
350
        $principalEntitySetName,
351
        $dependentEntitySetName
352
    ) {
353
        $as = new AssociationSetAnonymousType();
354
        $name = $association->getName();
355
        $as->setName($name);
356
        $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace();
357 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...
358
            $associationSetName = $association->getName();
359
        } else {
360
            $associationSetName = $namespace . "." . $association->getName();
361
        }
362
        $as->setAssociation($associationSetName);
363
        $end1 = new EndAnonymousType();
364
        $end1->setRole($association->getEnd()[0]->getRole());
365
        $end1->setEntitySet($principalEntitySetName);
366
        $end2 = new EndAnonymousType();
367
        $end2->setRole($association->getEnd()[1]->getRole());
368
        $end2->setEntitySet($dependentEntitySetName);
369
        $as->addToEnd($end1);
370
        $as->addToEnd($end2);
371
        return $as;
372
    }
373
374
    public function getLastError()
375
    {
376
        return $this->lastError;
377
    }
378
}
379