Completed
Pull Request — master (#8)
by
unknown
03:08
created

PropertySupportBehavior::getPropertyTerm()   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
namespace nkostadinov\taxonomy\behaviors;
4
5
use Yii;
6
use yii\base\Behavior;
7
use yii\base\Event;
8
use yii\db\ActiveRecord;
9
use yii\db\AfterSaveEvent;
10
11
/**
12
 * @author Nikolay Traykov
13
 */
14
class PropertySupportBehavior extends Behavior
15
{
16
    public $property;
17
18
    private $_allowedMethods = [
19
        'propertyTerm' => 'getPropertyTerm',
20
        'properties' => 'getProperties',
21
    ];
22
23
    public function canGetProperty($name, $checkVars = true)
24
    {
25
        if (array_key_exists($name, $this->_allowedMethods)) {
26
            return true;
27
        }
28
29
        return false;
30
    }
31
32
    public function __get($name)
33
    {
34
        if (array_key_exists($name, $this->_allowedMethods)) {
35
            return $this->{$this->_allowedMethods[$name]}();
36
        }
37
38
        return false;
39
    }
40
41
    public function hasMethod($name)
42
    {
43
        return in_array($name, $this->_allowedMethods);
44
    }
45
46
    public function getPropertyTerm()
47
    {
48
        return Yii::$app->taxonomy->getTerm($this->property);
49
    }
50
51
    public function getProperties($properties = [])
52
    {
53
        return $this->propertyTerm->getTerms($this->id, $properties);
0 ignored issues
show
Bug introduced by
The property propertyTerm does not seem to exist. Did you mean property?

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 id does not exist on object<nkostadinov\taxon...ropertySupportBehavior>. 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...
54
    }
55
}
56