Passed
Push — master ( 77647e...8c97e9 )
by Chubarov
02:42
created

MigrateLaravelCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 125
Duplicated Lines 22.4 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 28
loc 125
ccs 9
cts 9
cp 1
rs 10
wmc 12
lcom 1
cbo 5

9 Methods

Rating   Name   Duplication   Size   Complexity  
A moveMigrate() 14 14 2
A moveSeeders() 14 14 2
A moveConfig() 0 9 1
A createDir() 0 7 2
A getContent() 0 4 1
A getDateNormalize() 0 8 1
A __construct() 0 7 1
A configure() 0 4 1
A execute() 0 19 1

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
namespace agoalofalife\Commands;
3
4
5
use Carbon\Carbon;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class MigrateLaravelCommand extends Command
13
{
14
    protected $listFileMigrations = [
15
        'country' => '_create_table_country',
16
        'regions' => '_create_table_regions',
17
        'cities'  => '_create_table_cities'
18
    ];
19
20
    protected $listFilesSeeder = [
21
      'CountryTableSeeder' => 'CountryTableSeeder.php',
22
      'RegionsTableSeeder' => 'RegionsTableSeeder.php',
23
      'CitiesTableSeeder'  => 'CitiesTableSeeder.php'
24
    ];
25
26
    protected $formatterStyle;
27
    protected $progressBar;
28
29 2
    public function __construct(OutputFormatterStyle $formatterStyle, ProgressBar $bar)
30
    {
31 2
        parent::__construct();
32 2
        $this->formatterStyle = $formatterStyle;
33 2
        $this->progressBar    = $bar;
34 2
        $this->progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%');
35 2
    }
36
37 2
    protected function configure() : void
38
    {
39 2
        $this->setName('migrate:laravel')->setHelp('to migrate files to Laravel');
40 2
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $output->getFormatter()->setStyle('fire', $this->formatterStyle);
45
46
        $output->writeln([
47
            '<fire>There is a migration in the project Laravel</fire>',
48
            ''
49
        ]);
50
        
51
        $this->progressBar->start();
52
53
        $this->moveMigrate( $this->progressBar );
54
        $this->moveSeeders( $this->progressBar );
55
        $this->moveConfig(  $this->progressBar );
56
57
        $this->progressBar->finish();
58
        $output->writeln(['']);
59
        $output->writeln(['<info>All successfully copied!</info>']);
60
    }
61
62 View Code Duplication
    protected function moveMigrate(ProgressBar $progress)
0 ignored issues
show
Duplication introduced by
This method 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...
63
    {
64
        $pathToMigrationsLaravel  =  $_SERVER["PWD"] . '/database/migrations/';
65
        $pathToStubs              = __DIR__ . '/../database/migrations/stubs/';
66
67
        $this->createDir($pathToMigrationsLaravel);
68
69
        foreach ($this->listFileMigrations as $name => $migrate)
70
        {
71
            $fileName = $pathToMigrationsLaravel . $this->getDateNormalize() . $migrate . '.php';
72
            file_put_contents($fileName, $this->getContent($pathToStubs . $name));
73
            $progress->advance();
74
        }
75
    }
76
77 View Code Duplication
    protected function moveSeeders(ProgressBar $progress)
0 ignored issues
show
Duplication introduced by
This method 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...
78
    {
79
        $pathToSeedersLaravel     =  $_SERVER["PWD"] . '/database/seeds/';
80
        $pathToSeed               = __DIR__ . '/../database/seeds/stubs/';
81
82
        $this->createDir($pathToSeedersLaravel);
83
84
        foreach ($this->listFilesSeeder as $name => $seed)
85
        {
86
            $fileName = $pathToSeedersLaravel . $seed;
87
            file_put_contents($fileName, $this->getContent($pathToSeed . $name));
88
            $progress->advance();
89
        }
90
    }
91
92
    protected function moveConfig(ProgressBar $progress) : void
93
    {
94
        $pathToConfig               = __DIR__ . '/../config.php';
95
        $pathToConfigsLaravel       =  $_SERVER["PWD"] . '/config/';
96
        $this->createDir($pathToConfigsLaravel);
97
98
        copy($pathToConfig,$pathToConfigsLaravel . 'geography.php' );
99
        $progress->advance();
100
    }
101
102
    /**
103
     * Just create directory
104
     * @param $dir
105
     */
106
    protected function createDir(string $dir) : void
107
    {
108
        if (is_dir($dir) === false)
109
        {
110
            mkdir($dir, 0777, true);
111
        }
112
    }
113
114
    /**
115
     * Data from stubs file
116
     * @param $nameFile
117
     * @return bool|string
118
     */
119
    protected function getContent(string $nameFile) : string
120
    {
121
        return file_get_contents($nameFile);
122
    }
123
124
    /**
125
     * Get date normalize mow
126
     * @return mixed
127
     */
128
    protected function getDateNormalize() : string
129
    {
130
        $date = Carbon::now();
131
        $date = preg_replace('/-|\s/','_', $date);
132
        $data = preg_replace('/:/','', $date);
133
134
        return $data;
135
    }
136
}