Passed
Push — main ( 2c7951...c544a0 )
by Michael
58s queued 13s
created

BootsCallProxies::makeFluentObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\EnhancedContainer\Traits;
6
7
use Illuminate\Support\Fluent;
8
9
trait BootsCallProxies
10
{
11
    /**
12
     * @var Fluent|null
13
     */
14
    protected ?Fluent $proxy = null;
15
16
    /**
17
     * Boots the fluent object of call proxies.
18
     *
19
     * @param  string|null  $method
20
     *
21
     * @return void
22
     */
23 5
    public function bootCallProxies(?string $method = null): void
24
    {
25 5
        $reflection = new \ReflectionClass(static::class);
26
27 5
        $method = $this->getMethod($method, $reflection);
28
29 5
        $dependencies = $method->getParameters();
30
31 5
        if (! empty($dependencies)) {
32 5
            $this->proxy = new Fluent;
33
34 5
            collect($dependencies)->map(function ($param) {
0 ignored issues
show
Bug introduced by
$dependencies of type ReflectionParameter[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
            collect(/** @scrutinizer ignore-type */ $dependencies)->map(function ($param) {
Loading history...
35 5
                $class = $param->getType()->getName();
36
37 5
                if (class_exists($class) || interface_exists($class)) {
38 5
                    property_exists(static::class, $param->getName()) && is_object($this->{$param->getName()})
39 3
                        ? $this->proxy->{$param->getName()} = call($this->{$param->getName()})
40 3
                        : $this->proxy->{$param->getName()} = call($class);
41
                }
42
            });
43
        }
44
    }
45
46
    /**
47
     * Determines which method to use.
48
     *
49
     * @param  string|null  $method
50
     * @param  \ReflectionClass  $reflectionClass
51
     *
52
     * @return \ReflectionMethod|null
53
     */
54 5
    private function getMethod(?string $method, \ReflectionClass $reflectionClass): ?\ReflectionMethod
55
    {
56 5
        return is_null($method)
57 4
            ? $reflectionClass->getConstructor()
58 5
            : $reflectionClass->getMethod($method);
59
    }
60
}
61