Test Setup Failed
Push — master ( 1c8c73...68a798 )
by Php Easy Api
04:02
created

Model::create()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 116
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 56
c 0
b 0
f 0
nc 64
nop 0
dl 0
loc 116
rs 8.0266

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
        //set entity map
74
75
        $entityDir = $this->directory['modelDir'].''.DIRECTORY_SEPARATOR.'Entity';
76
77
        if(!file_exists($entityDir)){
78
            files()->makeDirectory($entityDir);
79
        }
80
81
        $entityTableName = ucfirst($this->argument['table']);
82
83
        $entityClass = $entityDir.''.DIRECTORY_SEPARATOR.''.$entityTableName.''.DIRECTORY_SEPARATOR.''.$entityTableName;
84
85
86
        $generator = new Generator($entityDir,'EntityMap');
87
88
        if(!file_exists($entityDir.''.DIRECTORY_SEPARATOR.'EntityMap.php')){
89
90
            //$this->setAnnotations();
91
            $generator->createClass();
92
        }
93
94
        $generator->createClassUse([
95
            Utils::getNamespace($entityClass)
96
        ]);
97
98
        $generator->createMethod([
99
            strtolower($this->argument['table'])
100
        ]);
101
102
        $generator->createMethodParameters([
103
            strtolower($this->argument['table']) => '$query'
104
        ]);
105
106
        $generator->createMethodBody([
107
            strtolower($this->argument['table'])=>'return new '.$entityTableName.'($query);'
108
        ]);
109
110
        $generator->createMethodDocument([
111
            strtolower($this->argument['table']) => [
112
                $entityTableName.' Entity Instance',
113
                '',
114
                '@param $query',
115
                '@return '.$entityTableName
116
            ]
117
        ]);
118
119
120
121
        //set builder map
122
        $generator = new Generator($this->directory['builderDir'],'BuilderMap');
123
124
        if(!file_exists($this->directory['builderDir'].''.DIRECTORY_SEPARATOR.'BuilderMap.php')){
125
126
            $this->setAnnotations();
127
            $generator->createClass();
128
        }
129
130
        if(!file_exists($this->touch['model/model'])){
131
132
            $generator->createMethod([
133
                strtolower($this->argument['file'])
134
            ]);
135
136
            $generator->createMethodBody([
137
                strtolower($this->argument['file'])=>'return new '.$this->argument['file'].'Builder();'
138
            ]);
139
140
            $generator->createMethodDocument([
141
                strtolower($this->argument['file']) => [
142
                    $this->argument['file'].' Builder Instance',
143
                    '',
144
                    '@return '.$this->argument['file'].'Builder'
145
                ]
146
            ]);
147
148
        }
149
150
        //set project touch
151
        $this->file->touch($this,[
152
            'stub'=>'Model_Create'
153
        ]);
154
155
156
        echo $this->classical(' > Model called as "'.$this->argument['file'].'" has been successfully created in the '.app()->namespace()->model().'');
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    private function setAnnotations(){
163
164
        $entityMap = app()->path()->model().''.DIRECTORY_SEPARATOR.'Entity'.DIRECTORY_SEPARATOR.'EntityMap.php';
165
166
        if(file_exists($entityMap)){
167
168
            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

168
            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...
169
                [
170
                    'Trait ServiceAnnotationsManager'=>'Trait ServiceAnnotationsManager'.PHP_EOL.' * @property \\'.app()->namespace()->model().'\Entity\EntityMap entity',
171
                    '* @property \\'.app()->namespace()->model().'\Entity\EntityMap entity'=>'* @property \\'.app()->namespace()->model().'\Entity\EntityMap entity'.PHP_EOL.' * @property \\'.app()->namespace()->builder().'\BuilderMap builder',
172
                ]);
173
        }
174
175
176
        /**Utils::changeClass(path()->serviceAnnotations().'.php',
177
        ['Trait ServiceAnnotationsManager'=>'Trait ServiceAnnotationsManager'.PHP_EOL.' * @property \\'.app()->namespace()->builder().'\BuilderMap builder'
178
        ]);**/
179
    }
180
}