Completed
Pull Request — master (#38)
by Marco
05:15
created

PropertyDefaultValueChanged::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 14
nc 3
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
12
final class PropertyDefaultValueChanged implements PropertyBased
13
{
14
    /** @var ReflectionPropertyName */
15
    private $formatProperty;
16
17
    public function __construct()
18
    {
19
        $this->formatProperty = new ReflectionPropertyName();
20
    }
21
22
    public function compare(ReflectionProperty $fromProperty, ReflectionProperty $toProperty) : Changes
23
    {
24
        if ($fromProperty->isPrivate()) {
25
            return Changes::new();
26
        }
27
28
        $fromPropertyDefaultValue = $fromProperty->getDefaultValue();
29
        $toPropertyDefaultValue   = $toProperty->getDefaultValue();
30
31
        if ($fromPropertyDefaultValue === $toPropertyDefaultValue) {
32
            return Changes::new();
33
        }
34
35
        return Changes::fromArray([
36
            Change::changed(
37
                sprintf(
38
                    'Property %s changed default value from %s to %s',
39
                    $this->formatProperty->__invoke($fromProperty),
40
                    var_export($fromPropertyDefaultValue, true),
41
                    var_export($toPropertyDefaultValue, true)
42
                ),
43
                true
44
            )
45
        ]);
46
    }
47
}
48