RequiredParameterAmountIncreased   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

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