Test Setup Failed
Push — master ( 66c8e3...fc718b )
by Php Easy Api
03:36
created

Model::create()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 126
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 61
nc 256
nop 0
dl 0
loc 126
rs 6.0686
c 0
b 0
f 0

How to fix   Long Method   

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 Resta\Console\Source\Model;
4
5
use Resta\Support\Generator\Generator;
6
use Resta\Support\Utils;
7
use Resta\Console\ConsoleOutputter;
8
use Resta\Console\ConsoleListAccessor;
9
use Resta\Foundation\PathManager\StaticPathModel;
10
11
class Model extends ConsoleOutputter {
12
13
    use ConsoleListAccessor;
14
15
    /**
16
     * @var $type
0 ignored issues
show
Documentation Bug introduced by
The doc comment $type at position 0 could not be parsed: Unknown type name '$type' at position 0 in $type.
Loading history...
17
     */
18
    public $type='model';
19
20
    /**
21
     * @var array
22
     */
23
    protected $runnableMethods = [
24
        'create'=>'Creates a model file'
25
    ];
26
27
    /**
28
     * @var bool
29
     */
30
    protected $projectStatus = true;
31
32
    /**
33
     * @var $commandRule
0 ignored issues
show
Documentation Bug introduced by
The doc comment $commandRule at position 0 could not be parsed: Unknown type name '$commandRule' at position 0 in $commandRule.
Loading history...
34
     */
35
    public $commandRule=['model','?table'];
36
37
    /**
38
     * @method create
39
     * @return mixed
40
     */
41
    public function create(){
42
43
        $this->argument['file'] = $this->argument['model'];
44
45
        if(!isset($this->argument['table'])){
46
            $this->argument['table'] = $this->argument['file'].'s';
47
        }
48
49
        //lower case for table
50
        $this->argument['table'] = strtolower($this->argument['table']);
51
52
        $this->directory['modelDir']    = app()->path()->model();
53
        $this->directory['builderDir']  = $this->directory['modelDir'].'/Builder';
54
        $this->directory['contract']    = $this->directory['modelDir'].'/Contract';
55
        $this->directory['helper']      = $this->directory['modelDir'].'/Helper';
56
57
        $this->argument['modelNamespace'] = Utils::getNamespace($this->directory['modelDir']);
58
        $this->argument['builderNamespace'] = Utils::getNamespace($this->directory['builderDir']);
59
        $this->argument['contractNamespace'] = Utils::getNamespace($this->directory['contract']);
60
61
        //set project directory
62
        $this->file->makeDirectory($this);
63
64
        //model set
65
        $this->touch['model/model']     = $this->directory['modelDir'].''.DIRECTORY_SEPARATOR.''.$this->argument['file'].'.php';
66
        $this->touch['model/builder']   = $this->directory['builderDir'].''.DIRECTORY_SEPARATOR.''.$this->argument['file'].'Builder.php';
67
        $this->touch['model/contract']  = $this->directory['contract'].''.DIRECTORY_SEPARATOR.''.$this->argument['file'].'Contract.php';
68
69
        if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'Scope.php')){
70
            $this->touch['model/scope'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'Scope.php';
71
        }
72
73
        if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'Event.php')){
74
            $this->touch['model/event'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'Event.php';
75
        }
76
77
        //set entity map
78
79
        $entityDir = $this->directory['modelDir'].''.DIRECTORY_SEPARATOR.'Entity';
80
81
        if(!file_exists($entityDir)){
82
            files()->makeDirectory($entityDir);
83
        }
84
85
        $entityTableName = ucfirst($this->argument['table']);
86
87
        $entityClass = $entityDir.''.DIRECTORY_SEPARATOR.''.$entityTableName.''.DIRECTORY_SEPARATOR.''.$entityTableName;
88
89
90
        $generator = new Generator($entityDir,'EntityMap');
91
92
        if(!file_exists($entityDir.''.DIRECTORY_SEPARATOR.'EntityMap.php')){
93
94
            //$this->setAnnotations();
95
            $generator->createClass();
96
        }
97
98
        $entityMapNamespace = Utils::getNamespace($entityDir.''.DIRECTORY_SEPARATOR.'EntityMap.php');
99
100
        $entityMapNamespaceResolve = new $entityMapNamespace;
101
102
        if(!method_exists($entityMapNamespaceResolve,strtolower($this->argument['table']))){
103
104
            $generator->createClassUse([
105
                Utils::getNamespace($entityClass)
106
            ]);
107
108
            $generator->createMethod([
109
                strtolower($this->argument['table'])
110
            ]);
111
112
            $generator->createMethodParameters([
113
                strtolower($this->argument['table']) => '$query'
114
            ]);
115
116
            $generator->createMethodBody([
117
                strtolower($this->argument['table'])=>'return new '.$entityTableName.'($query);'
118
            ]);
119
120
            $generator->createMethodDocument([
121
                strtolower($this->argument['table']) => [
122
                    $entityTableName.' Entity Instance',
123
                    '',
124
                    '@param $query',
125
                    '@return '.$entityTableName
126
                ]
127
            ]);
128
        }
129
130
131
        //set builder map
132
        $generator = new Generator($this->directory['builderDir'],'BuilderMap');
133
134
        if(!file_exists($this->directory['builderDir'].''.DIRECTORY_SEPARATOR.'BuilderMap.php')){
135
136
            $this->setAnnotations();
137
            $generator->createClass();
138
        }
139
140
        if(!file_exists($this->touch['model/model'])){
141
142
            $generator->createMethod([
143
                strtolower($this->argument['file'])
144
            ]);
145
146
            $generator->createMethodBody([
147
                strtolower($this->argument['file'])=>'return new '.$this->argument['file'].'Builder();'
148
            ]);
149
150
            $generator->createMethodDocument([
151
                strtolower($this->argument['file']) => [
152
                    $this->argument['file'].' Builder Instance',
153
                    '',
154
                    '@return '.$this->argument['file'].'Builder'
155
                ]
156
            ]);
157
158
        }
159
160
        //set project touch
161
        $this->file->touch($this,[
162
            'stub'=>'Model_Create'
163
        ]);
164
165
166
        echo $this->classical(' > Model called as "'.$this->argument['file'].'" has been successfully created in the '.app()->namespace()->model().'');
167
    }
168
169
    /**
170
     * @return bool
171
     */
172
    private function setAnnotations(){
173
174
        $entityMap = app()->path()->model().''.DIRECTORY_SEPARATOR.'Entity'.DIRECTORY_SEPARATOR.'EntityMap.php';
175
176
        if(file_exists($entityMap)){
177
178
            Utils::changeClass(path()->serviceAnnotations().'.php',
0 ignored issues
show
Bug introduced by
The method serviceAnnotations() does not exist on Resta\Contracts\StaticPathContracts. ( Ignorable by Annotation )

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

178
            Utils::changeClass(path()->/** @scrutinizer ignore-call */ serviceAnnotations().'.php',

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
179
                [
180
                    'Trait ServiceAnnotationsManager'=>'Trait ServiceAnnotationsManager'.PHP_EOL.' * @property \\'.app()->namespace()->model().'\Entity\EntityMap entity',
181
                    '* @property \\'.app()->namespace()->model().'\Entity\EntityMap entity'=>'* @property \\'.app()->namespace()->model().'\Entity\EntityMap entity'.PHP_EOL.' * @property \\'.app()->namespace()->builder().'\BuilderMap builder',
182
                ]);
183
        }
184
185
186
        /**Utils::changeClass(path()->serviceAnnotations().'.php',
187
        ['Trait ServiceAnnotationsManager'=>'Trait ServiceAnnotationsManager'.PHP_EOL.' * @property \\'.app()->namespace()->builder().'\BuilderMap builder'
188
        ]);**/
189
    }
190
}