Completed
Push — feature/ss4-upgrade ( f41a3f )
by
unknown
10:12
created

AddonKeyword::get_by_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace SilverStripe\Addons\Model;
4
5
use SilverStripe\ORM\DataObject;
6
7
/**
8
 * A keyword which is attached to add-ons and versions.
9
 */
10
class AddonKeyword extends DataObject 
11
{
12
13
	private static $db = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
14
		'Name' => 'Varchar(255)'
15
	];
16
17
	private static $belongs_many_many = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
18
		'Addons' => 'SilverStripe\Addons\Model\Addon',
19
		'Versions' => 'SilverStripe\Addons\Model\AddonVersion'
20
	];
21
22
	/**
23
	 * Gets a keyword object by name, creating one if it does not exist.
24
	 *
25
	 * @param string $name
26
	 * @return AddonKeyword
27
	 */
28
	public static function get_by_name($name) 
29
	{
30
		$name = strtolower($name);
31
		$kw = AddonKeyword::get()->filter('Name', $name)->first();
32
33
		if (!$kw) {
34
			$kw = new AddonKeyword();
35
			$kw->Name = $name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<SilverStripe\Addons\Model\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...
36
			$kw->write();
37
		}
38
39
		return $kw;
40
	}
41
42
}
43