Completed
Push — master ( 371549...7c209a )
by Andrii
02:33
created

File::setExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
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 file stat
59
     */
60
    protected $_stat;
61
62
    /**
63
     * @var array possible types
64
     */
65
    public $types = [];
66
67
    /**
68
     * @var string type
69
     */
70
    public $type;
71
72
    /**
73
     * @var string template
74
     */
75
    public $template;
76
77
    /**
78
     * @var mixed data
79
     */
80
    protected $data;
81
82
    /**
83
     * @var string path to minimal example file
84
     */
85
    public $minimal;
86
87
    /**
88
     * Create file object.
89
     * @param string|array $path or config
90
     * @return File
91
     */
92
    public static function create($path)
93
    {
94
        $config = is_array($path) ? $path : compact('path');
95
        $config['class'] = get_called_class();
96
97
        return Yii::createObject($config);
98
    }
99
100
    public function getMinimalPath()
101
    {
102
        return Yii::getAlias($this->minimal);
103
    }
104
105
    /**
106
     * @var array type to extension correspondance
107
     */
108
    protected static $_extension2type = [
109
        'json'      => 'json',
110
        'yml'       => 'yaml',  /// first one is preferred
111
        'yaml'      => 'yaml',
112
        'xml'       => 'xml',
113
        'xml.dist'  => 'xml',
114
    ];
115
116
    public function getExtensionByType($type)
117
    {
118
        static $type2extension;
119
        if ($type2extension === null) {
120
            foreach (static::$_extension2type as $e => $t) {
121
                if (!$type2extension[$t]) {
122
                    $type2extension[$t] = $e;
123
                }
124
            }
125
        }
126
127
        return $type2extension[$type];
128
    }
129
130
    public function getTypeByExtension($extension)
131
    {
132
        return static::$_extension2type[$extension];
133
    }
134
135
    public function findType()
136
    {
137
        return ($this->goal ? $this->goal->fileType : null) ?: static::getTypeByExtension($this->_extension) ?: 'template';
138
    }
139
140
    public function setPath($path)
141
    {
142
        $path             = Yii::getAlias($path);
143
        $info             = pathinfo($path);
144
        $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...
145
        $this->_dirname   = $info['dirname'];
146
        $this->_basename  = $info['basename'];
147
        $this->_filename  = $info['filename'];
148
        $this->_extension = $info['extension'];
149
        $this->type       = $this->findType();
150
    }
151
152
    public function getPath()
153
    {
154
        return $this->_path;
155
    }
156
157
    public function setBasename($basename)
158
    {
159
        $this->setPath($this->_dirname . '/' . $basename);
160
    }
161
162
    public function getBasename()
163
    {
164
        return $this->_basename;
165
    }
166
167
    public function setDirname($dirname)
168
    {
169
        $this->setPath($dirname . '/' . $this->_basename);
170
    }
171
172
    public function getDirname()
173
    {
174
        return $this->_dirname;
175
    }
176
177
    public function setFilename($filename)
178
    {
179
        $this->setPath($this->_dirname . '/' . $filename . '.' . $this->_extension);
180
    }
181
182
    public function getFilename()
183
    {
184
        return $this->_filename;
185
    }
186
187
    public function setExtension($extension)
188
    {
189
        $this->setPath($this->_dirname . '/' . $this->_filename . '.' . $extension);
190
    }
191
192
    public function getExtension()
193
    {
194
        return $this->_extension;
195
    }
196
197
    public function getCtype()
198
    {
199
        return Helper::id2camel($this->type);
200
    }
201
202
    /**
203
     * Save file.
204
     * @param mixed $data
205
     * @return bool true if file was changed
206
     */
207
    public function save($data = null)
208
    {
209
        if ($data !== null) {
210
            $this->data = $data;
211
        }
212
213
        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...
214
    }
215
216
    public function write($content)
217
    {
218
        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...
219
    }
220
221
    public function load()
222
    {
223
        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...
224
    }
225
226
    public function read()
227
    {
228
        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...
229
    }
230
231
    public function readArray()
232
    {
233
        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...
234
    }
235
236
    public function getHandler()
237
    {
238
        if (!is_object($this->_handler)) {
239
            $this->_handler = Yii::createObject([
240
                'class'    => 'hidev\handlers\\' . $this->getCtype() . 'Handler',
241
                'template' => $this->template,
242
                'goal'     => $this->goal,
243
            ]);
244
        }
245
246
        return $this->_handler;
247
    }
248
249
    public static function file_exists($path)
250
    {
251
        return file_exists(Yii::getAlias($path));
252
    }
253
254
    public function exists()
255
    {
256
        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...
257
    }
258
259
    public function find(array $types = [])
260
    {
261
        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...
262
            $types = $this->types;
263
        }
264
        foreach ($types as $type) {
265
            foreach (static::$_extension2type as $e => $t) {
266
                if ($t === $type) {
267
                    $this->setExtension($e);
268
                    if ($this->exists()) {
269
                        return true;
270
                    }
271
                }
272
            }
273
        }
274
275
        return false;
276
    }
277
278
    public function get($name)
279
    {
280
        return $this->data[$name];
281
    }
282
283
    public function getStat($field = null)
284
    {
285
        if ($this->_stat === null) {
286
            $this->_stat = stat($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
        }
288
289
        return is_null($field) ? $this->_stat : $this->_stat[$field];
290
    }
291
292
    public function getUid()
293
    {
294
        return $this->getStat(4);
295
    }
296
297 View Code Duplication
    public function getOwner()
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...
298
    {
299
        if (!isset($this->_stat['owner'])) {
300
            $this->_stat['owner'] = posix_getpwuid($this->getUid());
301
        }
302
303
        return $this->getStat('owner')['name'];
304
    }
305
306
    public function getGid()
307
    {
308
        return $this->getStat(5);
309
    }
310
311 View Code Duplication
    public function getGroup()
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...
312
    {
313
        if (!isset($this->_stat['group'])) {
314
            $this->_stat['group'] = posix_getgrgid($this->getGid());
315
        }
316
317
        return $this->getStat('group')['name'];
318
    }
319
320
    public function getPermissions()
321
    {
322
        return substr(sprintf('%o', $this->getStat(2)), -4);
323
    }
324
325
    public function chmod($value)
326
    {
327
        if ($value === $this->getPermissions()) {
328
            return;
329
        }
330
        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...
331
        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...
332
    }
333
334
    public function chown($value)
335
    {
336
        $ownergroup = $this->getOwner() . ':' . $this->getGroup();
337
        if (in_array($value, [$ownergroup, $this->getOwner(), $this->getUid()])) {
338
            return;
339
        }
340
        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...
341
        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...
342
    }
343
344
    public function chgrp($value)
345
    {
346
        if (in_array($value, [$this->getGroup(), $this->getGid()])) {
347
            return;
348
        }
349
        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...
350
        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...
351
    }
352
}
353