Test Failed
Push — master ( 68289f...b25dc9 )
by huang
03:32
created

SeedCreate   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 152
Duplicated Lines 21.71 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 33
loc 152
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 33 33 3
C execute() 0 76 9
B getTemplate() 0 29 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
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @link      https://www.github.com/janhuang
7
 * @link      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Console;
11
12
13
use Phinx\Config\Config as MConfig;
14
use Phinx\Console\Command\Create;
15
use Phinx\Util\Util;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class SeedCreate extends Create
20
{
21 View Code Duplication
    public function configure()
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...
22
    {
23
        parent::configure();
24
        $path = app()->getPath() . '/database/schema';
25
        if (!file_exists($path)) {
26
            mkdir($path, 0755, true);
27
        }
28
        $this->setName('seed:create');
29
        $database = config()->get('database');
30
        $env = [];
31
        $keys = array_keys($database);
32
        $default = $keys[0];
33
        foreach ($database as $name => $config) {
34
            $env[$name] = [
35
                "adapter" => "mysql",
36
                "host" => config()->get('database.' . $name . '.host'),
37
                "name" => config()->get('database.' . $name . '.name'),
38
                "user" => config()->get('database.' . $name . '.user'),
39
                "pass" => config()->get('database.' . $name . '.pass'),
40
                "port" => config()->get('database.' . $name . '.port'),
41
                'charset' => config()->get('database.' . $name . '.charset', 'utf8'),
42
            ];
43
        }
44
        $this->setConfig(new MConfig(array(
45
            "paths" => array(
46
                "migrations" => $path,
47
                "seeds" => $path,
48
            ),
49
            "environments" => array_merge([
50
                "default_database" => $default,
51
            ], $env),
52
        )));
53
    }
54
55
    /**
56
     * Create the new migration.
57
     *
58
     * @param InputInterface $input
59
     * @param OutputInterface $output
60
     * @throws \RuntimeException
61
     * @throws \InvalidArgumentException
62
     * @return void
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66
        $this->bootstrap($input, $output);
67
68
        // get the migration path from the config
69
        $path = $this->getMigrationPath($input, $output);
70
71
        if (!file_exists($path)) {
72
            $helper = $this->getHelper('question');
73
            $question = $this->getCreateMigrationDirectoryQuestion();
74
75
            if ($helper->ask($input, $output, $question)) {
76
                mkdir($path, 0755, true);
77
            }
78
        }
79
80
        $this->verifyMigrationDirectory($path);
81
82
        $path = realpath($path);
83
        $className = $input->getArgument('name');
84
85
        if (!Util::isValidPhinxClassName($className)) {
86
            throw new \InvalidArgumentException(sprintf(
87
                'The migration class name "%s" is invalid. Please use CamelCase format.',
88
                $className
89
            ));
90
        }
91
92
        if (!Util::isUniqueMigrationClassName($className, $path)) {
93
            throw new \InvalidArgumentException(sprintf(
94
                'The migration class name "%s" already exists',
95
                $className
96
            ));
97
        }
98
99
        // Compute the file path
100
        $fileName = $className . '.php';
101
        $filePath = $path . DIRECTORY_SEPARATOR . $fileName;
102
103
        if (is_file($filePath)) {
104
            throw new \InvalidArgumentException(sprintf(
105
                'The file "%s" already exists',
106
                $filePath
107
            ));
108
        }
109
110
111
        $contents = $this->getTemplate();
112
113
        // inject the class names appropriate to this migration
114
        $classes = array(
115
            '$useClassName' => $this->getConfig()->getMigrationBaseClassName(false),
116
            '$className' => $className,
117
            '$baseClassName' => $this->getConfig()->getMigrationBaseClassName(true),
118
        );
119
        $contents = strtr($contents, $classes);
120
121
        if (false === file_put_contents($filePath, $contents)) {
122
            throw new \RuntimeException(sprintf(
123
                'The file "%s" could not be written to',
124
                $path
125
            ));
126
        }
127
128
        $output->writeln('<info>using migration base class</info> ' . $classes['$useClassName']);
129
130
        if (!empty($altTemplate)) {
0 ignored issues
show
Bug introduced by
The variable $altTemplate seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
131
            $output->writeln('<info>using alternative template</info> ' . $altTemplate);
132
        } elseif (!empty($creationClassName)) {
0 ignored issues
show
Bug introduced by
The variable $creationClassName does not exist. Did you mean $className?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
133
            $output->writeln('<info>using template creation class</info> ' . $creationClassName);
134
        } else {
135
            $output->writeln('<info>using default template</info>');
136
        }
137
138
        $output->writeln('<info>created</info> ' . str_replace(getcwd(), '', $filePath));
139
    }
140
141
    public function getTemplate()
142
    {
143
        return '
144
<?php
145
146
use FastD\Model\Migration;
147
use Phinx\Db\Table;
148
149
class $className extends Migration
150
{
151
    /**
152
     * @return Table
153
     */
154
    public function setUp()
155
    {
156
        
157
    }
158
    
159
    /**
160
     * The table preinstall dataset.
161
     *
162
     * @return mixed
163
     */
164
    public function dataSet(Table $table)
165
    {
166
        
167
    }
168
}';
169
    }
170
}