StrictMatchingSegment::criteria()   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 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 0
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
final class StrictMatchingSegment implements Segment
13
{
14
    public const NAME = 'strict_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 15
    public function __construct(string $id, array $criteria)
21
    {
22 15
        $this->id = $id;
23 15
        $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 7
    public function id(): string
27
    {
28 7
        return $this->id;
29
    }
30
31 7
    public function type(): string
32
    {
33 7
        return self::NAME;
34
    }
35
36 7
    public function criteria(): array
37
    {
38 7
        return $this->criteria;
39
    }
40
41 12
    public function match(array $payload): bool
42
    {
43 12
        $match = false;
44
45
        /** @var mixed $value */
46 12
        foreach ($this->criteria as $key => $value) {
47 10
            if (array_key_exists($key, $payload) && $value === $payload[$key]) {
48 6
                $match = true;
49 6
                continue;
50
            }
51
52 4
            return false;
53
        }
54
55 8
        return $match;
56
    }
57
58 8
    public function toArray(): array
59
    {
60
        return [
61 8
            'id' => $this->id,
62
            'type' => self::NAME,
63 8
            'criteria' => $this->criteria,
64
        ];
65
    }
66
67 7
    public function jsonSerialize(): array
68
    {
69 7
        return $this->toArray();
70
    }
71
}
72