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

ConfigGoal::goal2class()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 6
c 3
b 0
f 0
rs 9.4286
cc 3
eloc 3
nc 4
nop 2
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 hidev\helpers\Helper;
15
use Yii;
16
use yii\base\BootstrapInterface;
17
use yii\base\InvalidParamException;
18
use yii\helpers\ArrayHelper;
19
20
/**
21
 * The Config. Keeps config and Goals.
22
 */
23
class ConfigGoal extends FileGoal implements BootstrapInterface
24
{
25
    //protected $_itemClass = 'hiqdev\collection\Manager';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
26
27
    /**
28
     * @param array $config name-value pairs that will be used to initialize the object properties.
29
     */
30
    public function __construct($config = [])
31
    {
32
        parent::__construct('config', Yii::$app, $config);
33
    }
34
35
    public $types = ['yaml', 'json'];
36
37
    public $_file = '.hidev/config.yml';
38
39
    public function hasGoal($name)
40
    {
41
        return $this->hasItem($name) || class_exists($this->getGoalClass($name));
42
    }
43
44
    public function getGoalClass($name)
45
    {
46
        return $this->getItemConfig($name)['class'];
47
    }
48
49
    public function getGoal($name)
50
    {
51
        return $this->getItem($name);
52
    }
53
54
    public static function findGoal($name)
55
    {
56
        $config = Yii::$app->pluginManager->get('goals')[$name];
57
        return is_scalar($config) ? ['class' => $config] : (array) $config;
58
    }
59
60
    public function getItemConfig($name = null, array $config = [])
61
    {
62
        $config = ArrayHelper::merge([
63
            'goalName' => $name,
64
            'class'    => 'hidev\goals\DefaultGoal',
65
        ], static::findGoal($name), $config);
66
67
        return $config;
68
    }
69
70
    protected function createItem($name, array $config = [])
71
    {
72
        #return Yii::createObject($this->getItemConfig($name, $config), [$name, $this->module]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% 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...
73
        return Yii::createObject($this->getItemConfig($name, $config), [$id, $this->module]);
0 ignored issues
show
Bug introduced by
The variable $id 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...
74
    }
75
76
    public function getItem($name)
77
    {
78
        if ($name === 'default') {
79
            return $this;
80
        }
81
        $item = &$this->_items[$name];
82
        if (is_array($item) || is_null($item)) {
83
            $item = $this->createItem($name, $item ?: []);
84
        }
85
86
        return $item;
87
    }
88
89
    /**
90
     * Bootstraps config. Reads or creates if doesn't exist.
91
     *
92
     * @param yii\base\Application $app application
93
     */
94
    public function bootstrap($app)
95
    {
96
        if ($app->isInit) {
97
            return;
98
        }
99
        if (!$this->file->find($this->types)) {
0 ignored issues
show
Documentation introduced by
The property file does not exist on object<hidev\goals\ConfigGoal>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
100
            throw new InvalidParamException('No config found. Use hidev init vendor/package');
101
        }
102
        if ($app->pluginManager->configFiles) {
103
            foreach ($app->pluginManager->configFiles as $path) {
104
                $this->includeConfig($path);
105
            }
106
        }
107
        $this->actionLoad();
108
        if ($this->has('include')) {
109
            foreach (Helper::csplit($this->rawItem('include')) as $path) {
110
                $this->includeConfig($path);
111
            }
112
        }
113
    }
114
115
    public function includeConfig($path)
116
    {
117
        $file = Yii::createObject(array_merge([
118
            'class' => 'hidev\base\File',
119
        ], is_array($path) ? $path : compact('path')));
120
        $this->setItems($file->load());
121
    }
122
}
123