Completed
Pull Request — master (#1773)
by
unknown
01:34
created

SeedRun::execute()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 16
cts 16
cp 1
rs 9.0648
c 0
b 0
f 0
cc 5
nc 4
nop 2
crap 5
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 = $this->getEnvironment($input, $output);
61
        if ($environment === null) {
62
            return self::CODE_ERROR;
63
        }
64
65
        $envOptions = $this->getConfig()->getEnvironment($environment);
66
        if (!$this->checkEnvironmentOptions($envOptions, $output)) {
67
            return self::CODE_ERROR;
68
        }
69 4
70
        $start = microtime(true);
71 4
72
        if (empty($seedSet)) {
73 4
            // run all the seed(ers)
74 4
            $this->getManager()->seed($environment);
75
        } else {
76 4
            // run seed(ers) specified in a comma-separated list of classes
77 3
            foreach ($seedSet as $seed) {
0 ignored issues
show
Bug introduced by
The expression $seedSet of type string|array<integer,string>|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
78 3
                $this->getManager()->seed($environment, trim($seed));
79 3
            }
80 1
        }
81
82
        $end = microtime(true);
83 4
84 4
        $output->writeln('');
85 3
        $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
86 3
87
        return self::CODE_SUCCESS;
88 4
    }
89
}
90