FieldMap::matches()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace TomPHP\HalClient\Resource;
4
5
use TomPHP\HalClient\Exception\FieldNotFoundException;
6
7
final class FieldMap implements FieldNode
8
{
9
    /** @var Node[] */
10
    private $fields;
11
12
    /** @param Node[] $fields */
13
    public function __construct(array $fields)
14
    {
15
        $this->fields = $fields;
16
    }
17
18
    /**
19
     * @param string $name
20
     *
21
     * @return Node
22
     */
23
    public function __get($name)
24
    {
25
        return $this->getField($name);
26
    }
27
28
    /**
29
     * @param string $name
30
     *
31
     * @return Field
0 ignored issues
show
Documentation introduced by
Should the return type not be Node?

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...
32
     *
33
     * @throws FieldNotFoundException
34
     */
35 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...
36
    {
37
        if (!array_key_exists($name, $this->fields)) {
38
            throw new FieldNotFoundException($name);
39
        }
40
41
        return $this->fields[$name];
42
    }
43
44
    public function matches($criteria)
45
    {
46
        $result = true;
47
48
        foreach ($criteria as $name => $value) {
49
            if (!array_key_exists($name, $this->fields)) {
50
                $result = false;
51
                break;
52
            }
53
54
            $result = $result && $this->fields[$name]->matches($value);
55
        }
56
57
        return $result;
58
    }
59
}
60