Generator::createClassTrait()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 3
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
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)){
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type null; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

19
        if(file_exists(/** @scrutinizer ignore-type */ $this->path)){
Loading history...
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())){
0 ignored issues
show
Bug introduced by
It seems like $this->getClassString() can also be of type null; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        if(!is_null($namespace) && !is_null($alias) && !preg_match('@extends@',/** @scrutinizer ignore-type */ $this->getClassString())){
Loading history...
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('@'.$this->type.'\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
     * creates abstract method for generator
209
     *
210
     * @param array $methods
211
     *
212
     * @throws FileNotFoundException
213
     */
214
    public function createMethodAbstract($methods=array())
215
    {
216
        if(preg_match('@'.$this->type.'\s.*\n{@',$this->fileSystem->get($this->file),$parse) && count($methods)){
217
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodAbstract'));
218
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
219
220
            $this->createClassProperty([],true);
221
            $this->loaded['createMethod'] = true;
222
        }
223
    }
224
225
    /**
226
     * accessible properties method for generator
227
     *
228
     * @param array $methods
229
     * @return void|mixed
230
     *
231
     * @throws FileNotFoundException
232
     */
233
    public function createMethodAccessibleProperty($methods=array())
234
    {
235
        foreach($methods as $method=>$accessibleValue){
236
            $this->accessibleProperties[$method] = $accessibleValue;
237
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodAccessibleProperty'));
238
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
239
        }
240
    }
241
242
    /**
243
     * creates method body for generator
244
     *
245
     * @param array $methods
246
     *
247
     * @throws FileNotFoundException
248
     */
249
    public function createMethodBody($methods=array())
250
    {
251
        foreach ($methods as $method=>$body){
252
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodBody'));
253
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
254
        }
255
    }
256
257
    /**
258
     * creates abstract method document for generator
259
     *
260
     * @param array $methods
261
     *
262
     * @throws FileNotFoundException
263
     */
264
    public function createMethodAbstractDocument($methods=array())
265
    {
266
        foreach ($methods as $method=>$documents){
267
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodAbstractDocument'));
268
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
269
        }
270
    }
271
272
    /**
273
     * creates method for generator
274
     *
275
     * @param array $methods
276
     *
277
     * @throws FileNotFoundException
278
     */
279
    public function createMethodDocument($methods=array())
280
    {
281
        foreach ($methods as $method=>$documents){
282
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodDocument'));
283
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
284
        }
285
    }
286
287
    /**
288
     * creates interface method for generator
289
     *
290
     * @param array $methods
291
     *
292
     * @throws FileNotFoundException
293
     */
294
    public function createMethodImplement($methods=array())
295
    {
296
        if(preg_match('@'.$this->type.'\s.*\n{@',$this->fileSystem->get($this->file),$parse) && count($methods)){
297
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodImplement'));
298
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
299
300
            $this->createClassProperty([],true);
301
            $this->loaded['createMethod'] = true;
302
        }
303
    }
304
305
    /**
306
     * creates interface method document for generator
307
     *
308
     * @param array $methods
309
     *
310
     * @throws FileNotFoundException
311
     */
312
    public function createMethodImplementDocument($methods=array())
313
    {
314
        foreach ($methods as $method=>$documents){
315
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodImplementDocument'));
316
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
317
        }
318
    }
319
320
    /**
321
     * accessible properties method for generator
322
     *
323
     * @param array $methods
324
     * @return void|mixed
325
     *
326
     * @throws FileNotFoundException
327
     */
328
    public function createMethodParameters($methods=array())
329
    {
330
        foreach($methods as $method=>$parameter) {
331
            $this->methodParameters[$method] = $parameter;
332
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodParameters'));
333
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
334
        }
335
    }
336
337
    /**
338
     * accessible properties abstract for generator
339
     *
340
     * @param array $methods
341
     * @return void|mixed
342
     *
343
     * @throws FileNotFoundException
344
     */
345
    public function createMethodAbstractParameters($methods=array())
346
    {
347
        foreach($methods as $method=>$parameter) {
348
            $this->methodParameters[''.$this->type.'_'.$method] = $parameter;
349
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodAbstractParameters'));
350
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
351
        }
352
    }
353
354
    /**
355
     * accessible properties interface for generator
356
     *
357
     * @param array $methods
358
     * @return void|mixed
359
     *
360
     * @throws FileNotFoundException
361
     */
362
    public function createMethodImplementParameters($methods=array())
363
    {
364
        foreach($methods as $method=>$parameter) {
365
            $this->methodParameters[''.$this->type.'_'.$method] = $parameter;
366
            $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodImplementParameters'));
367
            eval("?>$content");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
368
        }
369
    }
370
371
}
372