Test Failed
Pull Request — master (#37)
by Divine Niiquaye
03:08
created

ValueDefinition::build()   D

Complexity

Conditions 15
Paths 257

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 15
eloc 25
c 4
b 0
f 0
nc 257
nop 2
dl 0
loc 46
rs 4.3708

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2021 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI\Definitions;
19
20
use PhpParser\Node\Expr;
21
use PhpParser\Node\Stmt\Return_;
22
use Rade\DI\Exceptions\ContainerResolutionException;
23
use Rade\DI\Exceptions\ServiceCreationException;
24
use Rade\DI\Resolver;
25
26
/**
27
 * Represents a definition service that shouldn't be resolved.
28
 *
29
 * @author Divine Niiquaye Ibok <[email protected]>
30
 */
31
class ValueDefinition implements DefinitionInterface, ShareableDefinitionInterface, DepreciableDefinitionInterface
32
{
33
    use Traits\DeprecationTrait;
0 ignored issues
show
Bug introduced by
The trait Rade\DI\Definitions\Traits\DeprecationTrait requires the property $innerId which is not provided by Rade\DI\Definitions\ValueDefinition.
Loading history...
34
    use Traits\VisibilityTrait;
35
36
    /** @var mixed */
37
    private $value;
38
39
    /**
40
     * Definition constructor.
41
     *
42
     * @param mixed $value
43
     */
44
    public function __construct($value, bool $shared = true)
45
    {
46
        $this->replace($value);
47
        $this->shared = $shared;
48
    }
49
50
    /**
51
     * Replace the existing value.
52
     *
53
     * @param mixed $value
54
     *
55
     * @return $this
56
     */
57
    public function replace($value)
58
    {
59
        if ($value instanceof DefinitionInterface) {
60
            throw new ServiceCreationException(\sprintf('A definition entity must not be an instance of "%s".', DefinitionInterface::class));
61
        } elseif ($value instanceof \PhpParser\Node && !$value instanceof Expr) {
62
            throw new ServiceCreationException(\sprintf('A definition entity must be an instance of "%s".', Expr::class));
63
        }
64
65
        $this->value = $value;
66
67
        return $this;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getEntity()
74
    {
75
        return $this->value;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function build(string $id, Resolver $resolver)
82
    {
83
        $builder = $resolver->getBuilder();
84
85
        if ($this->abstract) {
86
            throw new ContainerResolutionException(\sprintf('Resolving an abstract definition %s is not allowed.', $id));
87
        }
88
89
        if (!empty($this->deprecation)) {
90
            $deprecation = $this->triggerDeprecation($id, $builder);
91
        }
92
93
        if (\is_array($value = $this->value)) {
94
            $value = $resolver->resolveArguments($value);
95
        }
96
97
        if (null === $builder) {
98
            return !\is_array($value) && $this->lazy ? $resolver->resolve($value) : $value;
99
        }
100
101
        $defNode = $builder->method($resolver->createMethod($id))->makeProtected();
102
103
        if ($value instanceof \PhpParser\Node) {
104
            if ($value instanceof Expr\Array_) {
105
                $defNode->setReturnType('array');
106
            } elseif ($value instanceof Expr\New_) {
107
                $defNode->setReturnType($value->class->toString());
0 ignored issues
show
Bug introduced by
The method toString() does not exist on PhpParser\Node\Stmt\Class_. ( Ignorable by Annotation )

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

107
                $defNode->setReturnType($value->class->/** @scrutinizer ignore-call */ toString());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method toString() does not exist on PhpParser\Node\Expr. ( Ignorable by Annotation )

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

107
                $defNode->setReturnType($value->class->/** @scrutinizer ignore-call */ toString());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
            }
109
        } elseif (\PHP_MAJOR_VERSION >= 8) {
110
            $defNode->setReturnType('mixed');
111
        }
112
113
        if (isset($deprecation)) {
114
            $defNode->addStmt($deprecation);
115
        }
116
117
        if ($this->lazy) {
118
            $lazyMethod = \is_array($value) ? 'resolveArguments' : 'resolve';
119
            $createdValue = $builder->methodCall($builder->propertyFetch($builder->var('this'), 'resolver'), $lazyMethod, [$value]);
120
        }
121
122
        if ($this->shared) {
123
            $createdValue = $this->triggerSharedBuild($id, $createdValue ?? $builder->val($value), $builder);
124
        }
125
126
        return $defNode->addStmt(new Return_($createdValue ?? $builder->val($value)));
127
    }
128
}
129