Completed
Push — master ( 4956d3...0f3d33 )
by Andrii
04:29
created

StartController::addAliases()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.2
cc 4
eloc 7
nc 2
nop 0
crap 20
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) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
use hidev\base\File;
15
use Yii;
16
use yii\base\InvalidParamException;
17
18
/**
19
 * Start goal.
20
 * Chdirs to the project's root directory and loads dependencies and configs.
21
 */
22
class StartController extends CommonController
23
{
24
    const MAIN_CONFIG = '.hidev/config.yml';
25
26
    /**
27
     * @var string absolute path to the project root directory
28
     */
29
    public $prjdir;
30
31
    /**
32
     * @var bool hidev already started flag
33
     */
34
    public static $started = false;
35
36
    /**
37
     * Make action.
38
     */
39
    public function actionMake()
40
    {
41
        $this->takeConfig()->includeConfig(static::MAIN_CONFIG);
42
        $this->addAliases();
43
        $this->requireAll();
44
        $this->includeAll();
45
        self::$started = true;
46
    }
47
48
    /**
49
     * Update action.
50
     * @return int exit code
51
     */
52
    public function actionUpdate()
53
    {
54
        return $this->passthru('composer', ['update', '-d', '.hidev', '--prefer-source', '--ansi']);
0 ignored issues
show
Documentation introduced by
array('update', '-d', '....efer-source', '--ansi') is of type array<integer,string,{"0..."string","4":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
    }
56
57
    /**
58
     * Adds aliases:
59
     * - @prjdir alias to current project root dir
60
     * - current package namespace for it could be used from hidev.
61
     */
62
    public function addAliases()
63
    {
64
        Yii::setAlias('@prjdir', $this->findPrjDir());
65
        $config = $this->takeConfig()->rawItem('package');
66
        $alias  = '@' . strtr($config['namespace'], '\\', '/');
67
        if ($alias && !Yii::getAlias($alias, false)) {
68
            $srcdir = Yii::getAlias('@prjdir/' . ($config['src'] ?: 'src'));
69
            Yii::setAlias($alias, $srcdir);
0 ignored issues
show
Bug introduced by
It seems like $srcdir defined by \Yii::getAlias('@prjdir/...onfig['src'] ?: 'src')) on line 68 can also be of type boolean; however, yii\BaseYii::setAlias() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
70
        }
71
    }
72
73
    /**
74
     * Require all configured requires.
75
     */
76
    protected function requireAll()
77
    {
78
        $require = $this->takeConfig()->rawItem('require');
79
        if ($require) {
80
            $require['hiqdev/composer-extension-plugin'] = '*@dev';
81
            $saved = File::create('.hidev/composer.json')->save(compact('require'));
82
            if ($saved || !is_dir('.hidev/vendor')) {
83
                $this->runAction('update');
84
            }
85
            /// backup config then reset with extra config then restore
86
            $config = $this->takeConfig()->getItems();
87
            Yii::$app->clear('config');
88
            Yii::$app->loadExtraVendor('.hidev/vendor');
89
            $this->takeConfig()->mergeItems($config);
90
        }
91
    }
92
93
    /**
94
     * Include all configs.
95
     */
96
    public function includeAll()
97
    {
98
        $still = true;
99
        while ($still) {
100
            $still = false;
101
            $include = $this->takeConfig()->rawItem('include');
102
            if ($include) {
103
                foreach ($include as $path) {
104
                    $still = $still || $this->takeConfig()->includeConfig($path);
105
                }
106
            }
107
        }
108
    }
109
110
    /**
111
     * Chdirs to project's root by looking for config file in the current directory and up.
112
     * @throws InvalidParamException when failed to find
113
     * @return string path to the root directory of hidev project
114
     */
115
    protected function findPrjDir()
116
    {
117
        $configDir = '.hidev';
118
        for ($i = 0;$i < 9;++$i) {
119
            if (is_dir($configDir)) {
120
                $this->prjdir = getcwd();
121
                return $this->prjdir;
122
            }
123
            chdir('..');
124
        }
125
        throw new InvalidParamException("Not a hidev project (or any of the parent directories).\nUse `hidev init` to initialize hidev project.");
126
    }
127
}
128