Completed
Push — master ( 4593f6...7b4260 )
by Andrii
36:14
created

InstallGoal::detectBin()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
rs 8.5126
cc 5
eloc 9
nc 6
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-2015, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\goals;
13
14
use Yii;
15
use yii\base\InvalidConfigException;
16
17
/**
18
 * Install goal.
19
 */
20
class InstallGoal extends DefaultGoal
21
{
22
    public function actionMake()
23
    {
24
        Yii::warning('install');
25
    }
26
27
    public function actionUpdate()
28
    {
29
        exec('cd .hidev;composer update --prefer-source');
30
        $this->module->runRequest('');
31
    }
32
33
    protected $_bins;
34
35
    public function getBin($prog)
36
    {
37
        if ($this->_bins[$prog] === null) {
38
            $this->_bins[$prog] = $this->detectBin($prog);
39
        }
40
41
        return $this->_bins[$prog];
42
    }
43
44
    /**
45
     * Detects how to run given prog.
46
     * Searches in this order:
47
     * 1. prog.phar in project root directory
48
     * 2. ./vendor/bin/prog
49
     * 3. $HOME/.composer/vendor/bin/prog
50
     * 4. `which $prog`.
51
     *
52
     * @param string $prog
53
     * @return string path to the binary prepended with `env php` when found file is not executable
54
     */
55
    public function detectBin($prog)
0 ignored issues
show
Coding Style introduced by
detectBin 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...
56
    {
57
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
        $pkg = $this->getItem('bin')[$prog];
59
        if (!$pkg) {
60
            throw new InvalidConfigException("Unknown bin: $prog");
61
        }
62
        $path = $this->package->hasRequireAny($pkg) ? './vendor/bin' : '$HOME/.composer/vendor/bin';
63
        */
64
65
        $paths = [Yii::getAlias("@source/$prog.phar"), Yii::getAlias("@source/vendor/bin/$prog"), "$_SERVER[HOME]/.composer/vendor/bin/$prog"];
66
        foreach ($paths as $path) {
67
            if (file_exists($path)) {
68
                return is_executable($path) ? $path : 'env php ' . $path;
69
            }
70
        }
71
72
        $path = exec('which ' . $prog);
73
        if ($path) {
74
            return $prog;
75
        }
76
77
        throw new InvalidConfigException("Failed to find how to run $prog");
78
    }
79
}
80