Completed
Push — master ( f6cc4f...20183c )
by Andrii
03:46
created

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