Completed
Push — master ( f6cc4f...20183c )
by Andrii
03:46
created

src/components/File.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\components;
12
13
use hidev\base\File as FileObj;
14
use hidev\helpers\Helper;
15
use Yii;
16
use yii\helpers\ArrayHelper;
17
18
/**
19
 * File component.
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class File extends \hidev\base\Component implements \yii\base\Arrayable, \ArrayAccess, \IteratorAggregate
23
{
24
    use \hiqdev\yii2\collection\ObjectTrait;
25
26
    /**
27
     * @var bool Don't touch file if exists
28
     */
29
    public $once;
30
31
    /**
32
     * @var string Username to change file owner to
33
     */
34
    public $chown;
35
36
    /**
37
     * @var string Group to change file group to
38
     */
39
    public $chgrp;
40
41
    /**
42
     * @var string|integer Permissions to change to
43
     */
44
    public $chmod;
45
46
    /**
47
     * @var string specifies handler to be used
48
     */
49
    public $fileType;
50
51
    /**
52
     * @var array|FileObj 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
The property id does not exist on object<hidev\components\File>. 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 FileObj
97
     */
98
    public function getFile()
99
    {
100
        if (!is_object($this->_file)) {
101
            $this->_file = Yii::createObject(array_merge([
102
                'class'    => FileObj::class,
103
                'template' => $this->getTemplate(),
104
                'goal'     => $this,
105
                'path'     => $this->_path ?: $this->id,
0 ignored issues
show
The property id does not exist on object<hidev\components\File>. 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 FileObj::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
    public function load()
192
    {
193
        $data = $this->getFile()->load() ?: [];
194
        if ($data) { /// TODO think what's better
195
            //  $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...
196
            $this->setItems(ArrayHelper::merge($this->toArray(), $data));
197
            //  $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...
198
        }
199
    }
200
201
    /**
202
     * General save: save and modify.
203
     */
204
    public function save()
205
    {
206
        if (!$this->once || !$this->exists()) {
207
            $this->saveFile();
208
        }
209
        $this->modifyFile();
210
    }
211
212
    /**
213
     * Save the file.
214
     */
215
    protected function saveFile()
216
    {
217
        $this->_items = Helper::uniqueConfig($this->_items);
218
        $this->getFile()->save($this);
219
    }
220
221
    /**
222
     * Applies modifications: chown, chgrp, chmod.
223
     */
224
    public function modifyFile()
225
    {
226
        foreach (['chown', 'chgrp', 'chmod'] as $key) {
227
            $value = $this->{$key};
228
            if ($value) {
229
                $this->file->{$key}($value);
0 ignored issues
show
The property file does not exist on object<hidev\components\File>. 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...
230
            }
231
        }
232
    }
233
}
234