Passed
Pull Request — master (#13)
by Aleksei
01:18
created

ArgumentException   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 35
c 3
b 0
f 0
dl 0
loc 55
ccs 34
cts 34
cp 1
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 5
A getClosureSignature() 0 26 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Injector;
6
7
abstract class ArgumentException extends \InvalidArgumentException
8
{
9
    protected const EXCEPTION_MESSAGE = 'Something is wrong with argument "%s" when calling "%s"%s.';
10
11 33
    public function __construct(\ReflectionFunctionAbstract $reflection, string $parameter)
12
    {
13 33
        $function = $reflection->getName();
14 33
        $class = $reflection->class ?? null;
15
16 33
        if ($class === null) {
17 26
            $method = $function;
18 26
            if (substr($method, -9) === '{closure}') {
19 26
                $method = $this->getClosureSignature($reflection);
20
            }
21
        } else {
22 7
            $method = "{$class}::{$function}";
23
        }
24
25 33
        $fileName = $reflection->getFileName();
26 33
        $line = $reflection->getStartLine();
27
28 33
        $fileAndLine = '';
29 33
        if (!empty($fileName) && !empty($line)) {
30 23
            $fileAndLine = " in \"$fileName\" at line $line";
31
        }
32
33 33
        parent::__construct(sprintf(static::EXCEPTION_MESSAGE, $parameter, $method, $fileAndLine));
34
    }
35
36 14
    private function getClosureSignature(\ReflectionFunctionAbstract $reflection): string
37
    {
38 14
        $result = 'function (';
39 14
        $closureParameters = [];
40
        $concat = static function ($condition, $string) use (&$parameterString) {
41 13
            if ($condition) {
42 4
                $parameterString .= $string;
43
            }
44 14
        };
45 14
        foreach ($reflection->getParameters() as $parameter) {
46 13
            $parameterString = '';
47 13
            if ($parameter->hasType()) {
48 13
                $concat($parameter->allowsNull(), '?');
49 13
                $parameterString .= $parameter->getType()->getName() . ' ';
50
            }
51 13
            $concat($parameter->isPassedByReference(), '&');
52 13
            $concat($parameter->isVariadic(), '...');
53 13
            $parameterString .= '$' . $parameter->name;
54 13
            if ($parameter->isDefaultValueAvailable()) {
55 3
                $parameterString .= ' = ' . var_export($parameter->getDefaultValue(), true);
56
            }
57 13
            $closureParameters[] = $parameterString;
58
        }
59 14
        $result .= implode(', ', $closureParameters);
60 14
        $result .= ')';
61 14
        return $result;
62
    }
63
}
64