Test Failed
Push — master ( 785c25...2b7dc4 )
by Christopher
04:16
created

TSchemaType::isStructureOK()   F

Complexity

Conditions 15
Paths 1176

Size

Total Lines 70
Code Lines 41

Duplication

Lines 8
Ratio 11.43 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 8
loc 70
rs 2.4836
cc 15
eloc 41
nc 1176
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    }
26
    /**
27
     * @property string $namespace
28
     */
29
    private $namespace = null;
30
31
    /**
32
     * @property string $namespaceUri
33
     */
34
    private $namespaceUri = null;
35
36
    /**
37
     * @property string $alias
38
     */
39
    private $alias = null;
40
41
    /**
42
     * Gets as namespaceUri
43
     *
44
     * @return string
45
     */
46
    public function getNamespaceUri()
47
    {
48
        return $this->namespaceUri;
49
    }
50
51
    /**
52
     * Sets a new namespaceUri
53
     *
54
     * @param string $namespaceUri
55
     * @return self
56
     */
57
    public function setNamespaceUri($namespaceUri)
58
    {
59
        if (null != $namespaceUri && !$this->isURLValid($namespaceUri)) {
60
            $msg = "Namespace url must be a valid url";
61
            throw new \InvalidArgumentException($msg);
62
        }
63
        $this->namespaceUri = $namespaceUri;
64
        return $this;
65
    }
66
67
    /**
68
     * Gets as alias
69
     *
70
     * @return string
71
     */
72
    public function getAlias()
73
    {
74
        return $this->alias;
75
    }
76
77
    /**
78
     * Sets a new alias
79
     *
80
     * @param string $alias
81
     * @return self
82
     */
83
    public function setAlias($alias)
84
    {
85
        if (null != $alias && !$this->isTSimpleIdentifierValid($alias)) {
86
            $msg = "Alias must be a valid TSimpleIdentifier";
87
            throw new \InvalidArgumentException($msg);
88
        }
89
        $this->alias = $alias;
90
        return $this;
91
    }
92
93
    public function isOK(&$msg = null)
94
    {
95
        if (!$this->isTNamespaceNameValid($this->namespace)) {
96
            $msg = "Namespace must be a valid TNamespaceName";
97
            return false;
98
        }
99
        if (null != $this->namespaceUri && !$this->isURLValid($this->namespaceUri)) {
100
            $msg = "Namespace url must be a valid url";
101
            return false;
102
        }
103 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...
104
            $msg = "Alias must be a valid TSimpleIdentifier";
105
            return false;
106
        }
107
        if (!$this->isGSchemaBodyElementsValid($msg)) {
108
            return false;
109
        }
110
        return $this->isStructureOK($msg);
111
    }
112
113
    public function isStructureOK(&$msg = null)
114
    {
115
        $entityTypeNames = [];
116
        $associationNames = [];
117
        $navigationProperties = [];
118
        $assocationSets = [];
119
        $namespaceLen = strlen($this->namespace);
120
        if (0 != $namespaceLen) {
121
            $namespaceLen++;
122
        }
123
124
        foreach ($this->getEntityType() as $entityType) {
125
            $entityTypeNames[] = $entityType->getName();
126
            foreach ($entityType->getNavigationProperty() as $navigationProperty) {
127
                $navigationProperties[substr($navigationProperty->getRelationship(), $namespaceLen)][] = $navigationProperty;
128
            }
129
        }
130
131
        foreach ($this->association as $association) {
132
            $associationNames[$association->getName()] = $association->getEnd();
133
        }
134
        foreach ($this->getEntityContainer()[0]->getAssociationSet as $assocationSet) {
0 ignored issues
show
Bug introduced by
The property getAssociationSet does not seem to exist. Did you mean associationSet?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
135
            $assocationSets[substr($assocationSet->getAssociation(), $namespaceLen)] = $assocationSet->getEnd();
136
        }
137
138
139
        $entitySets = $this->getEntityContainer()[0]->getEntitySet();
140
        foreach ($entitySets as $eset) {
141
            $eSetType = $eset->getEntityType();
142
            /*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...
143
                $msg = "Types for Entity Sets should have the namespace at the beginning " . __CLASS__;
144
                die($this->getNamespace());
145
                return false;
146
            }*/
147
            $eSetType = str_replace($this->getNamespace() . ".", "", $eSetType);
148
            if (!in_array($eSetType, $entityTypeNames)) {
149
                $msg = "entitySet Types should have a matching type name in entity Types";
150
                return false;
151
            }
152
        }
153
154
        // Check Associations to assocationSets
155
        if (count($assocationSets) != count($associationNames)) {
156
            $msg = "we have " . count($assocationSets) . "assocation sets and " . count($associationNames) . " Assocations, they should be the same";
157
        }
158
        if (count($associationNames) * 2 < $navigationProperties) {
159
            $msg = "we have two many navigation propertys. should have no more then double the number of assocations.";
160
        }
161
162
        foreach ($associationNames as $assocationName => $assocationEnds) {
163
            if (!array_key_exists($assocationName, $assocationSets)) {
164
                $msg = "assocation " . $assocationName . " exists without matching assocationSet";
165
                return false;
166
            }
167
168
            if (!array_key_exists($assocationName, $navigationProperties)) {
169
                $msg = "assocation " . $assocationName . " exists without matching Natvigation Property";
170
                return false;
171
            }
172 View Code Duplication
            if (!in_array($assocationSets[$assocationName][0]->getName, [$assocationEnds[0]->getName, $assocationEnds[1]->getName])) {
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...
173
                $msg = "assocation Set role " . $assocationSets[$assocationName][0]->getName . "lacks a matching property in the attached assocation";
174
                return false;
175
            }
176 View Code Duplication
            if (!in_array($assocationSets[$assocationName][1]->getName, [$assocationEnds[0]->getName, $assocationEnds[1]->getName])) {
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...
177
                $msg = "assocation Set role " . $assocationSets[$assocationName][0]->getName . "lacks a matching property in the attached assocation";
178
                return false;
179
            }
180
        }
181
        return true;
182
    }
183
184
    /**
185
     * Gets as namespace
186
     *
187
     * @return string
188
     */
189
    public function getNamespace()
190
    {
191
        return $this->namespace;
192
    }
193
194
    /**
195
     * Sets a new namespace
196
     *
197
     * @param string $namespace
198
     * @return self
199
     */
200
    public function setNamespace($namespace)
201
    {
202
        if (!$this->isTNamespaceNameValid($namespace)) {
203
            $msg = "Namespace must be a valid TNamespaceName";
204
            throw new \InvalidArgumentException($msg);
205
        }
206
        $this->namespace = $namespace;
207
        return $this;
208
    }
209
}
210