Completed
Push — PSR-11-2 ( a5ad88...7f5041 )
by Nikolaos
03:59
created

AutoResolver::getUnifiedParameter()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 26
ccs 12
cts 12
cp 1
rs 8.4444
cc 8
nc 5
nop 3
crap 8
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AuraPHP
12
 *
13
 * @link    https://github.com/auraphp/Aura.Di
14
 * @license https://github.com/auraphp/Aura.Di/blob/4.x/LICENSE
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\Container\Resolver;
20
21
use Phalcon\Container\Exception\NoSuchProperty;
22
use Phalcon\Container\Injection\LazyNew;
23
use ReflectionException;
24
use ReflectionParameter;
25
26
/**
27
 * This extension of the Resolver additionally auto-resolves unspecified
28
 * constructor params according to their typehints; use with caution as it can
29
 * be very difficult to debug.
30
 *
31
 * @property ValueObject $types
32
 */
33
class AutoResolver extends Resolver
34
{
35
    /**
36
     * Auto-resolve these typehints to these values.
37
     *
38
     * @var ValueObject
39
     */
40
    protected ValueObject $types;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
41
42
    /**
43
     * AutoResolver constructor.
44
     *
45
     * @param Reflector $reflector
46
     */
47 8
    public function __construct(Reflector $reflector)
48
    {
49 8
        parent::__construct($reflector);
50
51 8
        $this->types = new ValueObject();
52 8
    }
53
54
    /**
55
     * Auto-resolves params typehinted to classes.
56
     *
57
     * @param ReflectionParameter $rparam A parameter reflection.
58
     * @param string              $class  The class name to return values for.
59
     * @param array               $parent The parent unified params.
60
     *
61
     * @return LazyNew|mixed|DefaultValueParameter|UnresolvedParameter
62
     * @throws ReflectionException
63
     * @throws NoSuchProperty
64
     */
65 4
    protected function getUnifiedParameter(ReflectionParameter $rparam, string $class, array $parent)
66
    {
67 4
        $unified = parent::getUnifiedParameter($rparam, $class, $parent);
68
69
        // already resolved?
70 4
        if (!$unified instanceof UnresolvedParameter && !$unified instanceof DefaultValueParameter) {
71 1
            return $unified;
72
        }
73
74
        // use an explicit auto-resolution?
75 3
        $rtype = $rparam->getClass();
76 3
        if ($rtype && $this->types->has($rtype->name)) {
77 1
            return $this->types->get($rtype->name);
78
        }
79
80 3
        if ($unified instanceof DefaultValueParameter) {
81 2
            return $unified;
82
        }
83
84
        // use a lazy-new-instance of the typehinted class?
85 2
        if ($rtype && $rtype->isInstantiable()) {
86 1
            return new LazyNew($this, new Blueprint($rtype->name));
87
        }
88
89
        // $unified is still an UnresolvedParam
90 1
        return $unified;
91
    }
92
93
    /**
94
     * @return ValueObject
95
     */
96 3
    public function types(): ValueObject
97
    {
98 3
        return $this->types;
99
    }
100
}
101