IdMetadata   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 78
ccs 28
cts 35
cp 0.8
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hasIds() 0 3 1
A getName() 0 13 3
A getType() 0 11 3
A isMultiId() 0 3 1
A getIds() 0 3 1
A append() 0 3 1
B getValue() 0 23 5
1
<?php
2
3
namespace Vox\Webservice\Metadata;
4
5
use Vox\Metadata\PropertyMetadata;
6
7
class IdMetadata
8
{
9
    /**
10
     * @var PropertyMetadata[]
11
     */
12
    private $ids = [];
13
14 38
    public function append(PropertyMetadata $metadata)
15
    {
16 38
        $this->ids[] = $metadata;
17 38
    }
18
19 26
    public function getValue($object)
20
    {
21 26
        if (!$this->hasIds()) {
22
            throw new \RuntimeException("transfer " . get_class($object) . " has no id mapping");
23
        }
24
25 26
        if ($this->isMultiId()) {
26 2
            $values = [];
27
28 2
            foreach ($this->ids as $idMetadata) {
29 2
                $value = $idMetadata->getValue($object);
30
31 2
                if (!$value) {
32 1
                    continue;
33
                }
34
35 2
                $values[] = sprintf('%s=%s', $idMetadata->name, $idMetadata->getValue($object));
36
            }
37
38 2
            return implode(';', $values);
39
        }
40
41 25
        return $this->ids[0]->getValue($object);
42
    }
43
44 30
    public function hasIds(): bool
45
    {
46 30
        return !empty($this->ids);
47
    }
48
49 1
    public function getName()
50
    {
51 1
        if (!$this->hasIds()) {
52
            return;
53
        }
54
55 1
        if ($this->isMultiId()) {
56
            return implode(array_map(function ($metadata) {
0 ignored issues
show
Bug introduced by
The call to array_map() has too few arguments starting with arr1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
            return implode(/** @scrutinizer ignore-call */ array_map(function ($metadata) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
57
                return $metadata->name;
58
            }));
59
        }
60
61 1
        return $this->ids[0]->name;
62
    }
63
64 30
    public function isMultiId(): bool
65
    {
66 30
        return count($this->ids) > 1;
67
    }
68
69 3
    public function getType(): string
70
    {
71 3
        if (!$this->hasIds()) {
72
            return '';
73
        }
74
75 3
        if ($this->isMultiId()) {
76
            return 'multi';
77
        }
78
79 3
        return $this->ids[0]->type;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->ids[0]->type could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82 1
    public function getIds()
83
    {
84 1
        return $this->ids;
85
    }
86
}