Test Failed
Push — master ( 460acf...3c9528 )
by Christopher
04:41
created

TSchemaType::isStructureOK()   C

Complexity

Conditions 11
Paths 148

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 55
rs 6.0909
c 2
b 0
f 0
cc 11
eloc 33
nc 148
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
        $this->getEntityType();
0 ignored issues
show
Unused Code introduced by
The call to the method AlgoWeb\ODataMetadata\Me...maType::getEntityType() 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...
118
        foreach ($this->getEntityType() as $entityType) {
119
            $entityTypeNames[] = $entityType->getName();
120
        }
121
        foreach ($this->association as $association) {
122
            $associationNames[] = $association->getName();
123
        }
124
125
        $entitySets = $this->getEntityContainer()[0]->getEntitySet();
126
        foreach ($entitySets as $eset) {
127
            $eSetType = $eset->getEntityType();
128
            /*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...
129
                $msg = "Types for Entity Sets should have the namespace at the beginning " . __CLASS__;
130
                die($this->getNamespace());
131
                return false;
132
            }*/
133
            $eSetType = str_replace($this->getNamespace() . ".", "", $eSetType);
134
            if (!in_array($eSetType, $entityTypeNames)) {
135
                $msg = "entitySet Types should have a matching type name in entity Types";
136
                return false;
137
            }
138
        }
139
140
        // Check Associations to assocationSets
141
        $found = false;
142
        $namespaceLen = strlen($this->namespace);
143
        if (0 != $namespaceLen) {
144
            $namespaceLen++;
145
        }
146
        foreach ($this->association as $assocation) {
147
            foreach ($this->entityContainer->associationSet as $assocationSet) {
148
                if ($assocation->getName() == substr($assocationSet->association, $namespaceLen)) {
149
                    $aEnd1 = $assocationSet->getEnd()[0];
150
                    $aEnd2 = $assocationSet->getEnd()[1];
151
                    $asEnd1 = $assocationSet->getEnd()[0];
152
                    $asEnd2 = $assocationSet->getEnd()[1];
153
                    //== TRUE if $a and $b have the same key/value pairs.
154
                    //===TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
155
                    if ([$aEnd1->getRole(), $aEnd2->getRole()] == [$asEnd1->getRole(), $asEnd2->getRole()]) {
156
                        $found = true;
157
                    }
158
                }
159
            }
160
            if (!$found) {
161
                $msg = "can not find assocationset with matching roles for assocation";
162
                return false;
163
            }
164
            $found = false;
165
        }
166
        return true;
167
    }
168
169
    /**
170
     * Gets as namespace
171
     *
172
     * @return string
173
     */
174
    public function getNamespace()
175
    {
176
        return $this->namespace;
177
    }
178
179
    /**
180
     * Sets a new namespace
181
     *
182
     * @param string $namespace
183
     * @return self
184
     */
185
    public function setNamespace($namespace)
186
    {
187
        if (!$this->isTNamespaceNameValid($namespace)) {
188
            $msg = "Namespace must be a valid TNamespaceName";
189
            throw new \InvalidArgumentException($msg);
190
        }
191
        $this->namespace = $namespace;
192
        return $this;
193
    }
194
}
195