AddonKeyword   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 32
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A get_by_name() 0 13 2
1
<?php
2
/**
3
 * A keyword which is attached to add-ons and versions.
4
 */
5
class AddonKeyword extends DataObject
6
{
7
8
    public static $db = array(
9
        'Name' => 'Varchar(255)'
10
    );
11
12
    public static $belongs_many_many = array(
13
        'Addons' => 'Addon',
14
        'Versions' => 'AddonVersion'
15
    );
16
17
    /**
18
     * Gets a keyword object by name, creating one if it does not exist.
19
     *
20
     * @param string $name
21
     * @return AddonKeyword
22
     */
23
    public static function get_by_name($name)
24
    {
25
        $name = strtolower($name);
26
        $kw = AddonKeyword::get()->filter('Name', $name)->first();
27
28
        if (!$kw) {
29
            $kw = new AddonKeyword();
30
            $kw->Name = $name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<AddonKeyword>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
31
            $kw->write();
32
        }
33
34
        return $kw;
35
    }
36
}
37