Completed
Push — master ( dde429...22637e )
by Andrii
20:00
created

StartGoal::loadExtensions()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.6846
cc 4
eloc 18
nc 5
nop 0
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-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\goals;
13
14
use Yii;
15
use yii\base\InvalidConfigException;
16
17
/**
18
 * Goal that starts ...
19
 */
20
class StartGoal extends DefaultGoal
21
{
22
    public function actionPerform()
23
    {
24
        Yii::setAlias('@prjdir', $this->findDir());
25
        $this->loadExtensions();
26
    }
27
28
    protected function loadExtensions()
29
    {
30
            $require = Yii::createObject([
31
                'class' => 'hidev\base\File',
32
                'path'  => '.hidev/config.yml',
33
            ])->load()['require'];
34
            if ($require) {
35
                Yii::createObject([
36
                    'class' => 'hidev\base\File',
37
                    'path'  => '.hidev/composer.json',
38
                ])->save(compact('require'));
39
                if (!is_dir('.hidev/vendor')) {
40
                    exec('cd .hidev;composer update --prefer-source');
41
                }
42
                $main  = Yii::getAlias('@vendor');
43
                $local = realpath('./.hidev/vendor');
44
                if ($local !== $main) {
45
                    $this->extensions = array_merge(
0 ignored issues
show
Documentation introduced by
The property extensions does not exist on object<hidev\goals\StartGoal>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
                        $this->prepareExtensions($main),
0 ignored issues
show
Documentation Bug introduced by
The method prepareExtensions does not exist on object<hidev\goals\StartGoal>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
47
                        $this->prepareExtensions($local)
0 ignored issues
show
Documentation Bug introduced by
The method prepareExtensions does not exist on object<hidev\goals\StartGoal>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
48
                    );
49
                }
50
            }
51
    }
52
53
    /**
54
     * Looks for config file in the current directory and up.
55
     * @return string path to the root directory of hidev project
56
     * @throws InvalidConfigException when failed to find
57
     */
58
    protected function findDir()
59
    {
60
        $configDir = '.hidev';
61
        if (!$isInit) {
0 ignored issues
show
Bug introduced by
The variable $isInit does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
62
            for ($i = 0;$i < 9;++$i) {
63
                if (is_dir($configDir)) {
64
                    return getcwd();
65
                }
66
                chdir('..');
67
            }
68
        }
69
        throw new InvalidConfigException('Not a hidev project (or any of the parent directories). Use `hidev init` to initialize hidev project.');
70
    }
71
}
72