Completed
Push — master ( a20e35...2a3e96 )
by Greg
03:21
created

src/Task/Testing/Behat.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo\Task\Testing;
3
4
use Robo\Contract\CommandInterface;
5
use Robo\Contract\PrintedInterface;
6
use Robo\Task\BaseTask;
7
8
/**
9
 * Executes Behat tests
10
 *
11
 * ``` php
12
 * <?php
13
 * $this->taskBehat()
14
 *      ->format('pretty')
15
 *      ->noInteraction()
16
 *      ->run();
17
 * ?>
18
 * ```
19
 *
20
 */
21
class Behat extends BaseTask implements CommandInterface, PrintedInterface
22
{
23
    use \Robo\Common\ExecOneCommand;
24
25
    /**
26
     * @var string
27
     */
28
    protected $command;
29
30
    /**
31
     * @var string[] $formaters available formaters for format option
32
     */
33
    protected $formaters = ['progress', 'pretty', 'junit'];
34
35
    /**
36
     * @var string[] $verbose_levels available verbose levels
37
     */
38
    protected $verbose_levels = ['v', 'vv'];
39
40
    /**
41
     * Behat constructor.
42
     *
43
     * @param null|string $pathToBehat
44
     *
45
     * @throws \Robo\Exception\TaskException
46
     */
47 View Code Duplication
    public function __construct($pathToBehat = null)
0 ignored issues
show
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...
48
    {
49
        $this->command = $pathToBehat;
50
        if (!$this->command) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->command of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
51
            $this->command = $this->findExecutable('behat');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->findExecutable('behat') can also be of type false. However, the property $command is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
52
        }
53
        if (!$this->command) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->command of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
54
            throw new \Robo\Exception\TaskException(__CLASS__, "Neither composer nor phar installation of Behat found");
55
        }
56
    }
57
58
    /**
59
     * @return $this
60
     */
61
    public function stopOnFail()
62
    {
63
        $this->option('stop-on-failure');
64
        return $this;
65
    }
66
67
    /**
68
     * @return $this
69
     */
70
    public function noInteraction()
71
    {
72
        $this->option('no-interaction');
73
        return $this;
74
    }
75
76
    /**
77
     * @param $config_file
78
     *
79
     * @return $this
80
     */
81
    public function config($config_file)
82
    {
83
        $this->option('config', $config_file);
84
        return $this;
85
    }
86
87
    /**
88
     * @return $this
89
     */
90
    public function colors()
91
    {
92
        $this->option('colors');
93
        return $this;
94
    }
95
96
    /**
97
     * @return $this
98
     */
99
    public function noColors()
100
    {
101
        $this->option('no-colors');
102
        return $this;
103
    }
104
105
    /**
106
     * @param string $suite
107
     *
108
     * @return $this
109
     */
110
    public function suite($suite)
111
    {
112
        $this->option('suite', $suite);
113
        return $this;
114
    }
115
116
    /**
117
     * @param string $level
118
     *
119
     * @return $this
120
     */
121 View Code Duplication
    public function verbose($level = 'v')
0 ignored issues
show
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...
122
    {
123
        if (!in_array($level, $this->verbose_levels)) {
124
            throw new \InvalidArgumentException('expected ' . implode(',', $this->verbose_levels));
125
        }
126
        $this->option('-' . $level);
127
        return $this;
128
    }
129
130
    /**
131
     * @param string $formater
132
     *
133
     * @return $this
134
     */
135 View Code Duplication
    public function format($formater)
0 ignored issues
show
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...
136
    {
137
        if (!in_array($formater, $this->formaters)) {
138
            throw new \InvalidArgumentException('expected ' . implode(',', $this->formaters));
139
        }
140
        $this->option('format', $formater);
141
        return $this;
142
    }
143
144
    /**
145
     * Returns command that can be executed.
146
     * This method is used to pass generated command from one task to another.
147
     *
148
     * @return string
149
     */
150
    public function getCommand()
151
    {
152
        return $this->command . $this->arguments;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function run()
159
    {
160
        $this->printTaskInfo('Running behat {arguments}', ['arguments' => $this->arguments]);
161
        return $this->executeCommand($this->getCommand());
162
    }
163
}
164