Completed
Pull Request — master (#4)
by Nicolai
06:49
created

GenerateCest::handle()   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 4
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Console\Command;
5
6
use Exception;
7
use SmartWeb\ModuleTesting\Codeception\SmartWeb\Setup;
8
use SmartWeb\ModuleTesting\Console\CommandArgument;
9
use SmartWeb\ModuleTesting\Console\CommandArguments;
10
use SmartWeb\ModuleTesting\Console\InputArgumentType;
11
use SmartWeb\ModuleTesting\Generator\Test\CestGenerator;
12
13
/**
14
 * Command for generating a Codeception cest for a Module.
15
 *
16
 * @package SmartWeb\ModuleTesting\Console
17
 */
18
class GenerateCest extends BaseCommand
19
{
20
    
21
    /**
22
     * @inheritDoc
23
     */
24
    public function __construct()
25
    {
26
        parent::__construct('make-cest', 'Generate a Codeception Cest for the given module and suite');
27
    }
28
    
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return void
33
     *
34
     * @throws Exception
35
     */
36
    public function handle()
37
    {
38
        /**
39
         * @var string $moduleName
40
         * @var string $suite
41
         * @var string $name
42
         */
43
        $moduleName = $this->argument('moduleName');
44
        $suite = $this->argument('suite');
45
        $name = $this->argument('name');
46
        
47
        $this->info("Creating '{$name}' for module '{$moduleName}' in suite '{$suite}'.");
48
        
49
        $setup = new Setup($moduleName);
50
        
51
        try {
52
            $generator = new CestGenerator($setup, $suite, $name);
0 ignored issues
show
Bug introduced by
It seems like $suite defined by $this->argument('suite') on line 44 can also be of type array; however, SmartWeb\ModuleTesting\G...enerator::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $name defined by $this->argument('name') on line 45 can also be of type array; however, SmartWeb\ModuleTesting\G...enerator::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
            $generator->generate();
54
            $this->info("Created test!");
55
        } catch (Exception $e) {
56
            $this->error("Failed to create test!");
57
            throw $e;
58
        }
59
    }
60
    
61
    /**
62
     * @return CommandArguments
63
     */
64
    protected function provideArguments() : CommandArguments
65
    {
66
        return new CommandArguments([
67
            new CommandArgument('moduleName', InputArgumentType::required(), 'The name of the module.'),
68
            new CommandArgument('suite', InputArgumentType::required(), 'The suite to generate test in.'),
69
            new CommandArgument('name', InputArgumentType::required(), 'The name of the test.'),
70
        ]);
71
    }
72
    
73
}
74