Passed
Pull Request — master (#38)
by Marco
02:23
created

PropertyDefaultValueChanged::compare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased;
6
7
use Roave\ApiCompare\Change;
8
use Roave\ApiCompare\Changes;
9
use Roave\ApiCompare\Formatter\ReflectionPropertyName;
10
use Roave\BetterReflection\Reflection\ReflectionProperty;
11
use function sprintf;
12
use function var_export;
13
14
final class PropertyDefaultValueChanged implements PropertyBased
15
{
16
    /** @var ReflectionPropertyName */
17
    private $formatProperty;
18
19
    public function __construct()
20
    {
21
        $this->formatProperty = new ReflectionPropertyName();
22
    }
23
24
    public function compare(ReflectionProperty $fromProperty, ReflectionProperty $toProperty) : Changes
25
    {
26
        $fromPropertyDefaultValue = $fromProperty->getDefaultValue();
27
        $toPropertyDefaultValue   = $toProperty->getDefaultValue();
28
29
        if ($fromPropertyDefaultValue === $toPropertyDefaultValue) {
30
            return Changes::new();
31
        }
32
33
        return Changes::fromArray([Change::changed(
34
            sprintf(
35
                'Property %s changed default value from %s to %s',
36
                $this->formatProperty->__invoke($fromProperty),
37
                var_export($fromPropertyDefaultValue, true),
38
                var_export($toPropertyDefaultValue, true)
39
            ),
40
            true
41
        ),
42
        ]);
43
    }
44
}
45