Completed
Push — master ( 40d07d...371549 )
by Andrii
02:39
created

File::chown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\base;
13
14
use hidev\helpers\Helper;
15
use Yii;
16
17
/**
18
 * A file to be processed with hidev.
19
 */
20
class File extends \yii\base\Object
21
{
22
    /**
23
     * @var Goal
24
     */
25
    public $goal;
26
27
    /**
28
     * @var file hanler: renderer and parser
29
     */
30
    protected $_handler;
31
32
    /**
33
     * @var string absolute path
34
     */
35
    protected $_path;
36
37
    /**
38
     * @var string directory
39
     */
40
    protected $_dirname;
41
42
    /**
43
     * @var string name with extension
44
     */
45
    protected $_basename;
46
47
    /**
48
     * @var string name only, without extension
49
     */
50
    protected $_filename;
51
52
    /**
53
     * @var string extension
54
     */
55
    protected $_extension;
56
57
    /**
58
     * @var array possible types
59
     */
60
    public $types = [];
61
62
    /**
63
     * @var string type
64
     */
65
    public $type;
66
67
    /**
68
     * @var string template
69
     */
70
    public $template;
71
72
    /**
73
     * @var mixed data
74
     */
75
    protected $data;
76
77
    /**
78
     * @var string path to minimal example file
79
     */
80
    public $minimal;
81
82
    /**
83
     * Create file object.
84
     * @param string|array $path or config
85
     * @return File
86
     */
87
    public static function create($path)
88
    {
89
        $config = is_array($path) ? $path : compact('path');
90
        $config['class'] = get_called_class();
91
92
        return Yii::createObject($config);
93
    }
94
95
    public function getMinimalPath()
96
    {
97
        return Yii::getAlias($this->minimal);
98
    }
99
100
    /**
101
     * @var array type to extension correspondance
102
     */
103
    protected static $_extension2type = [
104
        'json'      => 'json',
105
        'yml'       => 'yaml',  /// first one is preferred
106
        'yaml'      => 'yaml',
107
        'xml'       => 'xml',
108
        'xml.dist'  => 'xml',
109
    ];
110
111
    public function getExtensionByType($type)
112
    {
113
        static $type2extension;
114
        if ($type2extension === null) {
115
            foreach (static::$_extension2type as $e => $t) {
116
                if (!$type2extension[$t]) {
117
                    $type2extension[$t] = $e;
118
                }
119
            }
120
        }
121
122
        return $type2extension[$type];
123
    }
124
125
    public function getTypeByExtension($extension)
126
    {
127
        return static::$_extension2type[$extension];
128
    }
129
130
    public function findType()
131
    {
132
        return ($this->goal ? $this->goal->fileType : null) ?: static::getTypeByExtension($this->_extension) ?: 'template';
133
    }
134
135
    public function setPath($path)
136
    {
137
        $path             = Yii::getAlias($path);
138
        $info             = pathinfo($path);
139
        $this->_path      = $path;
0 ignored issues
show
Documentation Bug introduced by
It seems like $path can also be of type boolean. However, the property $_path is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
140
        $this->_dirname   = $info['dirname'];
141
        $this->_basename  = $info['basename'];
142
        $this->_filename  = $info['filename'];
143
        $this->_extension = $info['extension'];
144
        $this->type       = $this->findType();
145
    }
146
147
    public function getPath()
148
    {
149
        return $this->_path;
150
    }
151
152
    public function setBasename($basename)
153
    {
154
        $this->setPath($this->_dirname . '/' . $basename);
155
    }
156
157
    public function getBasename()
158
    {
159
        return $this->_basename;
160
    }
161
162
    public function setDirname($dirname)
163
    {
164
        $this->setPath($dirname . '/' . $this->_basename);
165
    }
166
167
    public function getDirname()
168
    {
169
        return $this->_dirname;
170
    }
171
172
    public function setFilename($filename)
173
    {
174
        $this->setPath($this->_dirname . '/' . $filename . '.' . $this->_extension);
175
    }
176
177
    public function getFilename()
178
    {
179
        return $this->_filename;
180
    }
181
182
    public function setExtension($extension)
183
    {
184
        $this->setPath($this->_dirname . '/' . $this->_filename . '.' . $extension);
185
    }
186
187
    public function getExtension()
188
    {
189
        return $this->_extension;
190
    }
191
192
    public function getCtype()
193
    {
194
        return Helper::id2camel($this->type);
195
    }
196
197
    /**
198
     * Save file.
199
     * @param mixed $data
200
     * @return bool true if file was changed
201
     */
202
    public function save($data = null)
203
    {
204
        if ($data !== null) {
205
            $this->data = $data;
206
        }
207
208
        return $this->handler->renderPath($this->path, $this->data);
0 ignored issues
show
Bug introduced by
The property handler does not seem to exist. Did you mean _handler?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Documentation introduced by
The property path does not exist on object<hidev\base\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...
209
    }
210
211
    public function write($content)
212
    {
213
        return $this->handler->write($this->path, $content);
0 ignored issues
show
Bug introduced by
The property handler does not seem to exist. Did you mean _handler?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Documentation introduced by
The property path does not exist on object<hidev\base\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...
214
    }
215
216
    public function load()
217
    {
218
        return $this->data = $this->handler->parsePath($this->path, $this->minimalPath);
0 ignored issues
show
Bug introduced by
The property handler does not seem to exist. Did you mean _handler?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Documentation introduced by
The property path does not exist on object<hidev\base\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...
Bug introduced by
The property minimalPath does not seem to exist. Did you mean minimal?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
219
    }
220
221
    public function read()
222
    {
223
        return $this->handler->read($this->path);
0 ignored issues
show
Bug introduced by
The property handler does not seem to exist. Did you mean _handler?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Documentation introduced by
The property path does not exist on object<hidev\base\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...
224
    }
225
226
    public function readArray()
227
    {
228
        return $this->handler->readArray($this->path);
0 ignored issues
show
Bug introduced by
The property handler does not seem to exist. Did you mean _handler?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Documentation introduced by
The property path does not exist on object<hidev\base\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...
229
    }
230
231
    public function getHandler()
232
    {
233
        if (!is_object($this->_handler)) {
234
            $this->_handler = Yii::createObject([
235
                'class'    => 'hidev\handlers\\' . $this->getCtype() . 'Handler',
236
                'template' => $this->template,
237
                'goal'     => $this->goal,
238
            ]);
239
        }
240
241
        return $this->_handler;
242
    }
243
244
    public static function file_exists($path)
245
    {
246
        return file_exists(Yii::getAlias($path));
247
    }
248
249
    public function exists()
250
    {
251
        return file_exists($this->path);
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\base\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...
252
    }
253
254
    public function find(array $types = [])
255
    {
256
        if (!$types) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $types of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
257
            $types = $this->types;
258
        }
259
        foreach ($types as $type) {
260
            foreach (static::$_extension2type as $e => $t) {
261
                if ($t === $type) {
262
                    $this->setExtension($e);
263
                    if ($this->exists()) {
264
                        return true;
265
                    }
266
                }
267
            }
268
        }
269
270
        return false;
271
    }
272
273
    public function get($name)
274
    {
275
        return $this->data[$name];
276
    }
277
278
    public function chmod($value)
279
    {
280
        passthru("chmod $value $this->path");
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\base\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...
281
        Yii::warning("chmod $this->path '$value'", 'file');
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\base\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...
282
    }
283
284
    public function chown($value)
285
    {
286
        passthru("chown $value $this->path");
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\base\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...
287
        Yii::warning("chown $this->path '$value'", 'file');
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\base\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...
288
    }
289
290
    public function chgrp($value)
291
    {
292
        passthru("chgrp $value $this->path");
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\base\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...
293
        Yii::warning("chgrp $this->path '$value'", 'file');
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\base\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...
294
    }
295
}
296