Completed
Push — master ( a64369...5e51f5 )
by Andy
02:12
created

PopoloObject::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1.037
1
<?php
2
3
namespace mySociety\EveryPoliticianPopolo\Objects;
4
5
class PopoloObject
6
{
7
    use \mySociety\EveryPoliticianPopolo\Traits\ArrayGetterTrait;
8
9
    protected $properties = [];
10
    protected $baseProperties = [
11
        'keyForHash',
12
    ];
13
    protected $data;
14
    protected $allPopolo;
15
16
    /**
17
     *
18
     */
19 255
    public function __construct($data, $allPopolo)
20
    {
21 255
        $this->data = $data;
22 255
        $this->allPopolo = $allPopolo;
23 255
    }
24
25 255 View Code Duplication
    public function __get($prop)
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...
26
    {
27 255
        $properties = array_merge($this->baseProperties, $this->properties);
28 255
        if (in_array($prop, $properties)) {
29 255
            $getter = 'get' . ucfirst($prop);
30 255
            return $this->$getter();
31
        }
32 3
        trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR);
33
    }
34
35 222
    protected function getKeyForHash()
36 2
    {
37 222
         return $this->getId();
38
    }
39
40 2
    protected function getId()
41
    {
42
        return null;
43 2
    }
44
45 27
    private function getRelatedObjects($popoloArray)
46
    {
47 27
        return $this->arrGet($this->data, $popoloArray, []);
48
    }
49
50 35
    protected function getDate($attr, $default = null)
51
    {
52 33
        $d = $this->arrGet($this->data, $attr);
53 35
        if ($d) {
54 30
            return new \DateTime($d);
55
        }
56 3
        return $default;
57
    }
58
59 27
    private function getRelatedValues($popoloArray, $infoTypeKey, $infoType, $infoValueKey)
60
    {
61
        /* Get a value from one of the Popolo related objects
62
         *
63
         * For example, if you have a person with related links, like
64
         * this:
65
         *
66
         *     {
67
         *         "name": "Dale Cooper",
68
         *         "links": [
69
         *             {
70
         *                 "note": "wikipedia",
71
         *                 "url": "https://en.wikipedia.org/wiki/Dale_Cooper"
72
         *             }
73
         *         ]
74
         *     }
75
         *
76
         * When calling this method to get the Wikipedia URL, you would use:
77
         *
78
         *     popoloArray  = 'links'
79
         *     infoTypeKey  = 'note'
80
         *     infoType     = 'wikipedia'
81
         *     infoValueKey = 'url'
82
         *
83
         * ... so the following would work:
84
         *
85
         *     $this->getRelatedValue('links', 'note', 'wikipedia', 'url')
86
         *     # => 'https://en.wikipedia.org/wiki/Dale_Cooper'
87
         */
88 27
        $relatedValues = [];
89 27
        $relatedObjects = $this->getRelatedObjects($popoloArray);
90 27
        foreach ($relatedObjects as $o) {
91 21
            if ($o[$infoTypeKey] === $infoType) {
92 21
                $relatedValues[] = $o[$infoValueKey];
93 16
            }
94 18
        }
95 27
        return $relatedValues;
96
    }
97
98 12
    public function identifierValues($scheme)
99
    {
100 12
        return $this->getRelatedValues('identifiers', 'scheme', $scheme, 'identifier');
101
    }
102
103 12
    public function identifierValue($scheme)
104
    {
105 12
        $identifierValues = $this->identifierValues($scheme);
106 12
        return (count($identifierValues) > 0) ? $identifierValues[0] : null;
107
    }
108
109 12
    public function linkValues($note)
110
    {
111 12
        return $this->getRelatedValues('links', 'note', $note, 'url');
112
    }
113
114 9
    public function linkValue($note)
115
    {
116 9
        $linkValues = $this->linkValues($note);
117 9
        return (count($linkValues) > 0) ? $linkValues[0] : null;
118
    }
119
120 12
    public function contactDetailValues($contactType)
121
    {
122 12
        return $this->getRelatedValues('contact_details', 'type', $contactType, 'value');
123
    }
124
125 12
    public function contactDetailValue($contactType)
126
    {
127 12
        $contactDetailValues = $this->contactDetailValues($contactType);
128 12
        return (count($contactDetailValues) > 0) ? $contactDetailValues[0] : null;
129
    }
130
131 42 View Code Duplication
    public function equals($other)
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...
132
    {
133 42
        if (is_object($other) && get_class($other) == get_class($this)) {
134 39
            return $this->id == $other->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<mySociety\EveryPo...o\Objects\PopoloObject>. 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...
135 2
        }
136 3
        throw new \BadMethodCallException;
137
    }
138
139 51
    public function getRelatedObjectArr($popoloArray)
140
    {
141 51
        return $this->arrGet($this->data, $popoloArray, []);
142
    }
143
}
144