Model::create()   F
last analyzed

Complexity

Conditions 11
Paths 1024

Size

Total Lines 137
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 68
nc 1024
nop 0
dl 0
loc 137
rs 3.2181
c 0
b 0
f 0

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 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['builderAssistantDir']  = $this->directory['modelDir'].'/Builder/Assistant';
55
        $this->directory['contract']    = $this->directory['modelDir'].'/Contract';
56
        $this->directory['helper']      = $this->directory['modelDir'].'/Helper';
57
58
        $this->argument['modelNamespace'] = Utils::getNamespace($this->directory['modelDir']);
59
        $this->argument['builderNamespace'] = Utils::getNamespace($this->directory['builderDir']);
60
        $this->argument['builderAssistantNamespace'] = Utils::getNamespace($this->directory['builderAssistantDir']);
61
        $this->argument['contractNamespace'] = Utils::getNamespace($this->directory['contract']);
62
63
        //set project directory
64
        $this->file->makeDirectory($this);
65
66
        //model set
67
        $this->touch['model/model']     = $this->directory['modelDir'].''.DIRECTORY_SEPARATOR.''.$this->argument['file'].'.php';
68
        $this->touch['model/builder']   = $this->directory['builderDir'].''.DIRECTORY_SEPARATOR.''.$this->argument['file'].'Builder.php';
69
        $this->touch['model/builderasistant']   = $this->directory['builderAssistantDir'].''.DIRECTORY_SEPARATOR.'Builder.php';
70
        $this->touch['model/contract']  = $this->directory['contract'].''.DIRECTORY_SEPARATOR.''.$this->argument['file'].'Contract.php';
71
72
        if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'Scope.php')){
73
            $this->touch['model/scope'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'Scope.php';
74
        }
75
76
        if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'Event.php')){
77
            $this->touch['model/event'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'Event.php';
78
        }
79
80
        if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'TableChanges.php')){
81
            $this->touch['model/tablechanges'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'TableChanges.php';
82
        }
83
84
        if(!file_exists($this->directory['helper'].''.DIRECTORY_SEPARATOR.'Constructor.php')){
85
            $this->touch['model/constructor'] = $this->directory['helper'].''.DIRECTORY_SEPARATOR.'Constructor.php';
86
        }
87
88
        //set entity map
89
90
        $entityDir = $this->directory['modelDir'].''.DIRECTORY_SEPARATOR.'Entity';
91
92
        if(!file_exists($entityDir)){
93
            files()->makeDirectory($entityDir);
94
        }
95
96
        $entityTableName = ucfirst($this->argument['table']);
97
98
        $entityClass = $entityDir.''.DIRECTORY_SEPARATOR.''.$entityTableName.''.DIRECTORY_SEPARATOR.''.$entityTableName;
99
100
101
        $generator = new Generator($entityDir,'EntityMap');
102
103
        if(!file_exists($entityDir.''.DIRECTORY_SEPARATOR.'EntityMap.php')){
104
105
            //$this->setAnnotations();
106
            $generator->createClass();
107
        }
108
109
        $entityMapNamespace = Utils::getNamespace($entityDir.''.DIRECTORY_SEPARATOR.'EntityMap.php');
110
111
        $entityMapNamespaceResolve = new $entityMapNamespace;
112
113
        if(!method_exists($entityMapNamespaceResolve,strtolower($this->argument['table']))){
114
115
            $generator->createClassUse([
116
                Utils::getNamespace($entityClass)
117
            ]);
118
119
            $generator->createMethod([
120
                strtolower($this->argument['table'])
121
            ]);
122
123
            $generator->createMethodParameters([
124
                strtolower($this->argument['table']) => '$query'
125
            ]);
126
127
            $generator->createMethodBody([
128
                strtolower($this->argument['table'])=>'return new '.$entityTableName.'($query);'
129
            ]);
130
131
            $generator->createMethodDocument([
132
                strtolower($this->argument['table']) => [
133
                    $entityTableName.' Entity Instance',
134
                    '',
135
                    '@param $query',
136
                    '@return '.$entityTableName
137
                ]
138
            ]);
139
        }
140
141
142
        //set builder map
143
        $generator = new Generator($this->directory['builderDir'],'BuilderMap');
144
145
        if(!file_exists($this->directory['builderDir'].''.DIRECTORY_SEPARATOR.'BuilderMap.php')){
146
147
            $this->setAnnotations();
148
            $generator->createClass();
149
        }
150
151
        if(!file_exists($this->touch['model/model'])){
152
153
            $generator->createMethod([
154
                strtolower($this->argument['file'])
155
            ]);
156
157
            $generator->createMethodBody([
158
                strtolower($this->argument['file'])=>'return new '.$this->argument['file'].'Builder();'
159
            ]);
160
161
            $generator->createMethodDocument([
162
                strtolower($this->argument['file']) => [
163
                    $this->argument['file'].' Builder Instance',
164
                    '',
165
                    '@return '.$this->argument['file'].'Builder'
166
                ]
167
            ]);
168
169
        }
170
171
        //set project touch
172
        $this->file->touch($this,[
173
            'stub'=>'Model_Create'
174
        ]);
175
176
177
        echo $this->classical(' > Model called as "'.$this->argument['file'].'" has been successfully created in the '.app()->namespace()->model().'');
178
    }
179
180
    /**
181
     * @return bool
182
     */
183
    private function setAnnotations(){
184
185
        $entityMap = app()->path()->model().''.DIRECTORY_SEPARATOR.'Entity'.DIRECTORY_SEPARATOR.'EntityMap.php';
186
187
        if(file_exists($entityMap)){
188
189
            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

189
            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...
190
                [
191
                    'Trait ServiceAnnotationsManager'=>'Trait ServiceAnnotationsManager'.PHP_EOL.' * @property \\'.app()->namespace()->model().'\Entity\EntityMap entity',
192
                    '* @property \\'.app()->namespace()->model().'\Entity\EntityMap entity'=>'* @property \\'.app()->namespace()->model().'\Entity\EntityMap entity'.PHP_EOL.' * @property \\'.app()->namespace()->builder().'\BuilderMap builder',
193
                ]);
194
        }
195
196
197
        /**Utils::changeClass(path()->serviceAnnotations().'.php',
198
        ['Trait ServiceAnnotationsManager'=>'Trait ServiceAnnotationsManager'.PHP_EOL.' * @property \\'.app()->namespace()->builder().'\BuilderMap builder'
199
        ]);**/
200
    }
201
}