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

RequiredParameterAmountIncreased::compare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\FunctionBased;
6
7
use Roave\ApiCompare\Change;
8
use Roave\ApiCompare\Changes;
9
use Roave\ApiCompare\Formatter\ReflectionFunctionAbstractName;
10
use Roave\BetterReflection\Reflection\ReflectionFunctionAbstract;
11
use function sprintf;
12
13
/**
14
 * When new parameters are added, they must be optional, or else the callers will provide an insufficient
15
 * amount of parameters to the function.
16
 */
17
final class RequiredParameterAmountIncreased implements FunctionBased
18
{
19
    /** @var ReflectionFunctionAbstractName */
20
    private $formatFunction;
21
22
    public function __construct()
23
    {
24
        $this->formatFunction = new ReflectionFunctionAbstractName();
25
    }
26
27
    public function compare(ReflectionFunctionAbstract $fromFunction, ReflectionFunctionAbstract $toFunction) : Changes
28
    {
29
        $fromRequiredParameters = $fromFunction->getNumberOfRequiredParameters();
30
        $toRequiredParameters   = $toFunction->getNumberOfRequiredParameters();
31
32
        if ($fromRequiredParameters >= $toRequiredParameters) {
33
            return Changes::new();
34
        }
35
36
        return Changes::fromArray([
37
            Change::changed(
38
                sprintf(
39
                    'The number of required arguments for %s increased from %d to %d',
40
                    $this->formatFunction->__invoke($fromFunction),
41
                    $fromRequiredParameters,
42
                    $toRequiredParameters
43
                ),
44
                true
45
            ),
46
        ]);
47
    }
48
}
49