RequiredParameterAmountIncreased::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 2
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased;
6
7
use Roave\BackwardCompatibility\Change;
8
use Roave\BackwardCompatibility\Changes;
9
use Roave\BackwardCompatibility\Formatter\ReflectionFunctionAbstractName;
10
use Roave\BetterReflection\Reflection\ReflectionFunctionAbstract;
11
use function Safe\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 __invoke(ReflectionFunctionAbstract $fromFunction, ReflectionFunctionAbstract $toFunction) : Changes
28
    {
29
        $fromRequiredParameters = $fromFunction->getNumberOfRequiredParameters();
30
        $toRequiredParameters   = $toFunction->getNumberOfRequiredParameters();
31
32
        if ($fromRequiredParameters >= $toRequiredParameters) {
33
            return Changes::empty();
34
        }
35
36
        return Changes::fromList(Change::changed(
37
            sprintf(
38
                'The number of required arguments for %s increased from %d to %d',
39
                $this->formatFunction->__invoke($fromFunction),
40
                $fromRequiredParameters,
41
                $toRequiredParameters
42
            ),
43
            true
44
        ));
45
    }
46
}
47