Passed
Pull Request — master (#38)
by Marco
03:11
created

RequiredParameterAmountIncreased   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 18 2
A functionOrMethodName() 0 9 3
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\BetterReflection\Reflection\ReflectionFunctionAbstract;
10
use Roave\BetterReflection\Reflection\ReflectionMethod;
11
12
/**
13
 * When new parameters are added, they must be optional, or else the callers will provide an insufficient
14
 * amount of parameters to the function.
15
 */
16
final class RequiredParameterAmountIncreased implements FunctionBased
17
{
18
    public function compare(ReflectionFunctionAbstract $fromFunction, ReflectionFunctionAbstract $toFunction) : Changes
19
    {
20
        $fromRequiredParameters = $fromFunction->getNumberOfRequiredParameters();
21
        $toRequiredParameters   = $toFunction->getNumberOfRequiredParameters();
22
23
        if ($fromRequiredParameters >= $toRequiredParameters) {
24
            return Changes::new();
25
        }
26
27
        return Changes::fromArray([
28
            Change::changed(
29
                sprintf(
30
                    'The number of required arguments for %s increased from %d to %d',
31
                    $this->functionOrMethodName($fromFunction),
32
                    $fromRequiredParameters,
33
                    $toRequiredParameters
34
                ),
35
                true
36
            ),
37
        ]);
38
    }
39
40
    private function functionOrMethodName(ReflectionFunctionAbstract $function) : string
41
    {
42
        if ($function instanceof ReflectionMethod) {
43
            return $function->getDeclaringClass()->getName()
44
                . ($function->isStatic() ? '::' : '#')
45
                . $function->getName();
46
        }
47
48
        return $function->getName();
49
    }
50
}
51