Completed
Branch cleanup (6c017e)
by Paul
13:14
created

LibLynxResource   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __get() 0 4 1
A getLink() 0 7 2
1
<?php
2
3
namespace LibLynx\Connect;
4
5
/**
6
 * Class LibLynxResource provides a base class for all HAL-style resources retrieved from the LibLynx API.
7
 *
8
 * All JSON returned by the LibLynx API is modelled on the Hypertext Application Language
9
 * http://stateless.co/hal_specification.html
10
 *
11
 * @package LibLynx\Connect
12
 */
13
abstract class LibLynxResource
14
{
15
    public function __construct($obj)
16
    {
17
        $vars = get_object_vars($obj);
18
        foreach ($vars as $name => $value) {
19
            $this->$name = $value;
20
        }
21
    }
22
23
    public function __get($name)
24
    {
25
        throw new \RuntimeException("No value called $name");
26
    }
27
28
    public function getLink($name)
29
    {
30
        if (isset($this->_links->$name)) {
0 ignored issues
show
Documentation introduced by
The property _links does not exist on object<LibLynx\Connect\LibLynxResource>. 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...
31
            return $this->_links->$name->href;
0 ignored issues
show
Documentation introduced by
The property _links does not exist on object<LibLynx\Connect\LibLynxResource>. 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...
32
        }
33
        return false;
34
    }
35
}
36