Test Setup Failed
Push — master ( a3b2d5...39ac68 )
by Php Easy Api
04:18 queued 13s
created

Generator   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 75
c 1
b 1
f 0
dl 0
loc 261
rs 8.96
wmc 43

13 Methods

Rating   Name   Duplication   Size   Complexity  
A createClassTrait() 0 5 3
B createClassProperty() 0 21 7
A createMethod() 0 8 3
A createMethodAccessibleProperty() 0 6 2
A createMethodDocument() 0 5 2
A createClass() 0 12 3
A createClassImplements() 0 16 3
B createClassExtend() 0 24 8
A createMethodBody() 0 5 2
A createMethodParameters() 0 6 2
A createClassDocument() 0 5 3
A createClassUse() 0 5 3
A createClassPropertyDocument() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like Generator 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.

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 Generator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Resta\Support\Generator;
4
5
use Resta\Exception\FileNotFoundException;
6
7
class Generator extends GeneratorAbstract
8
{
9
    /**
10
     * creates class for generator
11
     *
12
     * @param array $replacement
13
     * @return Generator
14
     *
15
     * @throws FileNotFoundException
16
     */
17
    public function createClass($replacement=array())
18
    {
19
        if(file_exists($this->path)){
20
            $content = $this->fileSystem->get($this->getStubFile());
21
            $this->loaded['createClass'] = true;
22
23
            if($this->fileSystem->put($this->file,$content)!==FALSE){
24
                $this->replaceFileContent($replacement,$this->file);
25
            }
26
        }
27
28
        return $this;
29
    }
30
31
    /**
32
     * creates class document for generator
33
     *
34
     * @param array $documents
35
     *
36
     * @throws FileNotFoundException
37
     */
38
    public function createClassDocument($documents=array())
39
    {
40
        if(isset($this->loaded['createClass']) && count($documents)){
41
            $content = '<?php'.$this->fileSystem->get($this->getEval('createClassDocument'));
42
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
43
        }
44
    }
45
46
    /**
47
     * create class extend object for generator
48
     *
49
     * @param $namespace
50
     * @param $alias
51
     *
52
     * @throws FileNotFoundException
53
     */
54
    public function createClassExtend($namespace, $alias)
55
    {
56
        if(!is_null($namespace) && !is_null($alias) && !preg_match('@extends@',$this->getClassString())){
57
            if(preg_match('@class\s(.*?).*@',$this->getClassString(),$class)){
58
                $statements = explode(' ',$class[0]);
59
                $className = $statements[1];
60
61
                if(count($statements)>2){
62
                    $implements = implode(' ',array_slice($statements,2));
63
                }
64
            }
65
66
            if(isset($className)){
67
                $this->createClassUse([
68
                    $namespace
69
                ]);
70
71
                if(!isset($implements)){
72
                    $content = '<?php'.$this->fileSystem->get($this->getEval('createClassExtend'));
73
                    eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
74
                }
75
                else{
76
                    $content = '<?php'.$this->fileSystem->get($this->getEval('createClassExtendWithImplements'));
77
                    eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
78
                }
79
            }
80
        }
81
    }
82
83
    /**
84
     * create class interface object for generator
85
     *
86
     * @param array $implements
87
     *
88
     * @throws FileNotFoundException
89
     */
90
    public function createClassImplements($implements=array())
91
    {
92
        if(!is_null($this->getClassString())){
93
94
            $implementList = [];
95
            $implementUseList = [];
96
97
            foreach($implements as $namespace=>$alias) {
98
                $implementUseList[] = $namespace;
99
                $implementList[] = $alias;
100
            }
101
102
            $this->createClassUse($implementUseList);
103
104
            $content = '<?php'.$this->fileSystem->get($this->getEval('createClassImplements'));
105
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
106
        }
107
    }
108
109
    /**
110
     * creates class property for generator
111
     *
112
     * @param array $properties
113
     * @param bool $loadedMethod
114
     *
115
     * @throws FileNotFoundException
116
     */
117
    public function createClassProperty($properties=array(),$loadedMethod=false)
118
    {
119
        if(is_null($this->classProperties)){
120
            $this->classProperties = $properties;
121
        }
122
123
        if(isset($this->loaded['createMethod'])){
124
            $this->classProperties = $properties;
125
            $loadedMethod = true;
126
        }
127
128
        if($loadedMethod){
129
            foreach ($this->classProperties as $property) {
130
                if(!preg_match('@'.$this->regexEscape($property).'@',$this->fileSystem->get($this->file))){
131
                    $content = '<?php'.$this->fileSystem->get($this->getEval('createClassProperty'));
132
                    eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
133
                }
134
            }
135
136
            if(isset($this->loaded['createClassPropertyDocument'])){
137
                $this->createClassPropertyDocument($this->loaded['createClassPropertyDocument']);
138
            }
139
        }
140
    }
141
142
    /**
143
     * creates class property document for generator
144
     *
145
     * @param array $properties
146
     *
147
     * @throws FileNotFoundException
148
     */
149
    public function createClassPropertyDocument($properties=array())
150
    {
151
        $this->loaded['createClassPropertyDocument'] = $properties;
152
153
        foreach ($properties as $property=>$documents){
154
            $content = '<?php'.$this->fileSystem->get($this->getEval('createClassPropertyDocument'));
155
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
156
        }
157
    }
158
159
    /**
160
     * creates class trait for generator
161
     *
162
     * @param $trait
163
     *
164
     * @throws FileNotFoundException
165
     */
166
    public function createClassTrait($trait)
167
    {
168
        if(isset($this->loaded['createClass']) && is_string($trait)){
169
            $content = '<?php'.$this->fileSystem->get($this->getEval('createClassTrait'));
170
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
171
        }
172
    }
173
174
    /**
175
     * creates class use statements for generator
176
     *
177
     * @param array $uses
178
     *
179
     * @throws FileNotFoundException
180
     */
181
    public function createClassUse($uses=array())
182
    {
183
        if(!is_null($this->getClassString()) && count($uses)){
184
            $content = '<?php'.$this->fileSystem->get($this->getEval('createClassUse'));
185
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
186
        }
187
    }
188
189
    /**
190
     * creates method for generator
191
     *
192
     * @param array $methods
193
     *
194
     * @throws FileNotFoundException
195
     */
196
    public function createMethod($methods=array())
197
    {
198
        if(preg_match('@class\s.*\n{@',$this->fileSystem->get($this->file),$parse) && count($methods)){
199
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethod'));
200
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
201
202
            $this->createClassProperty([],true);
203
            $this->loaded['createMethod'] = true;
204
        }
205
    }
206
207
    /**
208
     * accessible properties method for generator
209
     *
210
     * @param array $methods
211
     * @return void|mixed
212
     *
213
     * @throws FileNotFoundException
214
     */
215
    public function createMethodAccessibleProperty($methods=array())
216
    {
217
        foreach($methods as $method=>$accessibleValue){
218
            $this->accessibleProperties[$method] = $accessibleValue;
219
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodAccessibleProperty'));
220
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
221
        }
222
    }
223
224
    /**
225
     * creates method for generator
226
     *
227
     * @param array $methods
228
     *
229
     * @throws FileNotFoundException
230
     */
231
    public function createMethodBody($methods=array())
232
    {
233
        foreach ($methods as $method=>$body){
234
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodBody'));
235
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
236
        }
237
    }
238
239
    /**
240
     * creates method for generator
241
     *
242
     * @param array $methods
243
     *
244
     * @throws FileNotFoundException
245
     */
246
    public function createMethodDocument($methods=array())
247
    {
248
        foreach ($methods as $method=>$documents){
249
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodDocument'));
250
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
251
        }
252
    }
253
254
    /**
255
     * accessible properties method for generator
256
     *
257
     * @param array $methods
258
     * @return void|mixed
259
     *
260
     * @throws FileNotFoundException
261
     */
262
    public function createMethodParameters($methods=array())
263
    {
264
        foreach($methods as $method=>$parameter) {
265
            $this->methodParameters[$method] = $parameter;
266
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodParameters'));
267
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
268
        }
269
    }
270
271
}
272