Passed
Push — master ( 168388...2ed342 )
by Alexander
01:24
created

ArgumentException   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 43.75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 52
ccs 14
cts 32
cp 0.4375
rs 10
c 1
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getClosureSignature() 0 23 6
A __construct() 0 23 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 30
    public function __construct(\ReflectionFunctionAbstract $reflection, string $parameter)
12
    {
13 30
        $function = $reflection->getName();
14 30
        $class = $reflection->class ?? null;
15
16 30
        if ($class === null) {
17 23
            $method = $function;
18 23
            if ($method === '{closure}') {
19 23
                $method = $this->getClosureSignature($reflection);
20
            }
21
        } else {
22 7
            $method = "{$class}::{$function}";
23
        }
24
25 30
        $fileName = $reflection->getFileName();
26 30
        $line = $reflection->getStartLine();
27
28 30
        $fileAndLine = '';
29 30
        if (!empty($fileName) && !empty($line)) {
30 20
            $fileAndLine = " in \"$fileName\" at line \"$line\"";
31
        }
32
33 30
        parent::__construct(sprintf(static::EXCEPTION_MESSAGE, $parameter, $method, $fileAndLine));
34
    }
35
36
    private function getClosureSignature(\ReflectionFunctionAbstract $reflection): string
37
    {
38
        $result = 'function (';
39
        $closureParameters = [];
40
        foreach ($reflection->getParameters() as $parameter) {
41
            $parameterString = '';
42
            if ($parameter->isArray()) {
43
                $parameterString .= 'array ';
44
            } else if ($parameter->getClass()) {
45
                $parameterString .= $parameter->getClass()->name . ' ';
46
            }
47
            if ($parameter->isPassedByReference()) {
48
                $parameterString .= '&';
49
            }
50
            $parameterString .= '$' . $parameter->name;
51
            if ($parameter->isOptional()) {
52
                $parameterString .= ' = ' . var_export($parameter->getDefaultValue(), true);
53
            }
54
            $closureParameters[] = $parameterString;
55
        }
56
        $result .= implode(', ', $closureParameters);
57
        $result .= ')';
58
        return $result;
59
    }
60
}
61