Test Failed
Pull Request — master (#92)
by Christopher
04:40
created

TSchemaType::isOK()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 24
Code Lines 15

Duplication

Lines 4
Ratio 16.67 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
loc 24
rs 5.7377
cc 8
eloc 15
nc 10
nop 1
1
<?php
2
3
namespace AlgoWeb\ODataMetadata\MetadataV3\edm;
4
5
use AlgoWeb\ODataMetadata\IsOK;
6
use AlgoWeb\ODataMetadata\MetadataV3\edm\Groups\GSchemaBodyElementsTrait;
7
use AlgoWeb\ODataMetadata\MetadataV3\edm\IsOKTraits\TNamespaceNameTrait;
8
use AlgoWeb\ODataMetadata\MetadataV3\edm\IsOKTraits\TSimpleIdentifierTrait;
9
use AlgoWeb\ODataMetadata\StringTraits\XSDTopLevelTrait;
10
11
/**
12
 * Class representing TSchemaType
13
 *
14
 *
15
 * XSD Type: TSchema
16
 */
17
class TSchemaType extends IsOK
18
{
19
    use GSchemaBodyElementsTrait, TSimpleIdentifierTrait, XSDTopLevelTrait, TNamespaceNameTrait {
20
        TSimpleIdentifierTrait::isNCName insteadof TNamespaceNameTrait;
21
        TSimpleIdentifierTrait::matchesRegexPattern insteadof TNamespaceNameTrait;
22
        TSimpleIdentifierTrait::isName insteadof TNamespaceNameTrait;
23
    }
24
    /**
25
     * @property string $namespace
26
     */
27
    private $namespace = null;
28
29
    /**
30
     * @property string $namespaceUri
31
     */
32
    private $namespaceUri = null;
33
34
    /**
35
     * @property string $alias
36
     */
37
    private $alias = null;
38
39
    /**
40
     * Gets as namespaceUri
41
     *
42
     * @return string
43
     */
44
    public function getNamespaceUri()
45
    {
46
        return $this->namespaceUri;
47
    }
48
49
    /**
50
     * Sets a new namespaceUri
51
     *
52
     * @param string $namespaceUri
53
     * @return self
54
     */
55
    public function setNamespaceUri($namespaceUri)
56
    {
57
        if (null != $namespaceUri && !$this->isURLValid($namespaceUri)) {
58
            $msg = "Namespace url must be a valid url";
59
            throw new \InvalidArgumentException($msg);
60
        }
61
        $this->namespaceUri = $namespaceUri;
62
        return $this;
63
    }
64
65
    /**
66
     * Gets as alias
67
     *
68
     * @return string
69
     */
70
    public function getAlias()
71
    {
72
        return $this->alias;
73
    }
74
75
    /**
76
     * Sets a new alias
77
     *
78
     * @param string $alias
79
     * @return self
80
     */
81
    public function setAlias($alias)
82
    {
83
        if (null != $alias && !$this->isTSimpleIdentifierValid($alias)) {
84
            $msg = "Alias must be a valid TSimpleIdentifier";
85
            throw new \InvalidArgumentException($msg);
86
        }
87
        $this->alias = $alias;
88
        return $this;
89
    }
90
91
    public function isOK(&$msg = null)
92
    {
93
        $entityCount = max(1, count($this->entityType));
94
        // done this way to enable die-roll to be mocked - don't know how to mock rand() directly
95
        if (1 < $entityCount * $this->getRand()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
96
        //    return true;
97
        }
98
        if (!$this->isTNamespaceNameValid($this->namespace)) {
99
            $msg = "Namespace must be a valid TNamespaceName";
100
            return false;
101
        }
102
        if (null != $this->namespaceUri && !$this->isURLValid($this->namespaceUri)) {
103
            $msg = "Namespace url must be a valid url";
104
            return false;
105
        }
106 View Code Duplication
        if (null != $this->alias && !$this->isTSimpleIdentifierValid($this->alias)) {
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...
107
            $msg = "Alias must be a valid TSimpleIdentifier";
108
            return false;
109
        }
110
        if (!$this->isGSchemaBodyElementsValid($msg)) {
111
            return false;
112
        }
113
        return $this->isStructureOK($msg);
114
    }
115
116
    public function isStructureOK(&$msg = null)
117
    {
118
        $entityTypeNames = [];
119
        $associationNames = [];
120
        $navigationProperties = [];
121
        $associationSets = [];
122
        $namespaceLen = strlen($this->namespace);
123
        if (0 != $namespaceLen) {
124
            $namespaceLen++;
125
        }
126
127
        foreach ($this->getEntityType() as $entityType) {
128
            $entityTypeNames[] = $entityType->getName();
129
            foreach ($entityType->getNavigationProperty() as $navigationProperty) {
130
                $navigationProperties[
131
                substr($navigationProperty->getRelationship(), $namespaceLen)
132
                ][] = $navigationProperty;
133
            }
134
        }
135
136
        foreach ($this->association as $association) {
137
            $associationNames[$association->getName()] = $association->getEnd();
138
        }
139
        foreach ($this->getEntityContainer()[0]->getAssociationSet() as $associationSet) {
140
            $associationSets[substr($associationSet->getAssociation(), $namespaceLen)] = $associationSet->getEnd();
141
        }
142
143
144
        $entitySets = $this->getEntityContainer()[0]->getEntitySet();
145
        foreach ($entitySets as $eset) {
146
            $eSetType = $eset->getEntityType();
147
            /*if (substr($eSetType, 0, strlen($this->getNamespace())) != $this->getNamespace()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
148
                $msg = "Types for Entity Sets should have the namespace at the beginning " . __CLASS__;
149
                die($this->getNamespace());
150
                return false;
151
            }*/
152
            $eSetType = str_replace($this->getNamespace() . ".", "", $eSetType);
153
            if (!in_array($eSetType, $entityTypeNames)) {
154
                $msg = "entitySet Types should have a matching type name in entity Types";
155
                return false;
156
            }
157
        }
158
159
        // Check Associations to associationSets
160
        if (count($associationSets) != count($associationNames)) {
161
            $msg = "we have " . count($associationSets) . "association sets and " . count($associationNames)
162
                   . " associations, they should be the same";
163
        }
164
        if (count($associationNames) * 2 < count($navigationProperties)) {
165
            $msg = "we have too many navigation properties. should have no more then double the"
166
                   ." number of associations.";
167
        }
168
        foreach ($associationNames as $associationName => $associationEnds) {
169
            if (!array_key_exists($associationName, $associationSets)) {
170
                $msg = "association " . $associationName . " exists without matching associationSet";
171
                return false;
172
            }
173
174
            if (!array_key_exists($associationName, $navigationProperties)) {
175
                $msg = "association " . $associationName . " exists without matching Natvigation Property";
176
                return false;
177
            }
178
            $roles = [$associationEnds[0]->getRole(), $associationEnds[1]->getRole()];
179 View Code Duplication
            if (!in_array($associationSets[$associationName][0]->getRole(), $roles)) {
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...
180
                $msg = "association Set role " . $associationSets[$associationName][0]->getRole()
181
                       . "lacks a matching property in the attached association";
182
                return false;
183
            }
184 View Code Duplication
            if (!in_array($associationSets[$associationName][1]->getRole(), $roles)) {
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...
185
                $msg = "association Set role " . $associationSets[$associationName][1]->getRole()
186
                       . "lacks a matching property in the attached association";
187
                return false;
188
            }
189
            foreach($navigationProperties[$associationName] as $navProp){
190 View Code Duplication
                if (!in_array($navProp->getToRole(),$roles)) {
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...
191
                    $msg = "Navigation Property Role " . $navProp->getToRole()
192
                         . " lacks a matching peroperty in the assocation";
193
                    return false;
194
                }
195 View Code Duplication
                if (!in_array($navProp->getFromRole(),$roles)) {
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...
196
                    $msg = "Navigation Property Role " .$navProp->getToRole()
197
                         . " lacks a matching peroperty in the assocation";
198
                    return false;
199
                }
200
            }
201
        }
202
        return true;
203
    }
204
205
    /**
206
     * Gets as namespace
207
     *
208
     * @return string
209
     */
210
    public function getNamespace()
211
    {
212
        return $this->namespace;
213
    }
214
215
    /**
216
     * Sets a new namespace
217
     *
218
     * @param string $namespace
219
     * @return self
220
     */
221
    public function setNamespace($namespace)
222
    {
223
        if (!$this->isTNamespaceNameValid($namespace)) {
224
            $msg = "Namespace must be a valid TNamespaceName";
225
            throw new \InvalidArgumentException($msg);
226
        }
227
        $this->namespace = $namespace;
228
        return $this;
229
    }
230
231
    protected function getRand()
232
    {
233
        return rand();
234
    }
235
}
236