Passed
Pull Request — master (#1928)
by Corey
03:52 queued 01:10
created

SeedRun::execute()   B

Complexity

Conditions 10
Paths 74

Size

Total Lines 62
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 10.4632

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 35
dl 0
loc 62
c 1
b 1
f 0
ccs 30
cts 36
cp 0.8333
rs 7.6666
cc 10
nc 74
nop 2
crap 10.4632

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Console\Command;
9
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class SeedRun extends AbstractCommand
15
{
16
    /**
17
     * @var string
18
     */
19
    protected static $defaultName = 'seed:run';
20
21
    /**
22
     * {@inheritDoc}
23
     *
24
     * @return void
25
     */
26
    protected function configure()
27
    {
28
        parent::configure();
29
30
        $this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
31
32
        $this->setDescription('Run database seeders')
33
            ->addOption('--seed', '-s', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'What is the name of the seeder?')
34
            ->setHelp(
35
                <<<EOT
36
The <info>seed:run</info> command runs all available or individual seeders
37
38
<info>phinx seed:run -e development</info>
39
<info>phinx seed:run -e development -s UserSeeder</info>
40 35
<info>phinx seed:run -e development -s UserSeeder -s PermissionSeeder -s LogSeeder</info>
41
<info>phinx seed:run -e development -v</info>
42 35
43
EOT
44 35
            );
45
    }
46 35
47 35
    /**
48 35
     * Run database seeders.
49 35
     *
50
     * @param \Symfony\Component\Console\Input\InputInterface $input Input
51
     * @param \Symfony\Component\Console\Output\OutputInterface $output Output
52
     *
53
     * @return int integer 0 on success, or an error code.
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $this->bootstrap($input, $output);
58
59 35
        $seedSet = $input->getOption('seed');
60 35
        $environment = $input->getOption('environment');
61
62
        if ($environment === null) {
63
            $environment = $this->getConfig()->getDefaultEnvironment();
64
            $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment);
65
        } else {
66
            $output->writeln('<info>using environment</info> ' . $environment);
0 ignored issues
show
Bug introduced by
Are you sure $environment of type boolean|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            $output->writeln('<info>using environment</info> ' . /** @scrutinizer ignore-type */ $environment);
Loading history...
67
        }
68
69 4
        if (!$this->getConfig()->hasEnvironment($environment)) {
0 ignored issues
show
Bug introduced by
It seems like $environment can also be of type string[]; however, parameter $name of Phinx\Config\ConfigInterface::hasEnvironment() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        if (!$this->getConfig()->hasEnvironment(/** @scrutinizer ignore-type */ $environment)) {
Loading history...
70
            $output->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));
0 ignored issues
show
Bug introduced by
It seems like $environment can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            $output->writeln(sprintf('<error>The environment "%s" does not exist</error>', /** @scrutinizer ignore-type */ $environment));
Loading history...
71 4
72
            return self::CODE_ERROR;
73 4
        }
74 4
75
        $envOptions = $this->getConfig()->getEnvironment($environment);
0 ignored issues
show
Bug introduced by
It seems like $environment can also be of type string[]; however, parameter $name of Phinx\Config\ConfigInterface::getEnvironment() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $envOptions = $this->getConfig()->getEnvironment(/** @scrutinizer ignore-type */ $environment);
Loading history...
76 4
        if (isset($envOptions['adapter'])) {
77 3
            $output->writeln('<info>using adapter</info> ' . $envOptions['adapter']);
78 3
        }
79 3
80 1
        if (isset($envOptions['wrapper'])) {
81
            $output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']);
82
        }
83 4
84 4
        if (isset($envOptions['name'])) {
85 3
            $output->writeln('<info>using database</info> ' . $envOptions['name']);
86 3
        } else {
87
            $output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
88 4
89
            return self::CODE_ERROR;
90
        }
91
92 4
        if (isset($envOptions['table_prefix'])) {
93 3
            $output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix']);
94 3
        }
95 1
        if (isset($envOptions['table_suffix'])) {
96 1
            $output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix']);
97
        }
98
99 3
        $start = microtime(true);
100
101
        if (empty($seedSet)) {
102 3
            // run all the seed(ers)
103
            $this->getManager()->seed($environment);
0 ignored issues
show
Bug introduced by
It seems like $environment can also be of type string[]; however, parameter $environment of Phinx\Migration\Manager::seed() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
            $this->getManager()->seed(/** @scrutinizer ignore-type */ $environment);
Loading history...
104
        } else {
105
            // run seed(ers) specified in a comma-separated list of classes
106 3
            foreach ($seedSet as $seed) {
107
                $this->getManager()->seed($environment, trim($seed));
108 3
            }
109
        }
110 2
111 2
        $end = microtime(true);
112
113 1
        $output->writeln('');
114 1
        $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
115 1
116
        return self::CODE_SUCCESS;
117
    }
118
}
119