FixturesCommand::execute()   F
last analyzed

Complexity

Conditions 14
Paths 1152

Size

Total Lines 60
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 34
nc 1152
nop 2
dl 0
loc 60
rs 2.1
c 0
b 0
f 0

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
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Shared\Testify\Fixtures;
9
10
use Codeception\Codecept;
11
use Codeception\Command\Run;
12
use Codeception\Command\Shared\ConfigTrait;
13
use Codeception\Configuration;
14
use Codeception\CustomCommandInterface;
15
use Codeception\Exception\TestRuntimeException;
16
use PHPUnit\Runner\Version;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class FixturesCommand extends Run implements CustomCommandInterface
22
{
23
    use ConfigTrait;
24
25
    /**
26
     * @var int
27
     */
28
    public const CODE_SUCCESS = 0;
29
30
    /**
31
     * @return string
32
     */
33
    public static function getCommandName(): string
34
    {
35
        return 'fixtures';
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getDescription(): string
42
    {
43
        return 'Builds fixtures and serializes them into files';
44
    }
45
46
    /**
47
     * Sets Run arguments
48
     *
49
     * @return void
50
     */
51
    protected function configure(): void
52
    {
53
        $this->setDefinition([
54
            new InputOption(
55
                'no-colors',
56
                '',
57
                InputOption::VALUE_NONE,
58
                'Force no colors in output (useful to override config file)',
59
            ),
60
            new InputOption('silent', '', InputOption::VALUE_NONE, 'Only outputs suite names and final results'),
61
            new InputOption('steps', '', InputOption::VALUE_NONE, 'Show steps in output'),
62
            new InputOption('debug', 'd', InputOption::VALUE_NONE, 'Show debug and scenario output'),
63
            new InputOption('no-exit', '', InputOption::VALUE_NONE, 'Don\'t finish with exit code'),
64
            new InputOption(
65
                'skip',
66
                's',
67
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
68
                'Skip selected suites',
69
            ),
70
            new InputOption(
71
                'group',
72
                'g',
73
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
74
                'Groups of fixtures to be build',
75
            ),
76
            new InputOption(
77
                'skip-group',
78
                'x',
79
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
80
                'Skip selected groups',
81
            ),
82
            new InputOption(
83
                'env',
84
                '',
85
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
86
                'Run tests in selected environments.',
87
            ),
88
            new InputOption(
89
                'seed',
90
                '',
91
                InputOption::VALUE_REQUIRED,
92
                'Define random seed for shuffle setting',
93
            ),
94
        ]);
95
    }
96
97
    /**
98
     * @param \Symfony\Component\Console\Input\InputInterface $input
99
     * @param \Symfony\Component\Console\Output\OutputInterface $output
100
     *
101
     * @throws \Codeception\Exception\TestRuntimeException
102
     *
103
     * @return int
104
     */
105
    public function execute(InputInterface $input, OutputInterface $output): int
106
    {
107
        $this->options = $input->getOptions();
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
108
        $this->output = $output;
0 ignored issues
show
Bug Best Practice introduced by
The property output does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
109
110
        // load config
111
        $config = $this->getGlobalConfig();
112
113
        if (!$this->options['silent']) {
114
            $output->writeln(
115
                Codecept::versionString() . "\nPowered by " . Version::getVersionString(),
116
            );
117
            $output->writeln(
118
                'Running with seed: ' . $this->options['seed'] . "\n",
119
            );
120
        }
121
        if ($this->options['group']) {
122
            $output->writeln(sprintf('[Groups] <info>%s</info> ', implode(', ', $this->options['group'])));
123
        }
124
        if ($this->options['debug']) {
125
            $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
126
        }
127
128
        $userOptions = array_intersect_key($this->options, array_flip($this->passedOptionKeys($input)));
129
        $userOptions['verbosity'] = $output->getVerbosity();
130
        $userOptions['seed'] = (int)$this->options['seed'] ?: mt_rand();
131
        $userOptions['colors'] = $this->options['no-colors'] || ($input->hasOption('no-ansi') && $input->getOption('no-ansi')) ? false : $config['settings']['colors'];
132
133
        if ($this->options['group'] !== []) {
134
            $userOptions['groups'] = $this->options['group'];
135
        }
136
137
        if ($this->options['skip-group'] !== []) {
138
            $userOptions['excludeGroups'] = $this->options['skip-group'];
139
        }
140
141
        $this->codecept = new FixturesRunner($userOptions);
0 ignored issues
show
Bug Best Practice introduced by
The property codecept does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
142
143
        $suites = Configuration::suites();
144
        $this->executed = $this->runSuites($suites);
0 ignored issues
show
Bug Best Practice introduced by
The property executed does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
145
146
        if (!empty($config['include'])) {
147
            $current_dir = Configuration::projectDir();
148
            $suites += $config['include'];
149
            $this->runIncludedSuites($config['include'], $current_dir);
150
        }
151
152
        if ($this->executed === 0) {
153
            throw new TestRuntimeException(
154
                sprintf("Suite '%s' could not be found", implode(', ', $suites)),
155
            );
156
        }
157
158
        $this->codecept->printResult();
159
160
        if (!$input->getOption('no-exit') && !$this->codecept->getResultAggregator()->wasSuccessful()) {
161
            exit(1);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return integer. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
162
        }
163
164
        return static::CODE_SUCCESS;
165
    }
166
}
167