Set   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A contains() 0 5 3
A findURI() 0 9 3
A isValid() 0 15 4
1
<?php declare(strict_types = 1);
2
3
namespace JSKOS;
4
5
use InvalidArgumentException;
6
7
/**
8
 * A JSKOS Set as defined by JSKOS specification.
9
 *
10
 * A Set is a possibly empty array with all members being JSKOS Resources
11
 * with distinct values in field `uri` (if given), except the last member
12
 * optionally being null.
13
 */
14
class Set extends Container
15
{
16
    /**
17
     * Check whether an equal member already exists in this Set.
18
     */
19
    public function contains($member): bool
20
    {
21
        return $member instanceof Resource && $member->uri &&
22
            $this->findURI($member->uri) >= 0;
23
    }
24
25
    /**
26
     * Return the offset of a member Resource with given URI or -1.
27
     */
28
    public function findURI(string $uri)
29
    {
30
        foreach ($this->members as $offset => $member) {
31
            if ($member->uri == $uri) {
32
                return $offset;
33
            }
34
        }
35
        return -1;
36
    }
37
38
    /**
39
     * Return whether this set does not contain same resources.
40
     */
41
    public function isValid(): bool
42
    {
43
        $uris = [];
44
        foreach ($this->members as $member) {
45
            $uri = $member->uri;
46
            if ($uri) {
47
                if (isset($uris[$uri])) {
48
                    return false;
49
                } else {
50
                    $uris[$uri] = true;
51
                }
52
            }
53
        }
54
        return true;
55
    }
56
}
57