Completed
Pull Request — master (#5)
by Eugene
01:02
created

UnresolvableArgumentException::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 4
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the rybakit/arguments-resolver package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace ArgumentsResolver;
15
16
class UnresolvableArgumentException extends \InvalidArgumentException
17
{
18 7
    public static function fromParameter(\ReflectionParameter $parameter) : self
19
    {
20 7
        return new self(\sprintf(
21 7
            'Unable to resolve argument $%s (#%d) of %s.',
22 7
            $parameter->name,
23 7
            $parameter->getPosition(),
24 7
            static::getFunctionName($parameter->getDeclaringFunction())
0 ignored issues
show
Bug introduced by
Since getFunctionName() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getFunctionName() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
25
        ));
26
    }
27
28
    /**
29
     * @param \ReflectionFunctionAbstract $reflection
30
     *
31
     * @return string
32
     */
33 7
    private static function getFunctionName(\ReflectionFunctionAbstract $reflection) : string
34
    {
35 7
        $name = $reflection->name.'()';
36
37 7
        if ($reflection instanceof \ReflectionMethod) {
38 6
            $name = $reflection->getDeclaringClass()->name.'::'.$name;
39
        }
40
41 7
        return $name;
42
    }
43
}
44