Completed
Push — master ( dde429...22637e )
by Andrii
20:00
created

BinaryPhp::detectPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\base;
13
14
use Yii;
15
16
class BinaryPhp extends Binary
17
{
18
    /**
19
     * @var string package full name, e.g. fabpot/php-cs-fixer
20
     */
21
    public $package;
22
23
    /**
24
     * @var string package version constraint, e.g. ^1.1
25
     */
26
    public $version;
27
28
    /**
29
     * @var string installer URL
30
     */
31
    public $installer;
32
33
    /**
34
     * @var string URL to download PHAR
35
     */
36
    public $phar_url;
37
38
    /**
39
     * Detects how to run the binary.
40
     * Searches in this order:
41
     * 1. name.phar in project root directory
42
     * 2. ./vendor/bin/name
43
     * 3. $HOME/.composer/vendor/bin/name.
44
     *
45
     * @param string $name
46
     * @return string path to the binary
47
     */
48
    public function detectPath($name)
0 ignored issues
show
Coding Style introduced by
detectPath uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
49
    {
50
        $paths = [Yii::getAlias("@prjdir/$name.phar"), Yii::getAlias("@prjdir/vendor/bin/$name"), "$_SERVER[HOME]/.composer/vendor/bin/$name"];
51
        foreach ($paths as $path) {
52
            if (file_exists($path)) {
53
                return $path;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $path; of type string|boolean is incompatible with the return type of the parent method hidev\base\Binary::detectPath of type string|null as it can also be of type boolean which is not included in this return type.
Loading history...
54
            }
55
        }
56
57
        return parent::detectPath($name);
58
    }
59
60
    public function detectCommand($path)
61
    {
62
        $path = parent::detectCommand($path);
63
64
        return is_executable($path) ? $path : '/usr/bin/env php ' . $path;
65
    }
66
}
67