Completed
Push — master ( 375de1...c13e1e )
by Andrii
13:21
created

ConfigFile::getFile()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.2
cc 4
eloc 11
nc 2
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\components;
12
13
use hidev\base\File;
14
use hidev\helpers\Helper;
15
use Yii;
16
use yii\helpers\ArrayHelper;
17
18
/**
19
 * Configuration file component.
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class ConfigFile extends \hidev\base\Component implements \yii\base\Arrayable, \ArrayAccess, \IteratorAggregate
23
{
24
    use \hiqdev\yii2\collection\ObjectTrait;
25
26
    /**
27
     * @var string specifies handler to be used
28
     */
29
    public $fileType;
30
31
    /**
32
     * @var bool Don't touch file if exists
33
     */
34
    public $once;
35
36
    /**
37
     * @var string Username to change file owner to
38
     */
39
    public $chown;
40
41
    /**
42
     * @var string Group to change file group to
43
     */
44
    public $chgrp;
45
46
    /**
47
     * @var string|integer Permissions to change to
48
     */
49
    public $chmod;
50
51
    /**
52
     * @var array|File the file to be handled
53
     */
54
    protected $_file;
55
56
    /**
57
     * @var string path to copy from
58
     */
59
    protected $_copy;
60
61
    /**
62
     * @var string the path to the file
63
     */
64
    protected $_path;
65
66
    /**
67
     * @var string the template name
68
     */
69
    protected $_template;
70
71
    public function init()
72
    {
73
        $this->load();
74
    }
75
76
    /**
77
     * Template setter.
78
     * @param string $template name
79
     */
80
    public function setTemplate($template)
81
    {
82
        $this->_template = $template;
83
    }
84
85
    /**
86
     * Template getter.
87
     */
88
    public function getTemplate()
89
    {
90
        return Helper::file2template($this->_template ?: $this->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hidev\components\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...
91
    }
92
93
    /**
94
     * Returns the file object.
95
     * Instantiates it if necessary.
96
     * @return File
97
     */
98 View Code Duplication
    public function getFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        if (!is_object($this->_file)) {
101
            $this->_file = Yii::createObject(array_merge([
102
                'class'    => File::class,
103
                'template' => $this->getTemplate(),
104
                'goal'     => $this,
105
                'path'     => $this->_path ?: $this->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hidev\components\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...
106
            ], is_string($this->_file)
107
                ? ['path' => $this->_file]
108
                : (array) $this->_file
109
            ));
110
        }
111
112
        return $this->_file;
113
    }
114
115
    /**
116
     * Sets file with given info.
117
     * @param mixed $info could be anything that is good for File::create
118
     */
119
    public function setFile($info)
120
    {
121
        $this->_file = $info;
122
    }
123
124
    /**
125
     * Sets the path to the file, but file info has precendence.
126
     * @param string $value
127
     */
128
    public function setPath($value)
129
    {
130
        $this->_path = $value;
131
    }
132
133
    /**
134
     * Copy setter. Turns this file type to `copy`.
135
     */
136
    public function setCopy($value)
137
    {
138
        $this->fileType = 'copy';
139
        $this->_copy = $value;
140
    }
141
142
    /**
143
     * Copy getter. Processes aliases.
144
     */
145
    public function getCopy()
146
    {
147
        return Yii::getAlias($this->_copy);
148
    }
149
150
    /**
151
     * Dirname getter.
152
     */
153
    public function getDirname()
154
    {
155
        return $this->getFile()->getDirname();
156
    }
157
158
    /**
159
     * Path getter.
160
     */
161
    public function getPath()
162
    {
163
        return $this->getFile()->getPath();
164
    }
165
166
    /**
167
     * Checks if the file exists.
168
     */
169
    public function exists()
170
    {
171
        return $this->getFile()->exists();
172
    }
173
174
    /**
175
     * Read the file.
176
     */
177
    public function read()
178
    {
179
        return $this->getFile()->read();
180
    }
181
182
    /**
183
     * Read the file into array.
184
     * @return array
185
     */
186
    public function readArray()
187
    {
188
        return $this->getFile()->readArray();
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function actionPerform($name = null, $path = null)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
    {
196
        Yii::trace("Started: '$this->id'");
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hidev\components\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...
197
198
        return $this->runActions(['before', 'make', 'after']);
0 ignored issues
show
Documentation Bug introduced by
The method runActions does not exist on object<hidev\components\ConfigFile>? 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...
199
    }
200
201 View Code Duplication
    public function load()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203
        $data = $this->getFile()->load() ?: [];
204
        if ($data) { /// TODO think what's better
205
        //  $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...
206
            $this->setItems(ArrayHelper::merge($this->toArray(), $data));
207
        //  $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...
208
        }
209
    }
210
211
    /**
212
     * DEPRECATED in favour of save().
213
     */
214
    public function actionSave()
215
    {
216
        return $this->save();
217
    }
218
219
    /**
220
     * Save the file.
221
     */
222 View Code Duplication
    public function save()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
    {
224
        if ($this->once && $this->exists()) {
225
            return 0;
226
        }
227
        $this->_items = Helper::uniqueConfig($this->_items);
228
        $this->getFile()->save($this);
229
    }
230
231
    /**
232
     * Modify action.
233
     */
234 View Code Duplication
    public function actionModify()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
    {
236
        foreach (['chown', 'chgrp', 'chmod'] as $k) {
237
            $v = $this->{$k};
238
            if ($v) {
239
                $this->file->{$k}($v);
0 ignored issues
show
Documentation introduced by
The property file does not exist on object<hidev\components\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...
240
            }
241
        }
242
    }
243
}
244