Completed
Push — master ( a0f8dd...4bb285 )
by Andrii
13:40
created

ConfigFile::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\base;
12
13
use hidev\helpers\Helper;
14
use Yii;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * Configuration file component.
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class ConfigFile extends \hidev\base\Component implements \yii\base\Arrayable, \ArrayAccess, \IteratorAggregate
22
{
23
    use \hiqdev\yii2\collection\ObjectTrait;
24
25
    /**
26
     * @var string specifies handler to be used
27
     */
28
    public $fileType;
29
30
    /**
31
     * @var array|File the file to be handled
32
     */
33
    protected $_file;
34
35
    /**
36
     * @var string path to copy from
37
     */
38
    protected $_copy;
39
40
    /**
41
     * @var string the path to the file
42
     */
43
    protected $_path;
44
45
    /**
46
     * @var string the template name
47
     */
48
    protected $_template;
49
50
    public function init()
51
    {
52
        $this->load();
53
    }
54
55
    /**
56
     * Template setter.
57
     * @param string $template name
58
     */
59
    public function setTemplate($template)
60
    {
61
        $this->_template = $template;
62
    }
63
64
    /**
65
     * Template getter.
66
     */
67
    public function getTemplate()
68
    {
69
        return Helper::file2template($this->_template ?: $this->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hidev\base\ConfigFile>. 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...
70
    }
71
72
    /**
73
     * Returns the file object.
74
     * Instantiates it if necessary.
75
     * @return File
76
     */
77
    public function getFile()
78
    {
79
        if (!is_object($this->_file)) {
80
            $this->_file = Yii::createObject(array_merge([
81
                'class'    => File::class,
82
                'template' => $this->getTemplate(),
83
                'goal'     => $this,
84
                'path'     => $this->_path ?: $this->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hidev\base\ConfigFile>. 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...
85
            ], is_string($this->_file)
86
                ? ['path' => $this->_file]
87
                : (array) $this->_file
88
            ));
89
        }
90
91
        return $this->_file;
92
    }
93
94
    /**
95
     * Sets file with given info.
96
     * @param mixed $info could be anything that is good for File::create
97
     */
98
    public function setFile($info)
99
    {
100
        $this->_file = $info;
101
    }
102
103
    /**
104
     * Sets the path to the file, but file info has precendence.
105
     * @param string $value
106
     */
107
    public function setPath($value)
108
    {
109
        $this->_path = $value;
110
    }
111
112
    /**
113
     * Copy getter. Processes aliases.
114
     */
115
    public function getCopy()
116
    {
117
        return Yii::getAlias($this->_copy);
118
    }
119
120
    /**
121
     * Dirname getter.
122
     */
123
    public function getDirname()
124
    {
125
        return $this->getFile()->getDirname();
126
    }
127
128
    /**
129
     * Path getter.
130
     */
131
    public function getPath()
132
    {
133
        return $this->getFile()->getPath();
134
    }
135
136
    /**
137
     * Checks if the file exists.
138
     */
139
    public function exists()
140
    {
141
        return $this->getFile()->exists();
142
    }
143
144
    /**
145
     * Read the file.
146
     */
147
    public function read()
148
    {
149
        return $this->getFile()->read();
150
    }
151
152
    /**
153
     * Read the file into array.
154
     * @return array
155
     */
156
    public function readArray()
157
    {
158
        return $this->getFile()->readArray();
159
    }
160
161
    public function load()
162
    {
163
        $data = $this->getFile()->load() ?: [];
164
        if ($data) { /// TODO think what's better
165
        //  $this->setItems(ArrayHelper::merge($data, $this->toArray()));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
166
            $this->setItems(ArrayHelper::merge($this->toArray(), $data));
167
        //  $this->setItems($data);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
168
        }
169
    }
170
171
    /**
172
     * Save the file.
173
     */
174
    public function save()
175
    {
176
        if ($this->once && $this->exists()) {
0 ignored issues
show
Documentation introduced by
The property once does not exist on object<hidev\base\ConfigFile>. 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...
177
            return 0;
178
        }
179
        $this->_items = Helper::uniqueConfig($this->_items);
180
        $this->getFile()->save($this);
181
    }
182
}
183