InterfaceDefinition   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 119
rs 10
wmc 21

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 4 1
A setImplement() 0 9 2
A complete() 0 14 4
A __construct() 0 3 1
A getResultDefinition() 0 3 1
A setResultDefinition() 0 5 1
A getResultType() 0 3 1
A getImplement() 0 3 1
A resolveType() 0 22 5
A generateMethod() 0 27 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.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 Biurad\DependencyInjection\Definitions;
19
20
use Nette;
21
use Nette\DI\Definitions;
22
use Nette\DI\ServiceCreationException;
23
24
/**
25
 * Definition of standard service.
26
 */
27
final class InterfaceDefinition extends Definitions\Definition
28
{
29
    /** @var Definitions\Definition|Definitions\ServiceDefinition */
30
    private $resultDefinition;
31
32
    public function __construct()
33
    {
34
        $this->resultDefinition = new Definitions\ServiceDefinition();
35
    }
36
37
    public function __clone()
38
    {
39
        parent::__clone();
40
        $this->resultDefinition = \unserialize(\serialize($this->resultDefinition));
41
    }
42
43
    /** @return static */
44
    public function setImplement(string $type)
45
    {
46
        if (!\interface_exists($type)) {
47
            throw new Nette\InvalidArgumentException("Service '{$this->getName()}': Interface '$type' not found.");
48
        }
49
50
        $this->resultDefinition->setName($this->getName());
0 ignored issues
show
Bug introduced by
It seems like $this->getName() can also be of type null; however, parameter $name of Nette\DI\Definitions\Definition::setName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
        $this->resultDefinition->setName(/** @scrutinizer ignore-type */ $this->getName());
Loading history...
51
52
        return parent::setType($type);
53
    }
54
55
    public function getImplement(): ?string
56
    {
57
        return $this->getType();
58
    }
59
60
    final public function getResultType(): ?string
61
    {
62
        return $this->resultDefinition->getType();
63
    }
64
65
    /** @return static */
66
    public function setResultDefinition(Definitions\Definition $definition)
67
    {
68
        $this->resultDefinition = $definition;
69
70
        return $this;
71
    }
72
73
    /** @return Definitions\ServiceDefinition */
74
    public function getResultDefinition(): Definitions\Definition
75
    {
76
        return $this->resultDefinition;
77
    }
78
79
    public function resolveType(Nette\DI\Resolver $resolver): void
80
    {
81
        $resultDef = $this->resultDefinition;
82
83
        try {
84
            $resolver->resolveDefinition($resultDef);
85
86
            return;
87
        } catch (ServiceCreationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
88
        }
89
90
        if (!$resultDef->getType()) {
91
            $interface = $this->getType();
92
93
            if (!$interface || !\interface_exists($interface)) {
94
                throw new ServiceCreationException('Type is missing in definition of service.');
95
            }
96
97
            $resultDef->setType($interface);
98
        }
99
100
        $resolver->resolveDefinition($resultDef);
101
    }
102
103
    public function complete(Nette\DI\Resolver $resolver): void
104
    {
105
        $resultDef = $this->resultDefinition;
106
107
        if ($resultDef instanceof Definitions\ServiceDefinition) {
108
            if ($resultDef->getEntity() instanceof Definitions\Reference && !$resultDef->getFactory()->arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $resultDef->getFactory()->arguments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
109
                $resultDef->setFactory([ // render as $container->createMethod()
110
                    new Definitions\Reference(Nette\DI\ContainerBuilder::THIS_CONTAINER),
111
                    Nette\DI\Container::getMethodName($resultDef->getEntity()->getValue()),
112
                ]);
113
            }
114
        }
115
116
        $resolver->completeDefinition($resultDef);
117
    }
118
119
    public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
120
    {
121
        $class = (new Nette\PhpGenerator\ClassType())
122
            ->addImplement($this->getType());
0 ignored issues
show
Bug introduced by
It seems like $this->getType() can also be of type null; however, parameter $name of Nette\PhpGenerator\ClassType::addImplement() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

122
            ->addImplement(/** @scrutinizer ignore-type */ $this->getType());
Loading history...
123
124
        if (!\is_string($entity = $this->resultDefinition->getEntity())) {
125
            throw new ServiceCreationException(
126
                \sprintf('Type entity must be a string of interface, \'%s\' given', \gettype($entity))
127
            );
128
        }
129
130
        $code = new Definitions\Statement('class', $this->resultDefinition->getFactory()->arguments);
131
        $code = $generator->formatStatement($code);
132
133
        $method->setBody('$service = ' . "{$code} " . $class->addExtend($entity) . ';');
134
135
        if (!empty($this->resultDefinition->getSetup())) {
136
            $setups = '';
137
138
            foreach ($this->resultDefinition->getSetup() as $setup) {
139
                $setups .= $generator->formatStatement($setup) . ";\n";
140
            }
141
142
            $method->addBody("\n" . $setups);
143
        }
144
145
        $method->addBody('return $service;');
146
    }
147
}
148