ValueObject::compare()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 4
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Budgegeria\IntlSort\Comparator;
6
7
use Budgegeria\IntlSort\Collator\Collator;
8
9
class ValueObject implements Comparable
10
{
11 26
    public function __construct(private Collator $collator, private string $methodOrPropertyName, private bool $isProperty)
12
    {
13 26
    }
14
15 24
    public function compare(mixed $value, mixed $comparativeValue): int
16
    {
17 24
        return $this->collator->compare(
18 24
            $this->callAccessor((object) $value),
19 24
            $this->callAccessor((object) $comparativeValue),
20 24
        );
21
    }
22
23 24
    private function callAccessor(object $valueObject): mixed
24
    {
25 24
        if ($this->isProperty) {
26 12
            return $valueObject->{$this->methodOrPropertyName};
27
        }
28
29 12
        return $valueObject->{$this->methodOrPropertyName}();
30
    }
31
}
32