Completed
Push — master ( 5955f6...652d4a )
by Andrii
03:36
created

BinaryPhp::getVcsignore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Automation tool mixed with code generator for easier continuous development
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\base;
13
14
use Yii;
15
16
class BinaryPhp extends Binary
17
{
18
    /**
19
     * Detects how to run the binary.
20
     * Searches in this order:
21
     * 1. PHAR in project's root directory
22
     * 2. projects's vendor/bin directory
23
     * 3. composer global vendor/bin directory.
24
     *
25
     * @param string $name
26
     * @return string path to the binary
27
     */
28
    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...
29
    {
30
        $paths = [Yii::getAlias("@root/$name.phar", false), Yii::getAlias("@root/vendor/bin/$name", false), "$_SERVER[HOME]/.composer/vendor/bin/$name"];
31
        foreach ($paths as $path) {
32
            if (file_exists($path)) {
33
                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...
34
            }
35
        }
36
37
        return parent::detectPath($name);
38
    }
39
40
    /**
41
     * Detect command execution string.
42
     * @param mixed $path
43
     * @return string
44
     */
45
    public function detectCommand($path)
46
    {
47
        $path = parent::detectCommand($path);
48
49
        return is_executable($path) ? $path : '/usr/bin/env php ' . $path;
50
    }
51
52
    public function install()
53
    {
54
        if ($this->installer) {
55
            passthru('/usr/bin/env wget ' . escapeshellarg($this->installer) . ' -O- | /usr/bin/env php', $exitcode);
56
        } elseif ($this->download) {
57
            $dest = Yii::getAlias('@root/' . $this->name . '.phar', false);
58
            passthru('/usr/bin/env wget ' . escapeshellarg($this->download) . ' -O ' . $dest, $exitcode);
59
        } else {
60
            return parent::install();
61
        }
62
63
        return $exitcode;
64
    }
65
66
    public function getVcsignore()
67
    {
68
        return $this->name . '.phar';
69
    }
70
}
71