Completed
Push — master ( 73efce...6751ec )
by JHONATAN
05:35
created

IdMetadata::isMultiId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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
    public function append(PropertyMetadata $metadata)
15
    {
16
        $this->ids[] = $metadata;
17
    }
18
19
    public function getValue($object)
20
    {
21
        if (!$this->hasIds()) {
22
            throw new \RuntimeException("transfer " . get_class($object) . " has no id mapping");
23
        }
24
25
        if ($this->isMultiId()) {
26
            $values = [];
27
28
            foreach ($this->ids as $idMetadata) {
29
                $value = $idMetadata->getValue($object);
30
31
                if (!$value) {
32
                    continue;
33
                }
34
35
                $values[] = sprintf('%s=%s', $idMetadata->name, $idMetadata->getValue($object));
36
            }
37
38
            return implode(';', $values);
39
        }
40
41
        return $this->ids[0]->getValue($object);
42
    }
43
44
    public function hasIds(): bool
45
    {
46
        return !empty($this->ids);
47
    }
48
49
    public function getName()
50
    {
51
        if (!$this->hasIds()) {
52
            return;
53
        }
54
55
        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
        return $this->ids[0]->name;
62
    }
63
64
    public function isMultiId(): bool
65
    {
66
        return count($this->ids) > 1;
67
    }
68
69
    public function getType(): string
70
    {
71
        if (!$this->hasIds()) {
72
            return;
0 ignored issues
show
Bug Best Practice introduced by
The expression return returns the type null which is incompatible with the type-hinted return string.
Loading history...
73
        }
74
75
        if ($this->isMultiId()) {
76
            return 'multi';
77
        }
78
79
        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
}