PropertyDocumentedTypeChanged::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased;
6
7
use Roave\BackwardCompatibility\Change;
8
use Roave\BackwardCompatibility\Changes;
9
use Roave\BackwardCompatibility\Formatter\ReflectionPropertyName;
10
use Roave\BetterReflection\Reflection\ReflectionProperty;
11
use function implode;
12
use function Safe\sort;
13
use function Safe\sprintf;
14
15
/**
16
 * Type declarations for properties are invariant: you can't restrict the type because the consumer may
17
 * write invalid values to it, and you cannot widen the type because the consumer may expect a specific
18
 * type when reading.
19
 */
20
final class PropertyDocumentedTypeChanged implements PropertyBased
21
{
22
    /** @var ReflectionPropertyName */
23
    private $formatProperty;
24
25
    public function __construct()
26
    {
27
        $this->formatProperty = new ReflectionPropertyName();
28
    }
29
30
    public function __invoke(ReflectionProperty $fromProperty, ReflectionProperty $toProperty) : Changes
31
    {
32
        if ($fromProperty->getDocComment() === '') {
33
            return Changes::empty();
34
        }
35
36
        $fromTypes = $fromProperty->getDocBlockTypeStrings();
37
        $toTypes   = $toProperty->getDocBlockTypeStrings();
38
39
        sort($fromTypes);
40
        sort($toTypes);
41
42
        if ($fromTypes === $toTypes) {
43
            return Changes::empty();
44
        }
45
46
        return Changes::fromList(Change::changed(
47
            sprintf(
48
                'Type documentation for property %s changed from %s to %s',
49
                $this->formatProperty->__invoke($fromProperty),
50
                implode('|', $fromTypes) ?: 'having no type',
51
                implode('|', $toTypes) ?: 'having no type'
52
            ),
53
            true
54
        ));
55
    }
56
}
57