InCollectionMatchingSegment::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pheature\Model\Toggle;
6
7
use Pheature\Core\Toggle\Read\Segment;
8
9
/**
10
 * @psalm-import-type SegmentPayload from Segment
11
 */
12
class InCollectionMatchingSegment implements Segment
13
{
14
    public const NAME = 'in_collection_matching_segment';
15
    private string $id;
16
    /** @var SegmentPayload */
0 ignored issues
show
Bug introduced by
The type Pheature\Model\Toggle\SegmentPayload was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
    private array $criteria;
18
19
    /** @param SegmentPayload $criteria */
20 9
    public function __construct(string $id, array $criteria)
21
    {
22 9
        $this->id = $id;
23 9
        $this->criteria = $criteria;
0 ignored issues
show
Documentation Bug introduced by
It seems like $criteria of type array is incompatible with the declared type Pheature\Model\Toggle\SegmentPayload of property $criteria.

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...
24
    }
25
26 8
    public function id(): string
27
    {
28 8
        return $this->id;
29
    }
30
31 8
    public function type(): string
32
    {
33 8
        return self::NAME;
34
    }
35
36 8
    public function criteria(): array
37
    {
38 8
        return $this->criteria;
39
    }
40
41 8
    public function match(array $payload): bool
42
    {
43 8
        if (empty($this->criteria)) {
44 1
            return false;
45
        }
46
47
        /** @var mixed $criterionValue */
48 7
        foreach ($this->criteria as $field => $criterionValue) {
49 7
            if (!array_key_exists($field, $payload)) {
50 1
                return false;
51
            }
52
53 7
            if (!$this->isAMatch($payload[$field], $criterionValue)) {
54 3
                return false;
55
            }
56
        }
57
58 3
        return true;
59
    }
60
61
    /**
62
     * @param mixed $payloadValue
63
     * @param mixed $criterionValue
64
     */
65 7
    private function isAMatch($payloadValue, $criterionValue): bool
66
    {
67 7
        if (is_array($criterionValue)) {
68 5
            return in_array($payloadValue, $criterionValue, true);
69
        }
70
71 4
        return $payloadValue === $criterionValue;
72
    }
73
74 8
    public function toArray(): array
75
    {
76
        return [
77 8
            'id' => $this->id,
78
            'type' => self::NAME,
79 8
            'criteria' => $this->criteria,
80
        ];
81
    }
82
83 8
    public function jsonSerialize(): array
84
    {
85 8
        return $this->toArray();
86
    }
87
}
88