FactoryCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 1
A getFile() 0 8 2
A getFieldsContent() 0 21 4
1
<?php namespace Wn\Generators\Commands;
2
3
4
class FactoryCommand extends BaseCommand {
5
6
	protected $signature = 'wn:factory
7
        {model : full qualified name of the model.}
8
        {--fields= : the fields to generate.}
9
        {--file= : the factories file.}
10
        {--parsed : tells the command that arguments have been already parsed. To use when calling the command from an other command and passing the parsed arguments and options}
11
        {--force= : override the existing files}
12
    ';
13
14
	protected $description = 'Generates a model factory';
15
16
    public function handle()
17
    {
18
        $model = $this->argument('model');
19
20
        $file = $this->getFile();
21
22
        $content = $this->fs->get($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $this->getFile() on line 20 can also be of type array; however, Illuminate\Filesystem\Filesystem::get() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
23
24
        $content .= $this->getTemplate('factory')
25
            ->with([
26
                'model' => $model,
27
                'factory_fields' => $this->getFieldsContent()
28
            ])
29
            ->get();
30
31
        $this->save($content, $file, "{$model} factory", true);
32
    }
33
34
    protected function getFile()
35
    {
36
        $file = $this->option('file');
37
        if(! $file){
38
            $file = './database/factories/ModelFactory.php';
39
        }
40
        return $file;
41
    }
42
43
    protected function getFieldsContent()
44
    {
45
        $content = [];
46
47
        $fields = $this->option('fields');
48
49
        if($fields){
50
            if(! $this->option('parsed')){
51
                $fields = $this->getArgumentParser('factory-fields')->parse($fields);
52
            }
53
            $template = $this->getTemplate('factory/field');
54
            foreach($fields as $field){
55
                $content[] = $template->with($field)->get();
56
            }
57
            $content = implode(PHP_EOL, $content);
58
        } else {
59
            $content = "        // Fields here";
60
        }
61
62
        return $content;
63
    }
64
65
}
66