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

PropertyDefaultValueChanged   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A compare() 0 22 3
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