Completed
Push — master ( d3a073...5737c8 )
by Greg
02:21
created

src/Task/Bower/Base.php (4 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\Bower;
3
4
use Robo\Task\BaseTask;
5
use Robo\Exception\TaskException;
6
7
abstract class Base extends BaseTask
8
{
9
    use \Robo\Common\ExecOneCommand;
10
11
    protected $opts = [];
12
    protected $action = '';
13
14
    /**
15
     * @var string
16
     */
17
    protected $command = '';
18
19
    /**
20
     * adds `allow-root` option to bower
21
     *
22
     * @return $this
23
     */
24
    public function allowRoot()
25
    {
26
        $this->option('allow-root');
27
        return $this;
28
    }
29
30
    /**
31
     * adds `force-latest` option to bower
32
     *
33
     * @return $this
34
     */
35
    public function forceLatest()
36
    {
37
        $this->option('force-latest');
38
        return $this;
39
    }
40
41
    /**
42
     * adds `production` option to bower
43
     *
44
     * @return $this
45
     */
46
    public function noDev()
47
    {
48
        $this->option('production');
49
        return $this;
50
    }
51
52
    /**
53
     * adds `offline` option to bower
54
     *
55
     * @return $this
56
     */
57
    public function offline()
58
    {
59
        $this->option('offline');
60
        return $this;
61
    }
62
63
    /**
64
     * Base constructor.
65
     *
66
     * @param null|string $pathToBower
67
     *
68
     * @throws \Robo\Exception\TaskException
69
     */
70 View Code Duplication
    public function __construct($pathToBower = 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...
71
    {
72
        $this->command = $pathToBower;
73
        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...
74
            $this->command = $this->findExecutable('bower');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->findExecutable('bower') 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...
75
        }
76
        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...
77
            throw new TaskException(__CLASS__, "Bower executable not found.");
78
        }
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getCommand()
85
    {
86
        return "{$this->command} {$this->action}{$this->arguments}";
87
    }
88
}
89