Completed
Push — feature/database-migrations ( b550b6 )
by Avtandil
02:27
created

MigrateCommand::getMigrationPaths()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 0
dl 0
loc 16
ccs 0
cts 11
cp 0
crap 20
rs 9.7333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Longman\TelegramBot\Console;
5
6
use Illuminate\Console\Command;
7
8
use function array_merge;
9
use function collect;
10
11
use const DIRECTORY_SEPARATOR;
12
13
class MigrateCommand extends Command
14
{
15
    protected $signature = 'migrate
16
                {--path= : The path to the migrations files to be executed}
17
                {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}
18
                {--pretend : Dump the SQL queries that would be run}
19
                {--step : Force the migrations to be run so they can be rolled back individually}';
20
21
    protected $description = 'Run the database migrations';
22
23
    /** @var \Illuminate\Database\Migrations\Migrator */
24
    protected $migrator;
25
26
    public function handle(): void
27
    {
28
        $app = $this->getApplication()->getLaravel();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getLaravel() does only exist in the following sub-classes of Symfony\Component\Console\Application: Illuminate\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
29
        $this->migrator = $app['migrator'];
30
31
        $this->prepareDatabase();
32
33
        $this->migrator->setOutput($this->output)
34
            ->run($this->getMigrationPaths(), [
35
                'pretend' => $this->option('pretend'),
36
                'step'    => $this->option('step'),
37
            ]);
38
    }
39
40
    protected function prepareDatabase(): void
41
    {
42
        $this->migrator->setConnection('default');
43
44
        if (! $this->migrator->repositoryExists()) {
45
            $this->call('migrate:install');
46
        }
47
    }
48
49
    protected function getMigrationPaths(): array
50
    {
51
        // Here, we will check to see if a path option has been defined. If it has we will
52
        // use the path relative to the root of the installation folder so our database
53
        // migrations may be run for any customized path from within the application.
54
        if ($this->input->hasOption('path') && $this->option('path')) {
55
            return collect($this->option('path'))->map(function ($path) {
56
                return ! $this->usingRealPath() ? $this->laravel->basePath() . '/' . $path : $path;
57
            })->all();
58
        }
59
60
        return array_merge(
61
            $this->migrator->paths(),
62
            [$this->getMigrationPath()]
63
        );
64
    }
65
66
    protected function usingRealPath(): bool
67
    {
68
        return $this->input->hasOption('realpath') && $this->option('realpath');
69
    }
70
71
    protected function getMigrationPath(): string
72
    {
73
        return $this->laravel->basePath() . DIRECTORY_SEPARATOR . 'migrations';
74
    }
75
}
76