Completed
Push — master ( de4386...03650d )
by Andrii
04:05
created

StartController::includeAll()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 8.8571
cc 5
eloc 8
nc 5
nop 0
crap 30
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->addAutoloader();
44
        $this->requireAll();
45
        $this->includeAll();
46
        self::$started = true;
47
    }
48
49
    public function addAutoloader()
50
    {
51
        $autoloader = Yii::getAlias('@prjdir/vendor/autoload.php');
52
        if (file_exists($autoloader)) {
53
            spl_autoload_unregister(['Yii', 'autoload']);
54
            require $autoloader;
55
            spl_autoload_register(['Yii', 'autoload'], true, true);
56
        }
57
    }
58
59
    /**
60
     * Update action.
61
     * @return int exit code
62
     */
63
    public function actionUpdate()
64
    {
65
        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...
66
    }
67
68
    /**
69
     * Adds aliases:
70
     * - @prjdir alias to current project root dir
71
     * - current package namespace for it could be used from hidev.
72
     */
73
    public function addAliases()
74
    {
75
        Yii::setAlias('@prjdir', $this->findPrjDir());
76
        $config = $this->takeConfig()->rawItem('package');
77
        $alias  = '@' . strtr($config['namespace'], '\\', '/');
78
        if ($alias && !Yii::getAlias($alias, false)) {
79
            $srcdir = Yii::getAlias('@prjdir/' . ($config['src'] ?: 'src'));
80
            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 79 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...
81
        }
82
    }
83
84
    /**
85
     * Require all configured requires.
86
     */
87
    protected function requireAll()
88
    {
89
        $require = $this->takeConfig()->rawItem('require');
90
        if ($require) {
91
            $require['hiqdev/composer-extension-plugin'] = '*@dev';
92
            $saved = File::create('.hidev/composer.json')->save(compact('require'));
93
            if ($saved || !is_dir('.hidev/vendor')) {
94
                $this->runAction('update');
95
            }
96
            /// backup config then reset with extra config then restore
97
            $config = $this->takeConfig()->getItems();
98
            Yii::$app->clear('config');
99
            Yii::$app->loadExtraVendor('.hidev/vendor');
100
            $this->takeConfig()->mergeItems($config);
101
        }
102
    }
103
104
    /**
105
     * Include all configs.
106
     */
107
    public function includeAll()
108
    {
109
        $still = true;
110
        while ($still) {
111
            $still = false;
112
            $include = $this->takeConfig()->rawItem('include');
113
            if ($include) {
114
                foreach ($include as $path) {
115
                    $still = $still || $this->takeConfig()->includeConfig($path);
116
                }
117
            }
118
        }
119
    }
120
121
    /**
122
     * Chdirs to project's root by looking for config file in the current directory and up.
123
     * @throws InvalidParamException when failed to find
124
     * @return string path to the root directory of hidev project
125
     */
126
    protected function findPrjDir()
127
    {
128
        $configDir = '.hidev';
129
        for ($i = 0;$i < 9;++$i) {
130
            if (is_dir($configDir)) {
131
                $this->prjdir = getcwd();
132
                return $this->prjdir;
133
            }
134
            chdir('..');
135
        }
136
        throw new InvalidParamException("Not a hidev project (or any of the parent directories).\nUse `hidev init` to initialize hidev project.");
137
    }
138
}
139