Completed
Pull Request — master (#20)
by Pavel
03:20
created

MigrateCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
lcom 0
cbo 4
dl 56
loc 56
rs 10
c 2
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 7 7 1
B execute() 27 27 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Console\Traits\DbHelper;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Finder\Finder;
10
11
/**
12
 * MigrateCommand
13
 */
14 View Code Duplication
class MigrateCommand extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    use DbHelper;
17
18
    /**
19
     * @var string Table name where migrations info is kept
20
     */
21
    const MIGRATIONS_TABLE = 'migrations';
22
23
    /**
24
     * Configuration of command
25
     */
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('migrate')
30
            ->setDescription('Command for run migration')
31
        ;
32
    }
33
34
    /**
35
     * Execute method of command
36
     *
37
     * @param InputInterface $input
38
     * @param OutputInterface $output
39
     *
40
     * @return void
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        if (!is_dir(MIGRATIONS_PATH) || !is_readable(MIGRATIONS_PATH)) {
45
            throw new \RunTimeException(sprintf('Migrations path `%s` is not good', MIGRATIONS_PATH));
46
        }
47
48
        $output->writeln([
49
            '<info>Run migrations</info>',
50
            sprintf('Ensure table `%s` presence', self::MIGRATIONS_TABLE)
51
        ]);
52
53
        try {
54
            $this->safeCreateTable(self::MIGRATIONS_TABLE);
55
        } catch (\Exception $e) {
56
            $output->writeln([
57
                sprintf('Can\'t ensure table `%s` presence. Please verify DB connection params and presence of database named', self::MIGRATIONS_TABLE),
58
                sprintf('Error: `%s`', $e->getMessage()),
59
            ]);
60
        }
61
62
        $finder = new Finder();
63
        $finder->files()->name('*.php')->in(MIGRATIONS_PATH);
64
65
        $this->runActions($finder, $output, self::MIGRATIONS_TABLE, 'up');
66
67
        return;
68
    }
69
}
70