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

src/Task/Testing/Phpspec.php (1 issue)

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\PrintedInterface;
5
use Robo\Task\BaseTask;
6
use Robo\Contract\CommandInterface;
7
8
/**
9
 * Executes Phpspec tests
10
 *
11
 * ``` php
12
 * <?php
13
 * $this->taskPhpspec()
14
 *      ->format('pretty')
15
 *      ->noInteraction()
16
 *      ->run();
17
 * ?>
18
 * ```
19
 *
20
 */
21
class Phpspec 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', 'html', 'pretty', 'junit', 'dot', 'tap'];
34
35
    /**
36
     * @var array $verbose_levels available verbose levels
37
     */
38
    protected $verbose_levels = ['v', 'vv', 'vvv'];
39
40 View Code Duplication
    public function __construct($pathToPhpspec = null)
41
    {
42
        $this->command = $pathToPhpspec;
43
        if (!$this->command) {
44
            $this->command = $this->findExecutable('phpspec');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->findExecutable('phpspec') 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...
45
        }
46
        if (!$this->command) {
47
            throw new \Robo\Exception\TaskException(__CLASS__, "Neither composer nor phar installation of Phpspec found");
48
        }
49
        $this->arg('run');
50
    }
51
52
    public function stopOnFail()
53
    {
54
        $this->option('stop-on-failure');
55
        return $this;
56
    }
57
58
    public function noCodeGeneration()
59
    {
60
        $this->option('no-code-generation');
61
        return $this;
62
    }
63
64
    public function quiet()
65
    {
66
        $this->option('quiet');
67
        return $this;
68
    }
69
70 View Code Duplication
    public function verbose($level = 'v')
71
    {
72
        if (!in_array($level, $this->verbose_levels)) {
73
            throw new \InvalidArgumentException('expected ' . implode(',', $this->verbose_levels));
74
        }
75
        $this->option('-' . $level);
76
        return $this;
77
    }
78
79
    public function noAnsi()
80
    {
81
        $this->option('no-ansi');
82
        return $this;
83
    }
84
85
    public function noInteraction()
86
    {
87
        $this->option('no-interaction');
88
        return $this;
89
    }
90
91
    public function config($config_file)
92
    {
93
        $this->option('config', $config_file);
94
        return $this;
95
    }
96
97 View Code Duplication
    public function format($formater)
98
    {
99
        if (!in_array($formater, $this->formaters)) {
100
            throw new \InvalidArgumentException('expected ' . implode(',', $this->formaters));
101
        }
102
        $this->option('format', $formater);
103
        return $this;
104
    }
105
106
    public function getCommand()
107
    {
108
        return $this->command . $this->arguments;
109
    }
110
111
    public function run()
112
    {
113
        $this->printTaskInfo('Running phpspec {arguments}', ['arguments' => $this->arguments]);
114
        return $this->executeCommand($this->getCommand());
115
    }
116
}
117