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

RequiredParameterAmountIncreased   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

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