Completed
Push — master ( 1857e6...d96bec )
by Joao
03:02
created

ClassAnnotations   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 222
Duplicated Lines 7.21 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 43
c 4
b 2
f 0
lcom 2
cbo 2
dl 16
loc 222
rs 8.3157

18 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 28 5
A getPropertyList() 0 17 3
A getPropertyAnnotation() 0 9 3
A getClassInstance() 0 4 1
A getClassRefl() 0 4 1
A getClassName() 0 10 3
A getClassGetter() 0 7 2
A getClassPropertyPattern() 0 12 3
A getClassWriteEmpty() 0 7 2
A getClassDocType() 0 7 2
A getClassRdfType() 8 8 2
A getClassRdfAbout() 8 8 2
A getClassDefaultPrefix() 0 7 2
A getClassIsRDF() 0 7 2
A getClassIgnoreAllClass() 0 7 2
B getClassNamespace() 0 21 5
A getClassDontCreateClassNode() 0 7 2
A setClassRefl() 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 ClassAnnotations 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 ClassAnnotations, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace ByJG\AnyDataset\Model;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
use stdClass;
8
9
class ClassAnnotations extends Annotations
10
{
11
    protected $classRefl;
12
    protected $className;
13
    protected $classGetter;
14
    protected $classPropertyPattern;
15
    protected $classWriteEmpty;
16
    protected $classDocType;
17
    protected $classRdfType;
18
    protected $classRdfAbout;
19
    protected $classDefaultPrefix;
20
    protected $classIsRDF;
21
    protected $classIgnoreAllClass;
22
    protected $classNamespace;
23
    protected $classDontCreateClassNode;
24
25
    protected $propertyList;
26
    protected $propertyAnnotations = [];
27
28
    protected $model;
29
    protected $forcePropName;
30
31
    public function __construct($model, $config = "object", $forcePropName = "")
32
    {
33
        $this->model = $model;
34
        $this->config = $config;
35
        $this->forcePropName = $forcePropName;
36
37
        $this->setClassRefl(new \ReflectionClass(new \stdClass));
38
        $this->annotations = [];
39
40
        if (is_array($model)) {
41
            $this->model = new stdClass();
42
            foreach ($model as $key => $value) {
43
                if (is_numeric($key)) {
44
                    $key = "__{$key}__";
45
                }
46
                $this->model->{$key} = $value;
47
            }
48
        }
49
50
        if (!$this->model instanceof stdClass) {
51
            $class = new ReflectionClass($this->model);
52
53
            $aux = null;
54
            preg_match_all('/@(?P<param>\S+)\s*(?P<value>\S+)?\r?\n/', $class->getDocComment(), $aux);
55
            $this->annotations = $this->adjustParams($aux);
56
            $this->setClassRefl($class);
57
        }
58
    }
59
60
    public function getPropertyList()
61
    {
62
        if (!$this->propertyList) {
63
            $this->propertyList = [];
64
            if ($this->model instanceof stdClass) {
65
                $this->propertyList = get_object_vars($this->model);
66
            } else {
67
                $this->propertyList = $this->getClassRefl()->getProperties(
68
                    ReflectionProperty::IS_PROTECTED |
69
                    ReflectionProperty::IS_PRIVATE   |
70
                    ReflectionProperty::IS_PUBLIC
71
                );
72
            }
73
        }
74
75
        return $this->propertyList;
76
    }
77
78
    /**
79
     *
80
     * @param ReflectionProperty $property
81
     * @param string $keyProp
82
     * @return PropertyAnnotations
83
     */
84
    public function getPropertyAnnotation($property, $keyProp = null)
85
    {
86
        $propName = ($property instanceof \ReflectionProperty) ? $property->getName() : $keyProp;
87
        if (!isset($this->propertyAnnotations[$propName])) {
88
            $this->propertyAnnotations[$propName] = new PropertyAnnotations($this, $this->config, $property, $propName);
89
        }
90
91
        return $this->propertyAnnotations[$propName];
92
    }
93
94
    public function getClassInstance()
95
    {
96
        return $this->model;
97
    }
98
99
    public function getClassRefl()
100
    {
101
        return $this->classRefl;
102
    }
103
104
    public function getClassName()
105
    {
106
        if (!$this->className) {
107
            $this->className = $this->forcePropName;
108
            if (empty($this->className)) {
109
                $this->className = $this->getAnnotations("nodename", get_class($this->model));
110
            }
111
        }
112
        return $this->className;
113
    }
114
115
    public function getClassGetter()
116
    {
117
        if (!$this->classGetter) {
118
            $this->classGetter = $this->getAnnotations("getter", "get");
119
        }
120
        return $this->classGetter;
121
    }
122
123
    public function getClassPropertyPattern($id = null)
124
    {
125
        if (!$this->classPropertyPattern) {
126
            $this->classPropertyPattern = $this->getAnnotations("propertypattern", ['/([^a-zA-Z0-9])/', '']);
127
        }
128
129
        if (!is_null($id)) {
130
            return $this->classPropertyPattern[$id];
131
        }
132
133
        return $this->classPropertyPattern;
134
    }
135
136
    public function getClassWriteEmpty()
137
    {
138
        if (!$this->classWriteEmpty) {
139
            $this->classWriteEmpty = $this->getAnnotations("writeempty", false);
140
        }
141
        return $this->classWriteEmpty;
142
    }
143
144
    public function getClassDocType()
145
    {
146
        if (!$this->classDocType) {
147
            $this->classDocType = $this->getAnnotations("doctype", "xml");
148
        }
149
        return $this->classDocType;
150
    }
151
152 View Code Duplication
    public function getClassRdfType()
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...
153
    {
154
        if (!$this->classRdfType) {
155
            $rdfType = $this->getAnnotations("rdftype", "{HOST}/rdf/class/{CLASS}");
156
            $this->classRdfType = $this->replaceVars($this->getClassName(), $rdfType);
157
        }
158
        return $this->classRdfType;
159
    }
160
161 View Code Duplication
    public function getClassRdfAbout()
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...
162
    {
163
        if (!$this->classRdfAbout) {
164
            $rdfAbout = $this->getAnnotations("rdfabout", "{HOST}/rdf/instance/{CLASS}/{GetID()}");
165
            $this->classRdfAbout = $this->replaceVars($this->getClassName(), $rdfAbout);
166
        }
167
        return $this->classRdfAbout;
168
    }
169
170
    public function getClassDefaultPrefix()
171
    {
172
        if (!$this->classDefaultPrefix) {
173
            $this->classDefaultPrefix = $this->getAnnotations("defaultprefix", "");
174
        }
175
        return $this->classDefaultPrefix;
176
    }
177
178
    public function getClassIsRDF()
179
    {
180
        if (!$this->classIsRDF) {
181
            $this->classIsRDF = ($this->getClassDocType() == "rdf");
182
        }
183
        return $this->classIsRDF;
184
    }
185
186
    public function getClassIgnoreAllClass()
187
    {
188
        if (!$this->classIgnoreAllClass) {
189
            $this->classIgnoreAllClass = $this->getAnnotations("ignore", false);
190
        }
191
        return $this->classIgnoreAllClass;
192
    }
193
194
    public function getClassNamespace()
195
    {
196
        if (!$this->classNamespace) {
197
            $this->classNamespace = $this->getAnnotations("namespace", []);
198
            if (!is_array($this->classNamespace) && !empty($this->classNamespace)) {
199
                $this->classNamespace = [ $this->classNamespace];
200
            }
201
202
            foreach ($this->classNamespace as $key => $value) {
203
                $prefix = strtok($value, "!");
204
                $uri = str_replace($prefix . "!", "", $value);
205
206
                $this->classNamespace[$key] = [
207
                    $this->classNamespace[$key],
208
                    "prefix" => $prefix,
209
                    "uri" => $this->replaceVars($this->classNamespace[$key], $uri)
210
                ];
211
            }
212
        }
213
        return $this->classNamespace;
214
    }
215
216
    public function getClassDontCreateClassNode()
217
    {
218
        if (!$this->classDontCreateClassNode) {
219
            $this->classDontCreateClassNode = $this->getAnnotations("dontcreatenode", false);
220
        }
221
        return $this->classDontCreateClassNode;
222
    }
223
224
225
    public function setClassRefl($classRefl)
226
    {
227
        $this->classRefl = $classRefl;
228
    }
229
230
}
231