Resource::isResourceSearchCriteria()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 2
dl 0
loc 7
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace TomPHP\HalClient\Resource;
4
5
use Assert\Assertion;
6
use TomPHP\HalClient\Exception\FieldNotFoundException;
7
use TomPHP\HalClient\Exception\LinkNotFoundException;
8
use TomPHP\HalClient\Exception\ResourceNotFoundException;
9
10
final class Resource implements ResourceNode
11
{
12
    /** @var array */
13
    private $fields = [];
14
15
    /** @var Link[] */
16
    private $links = [];
17
18
    /** @var Resource[] */
19
    private $resources = [];
20
21
    /**
22
     * @param FieldNode[]    $fields
23
     * @param Link[]         $links
24
     * @param ResourceNode[] $resources
25
     */
26
    public function __construct(array $fields, array $links = [], array $resources = [])
27
    {
28
        Assertion::allIsInstanceOf($fields, FieldNode::class);
29
        Assertion::allIsInstanceOf($links, Link::class);
30
        Assertion::allIsInstanceOf($resources, ResourceNode::class);
31
32
        $this->fields    = $fields;
33
        $this->links     = $links;
34
        $this->resources = $resources;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resources of type array<integer,object<Tom...Resource\ResourceNode>> is incompatible with the declared type array<integer,resource> of property $resources.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
    }
36
37
    /**
38
     * @param string $name
39
     *
40
     * @return FieldNode
41
     */
42
    public function __get($name)
43
    {
44
        return $this->getField($name);
45
    }
46
47
    /**
48
     * @param string $name
49
     *
50
     * @return FieldNode
51
     *
52
     * @throws FieldNotFoundException
53
     */
54 View Code Duplication
    public function getField($name)
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...
55
    {
56
        if (!array_key_exists($name, $this->fields)) {
57
            throw new FieldNotFoundException($name);
58
        }
59
60
        return $this->fields[$name];
61
    }
62
63
    /** @return string[] */
0 ignored issues
show
Documentation introduced by
Should the return type not be integer[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
64
    public function getLinks()
65
    {
66
        return array_keys($this->links);
67
    }
68
69
    /**
70
     * @return Link
71
     *
72
     * @throws LinkNotFoundException
73
     */
74
    public function getLink($name)
75
    {
76
        if (!array_key_exists($name, $this->links)) {
77
            throw new LinkNotFoundException($name);
78
        }
79
80
        return $this->links[$name];
81
    }
82
83
    /**
84
     * @return Resource
85
     */
86
    public function getResource($name)
87
    {
88
        if (!array_key_exists($name, $this->resources)) {
89
            throw new ResourceNotFoundException($name);
90
        }
91
92
        return $this->resources[$name];
93
    }
94
95
    public function matches($criteria)
96
    {
97
        foreach ($criteria as $name => $value) {
98
            if (!$this->matchesItem($name, $value)) {
99
                return false;
100
            }
101
        }
102
103
        return true;
104
    }
105
106
    /**
107
     * @param string|int $name
108
     * @param mixed      $value
109
     *
110
     * @return bool
111
     */
112
    private function matchesItem($name, $value)
113
    {
114
        if ($this->isResourceSearchCriteria($name, $value)) {
115
            return $this->matchesResource($value);
116
        }
117
118
        return $this->matchField($name, $value);
119
    }
120
121
    /**
122
     * @param string|int $name
123
     * @param mixed      $value
124
     *
125
     * @return bool
126
     */
127
    private function isResourceSearchCriteria($name, $value)
128
    {
129
        return is_int($name)
130
            && is_array($value)
131
            && array_key_exists(0, $value)
132
            && $value[0] === 'resource';
133
    }
134
135
    /** @return bool */
136
    private function matchesResource(array $value)
137
    {
138
        $name = $value[1];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
139
        $search = $value[2];
140
141
        if (!array_key_exists($name, $this->resources)) {
142
            return false;
143
        }
144
145
        return $this->resources[$name]->matches($search);
0 ignored issues
show
Bug introduced by
The method matches cannot be called on $this->resources[$name] (of type resource).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
146
    }
147
148
    /**
149
     * @param string $name
150
     * @param mixed  $value
151
     *
152
     * @return bool
153
     */
154
    public function matchField($name, $value)
155
    {
156
        if (!array_key_exists($name, $this->fields)) {
157
            return false;
158
        }
159
160
        return $this->fields[$name]->matches($value);
161
    }
162
}
163