Completed
Push — master ( aed5e6...e7b04e )
by Fumio
01:47
created

DatabaseSeedCommand::handle()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 15
cts 15
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 7
nop 1
crap 5
1
<?php
2
3
namespace Jumilla\Versionia\Laravel\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Console\ConfirmableTrait;
7
use Jumilla\Versionia\Laravel\Migrator;
8
9
class DatabaseSeedCommand extends Command
10
{
11
    use DatabaseCommandTrait;
12
    use ConfirmableTrait;
13
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'database:seed
20
        {name? : the name of seed.}
21
        {--force : Force the operation to run when in production.}
22
    ';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Insert seed to database';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
35
     *
36
     * @return mixed
37
     */
38 7
    public function handle(Migrator $migrator)
39
    {
40 7
        if (!$this->confirmToProceed()) {
41 1
            return;
42
        }
43
44 6
        $seed = $this->argument('name') ?: $migrator->defaultSeed();
45
46 6
        if (!$seed) {
47 1
            $this->error('Default seed is not defined.');
48
49 1
            return;
50
        }
51
52 5
        $class = $migrator->seedClass($seed);
0 ignored issues
show
Bug introduced by Fumio Furukawa
It seems like $seed defined by $this->argument('name') ...migrator->defaultSeed() on line 44 can also be of type array; however, Jumilla\Versionia\Laravel\Migrator::seedClass() 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...
53
54 5
        if (!$class) {
55 2
            $this->error("Seed '$seed' is not defined.");
56
57 2
            return;
58
        }
59
60 3
        $this->infoSeedRun($seed, $class);
0 ignored issues
show
Bug introduced by Fumio Furukawa
It seems like $seed defined by $this->argument('name') ...migrator->defaultSeed() on line 44 can also be of type array; however, Jumilla\Versionia\Larave...andTrait::infoSeedRun() 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...
61
62 3
        $seeder = new $class();
63
64 3
        $seeder->setCommand($this)->run();
65 3
    }
66
}
67