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

src/base/BinaryPython.php (3 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 BinaryPython extends Binary
16
{
17
    /**
18
     * Detects how to run the binary.
19
     * Searches in this order:
20
     * 1. exexcutable in project's root directory
21
     * 2. XXX ??? vendor directory
22
     * 3. /home/user/.local/bin.
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 = [
29
            Yii::getAlias("@root/$name", false),
30
            Yii::getAlias("@root/env/bin/$name", false),
31
            "$_SERVER[HOME]/.local/bin/$name",
32
        ];
33
        foreach ($paths as $path) {
34
            if (file_exists($path)) {
35
                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...
36
            }
37
        }
38
39
        return parent::detectPath($name);
40
    }
41
42
    /**
43
     * Detect command execution string.
44
     * @param mixed $path
45
     * @return string
46
     */
47
    public function detectCommand($path)
48
    {
49
        $path = parent::detectCommand($path);
50
51
        return is_executable($path) ? $path : '/usr/bin/env python ' . $path;
52
    }
53
54
    public function install()
0 ignored issues
show
install 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...
55
    {
56
        if ($this->installer) {
57
            passthru('/usr/bin/env wget ' . escapeshellarg($this->installer) . ' -O- | /usr/bin/env python', $exitcode);
58
        } elseif ($this->download) {
59
            $dest = Yii::getAlias('@root/' . $this->name, false);
60
            passthru('/usr/bin/env wget ' . escapeshellarg($this->download) . ' -O ' . $dest, $exitcode);
61
        } else {
62
            $args = ['install'];
63
            if (!$_SERVER['VIRTUAL_ENV']) {
64
                $args[] = '--user';
65
            }
66
            $args[] = $this->package;
67
            $exitcode = $this->controller->passthru('pip', $args);
68
        }
69
70
        return $exitcode;
71
    }
72
}
73