Completed
Push — master ( 0e1003...1276c7 )
by Abdelrahman
61:46 queued 59:06
created

MigrateMakeCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A writeMigration() 0 6 1
A makeDirectory() 0 8 2
A getMigrationPath() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Console\Commands;
6
7
use Illuminate\Console\ConfirmableTrait;
8
use Cortex\Foundation\Traits\ConsoleMakeModuleCommand;
9
use Illuminate\Database\Console\Migrations\MigrateMakeCommand as BaseMigrateMakeCommand;
10
11
class MigrateMakeCommand extends BaseMigrateMakeCommand
12
{
13
    use ConfirmableTrait;
14
    use ConsoleMakeModuleCommand;
15
16
    /**
17
     * The console command signature.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'make:migration {name : The name of the migration.}
22
        {--module= : The name of the module.}
23
        {--create= : The table to be created.}
24
        {--table= : The table to migrate.}';
25
26
    /**
27
     * Write the migration file to disk.
28
     *
29
     * @param string $name
30
     * @param string $table
31
     * @param bool   $create
32
     *
33
     * @return void
34
     */
35
    protected function writeMigration($name, $table, $create): void
36
    {
37
        $this->makeDirectory($this->getMigrationPath());
38
39
        parent::writeMigration($name, $table, $create);
40
    }
41
42
    /**
43
     * Build the directory for the class if necessary.
44
     *
45
     * @param string $path
46
     *
47
     * @return string
48
     */
49
    protected function makeDirectory($path): string
50
    {
51
        if (! $this->laravel['files']->isDirectory($path)) {
52
            $this->laravel['files']->makeDirectory($path, 0777, true, true);
53
        }
54
55
        return $path;
56
    }
57
58
    /**
59
     * Get migration path (either specified by '--path' or '--module' options).
60
     *
61
     * @throws \Exception
62
     *
63
     * @return string
64
     */
65
    protected function getMigrationPath(): string
66
    {
67
        if (! $this->laravel->files->exists($path = $this->laravel['path'].DIRECTORY_SEPARATOR.$this->moduleName())) {
0 ignored issues
show
Bug introduced by
Accessing files on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
68
            throw new \Exception("Invalid path: {$path}");
69
        }
70
71
        return $path.DIRECTORY_SEPARATOR.'database'.DIRECTORY_SEPARATOR.'migrations';
72
    }
73
}
74