Completed
Pull Request — 1.2 (#12)
by David
01:36
created

cloneWithoutIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TheCodingMachine\Yaco\ServiceProvider;
4
5
use TheCodingMachine\Yaco\Definition\DumpableInterface;
6
use TheCodingMachine\Yaco\Definition\InlineEntry;
7
use TheCodingMachine\Yaco\Definition\InlineEntryInterface;
8
9
/**
10
 * Fetches a service from the service-providers registry.
11
 */
12
class ExtendServiceFromRegistryDefinition implements DumpableInterface
13
{
14
    /**
15
     * The identifier of the instance in the container.
16
     *
17
     * @var string|null
18
     */
19
    private $identifier;
20
21
    /**
22
     * The key of the service provider in the registry.
23
     *
24
     * @var int
25
     */
26
    private $serviceProviderKey;
27
28
    /**
29
     * @var DumpableInterface|null
30
     */
31
    private $previousDefinition;
32
33
    /**
34
     * @var string
35
     */
36
    private $serviceName;
37
38
    /**
39
     * @param string|null                    $identifier
40
     * @param string                         $serviceName
41
     * @param int                            $serviceProviderKey
42
     * @param DumpableInterface|null $previousDefinition
43
     */
44
    public function __construct(?string $identifier, string $serviceName, int $serviceProviderKey, DumpableInterface $previousDefinition = null)
45
    {
46
        $this->identifier = $identifier;
47
        $this->serviceName = $serviceName;
48
        $this->serviceProviderKey = $serviceProviderKey;
49
        $this->previousDefinition = $previousDefinition;
50
    }
51
52
    /**
53
     * Returns the identifier for this object in the container.
54
     * If null, classes consuming this definition should assume the definition must be inlined.
55
     *
56
     * @return string|null
57
     */
58
    public function getIdentifier(): ?string
59
    {
60
        return $this->identifier;
61
    }
62
63
    /**
64
     * Returns an InlineEntryInterface object representing the PHP code necessary to generate
65
     * the container entry.
66
     *
67
     * @param string $containerVariable The name of the variable that allows access to the container instance. For instance: "$container", or "$this->container"
68
     * @param array  $usedVariables     An array of variables that are already used and that should not be used when generating this code.
69
     *
70
     * @return InlineEntryInterface
71
     */
72
    public function toPhpCode(string $containerVariable, array $usedVariables = array()): InlineEntryInterface
73
    {
74
        $previousCode = '';
0 ignored issues
show
Unused Code introduced by
$previousCode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
        if ($this->previousDefinition) {
76
            $previousCode = ', '.$this->previousDefinition->toPhpCode($containerVariable, $usedVariables)->getExpression();
77
        } else {
78
            $previousCode = ', null';
79
        }
80
        $code = sprintf('$this->registry->extendService(%s, %s, $this->delegateLookupContainer%s)', var_export($this->serviceProviderKey, true),
81
            var_export($this->serviceName, true), $previousCode);
82
83
        return new InlineEntry($code, null, $usedVariables);
84
    }
85
86
    public function cloneWithoutIdentifier(): self
87
    {
88
        return new self(null, $this->serviceName, $this->serviceProviderKey, $this->previousDefinition);
89
    }
90
}
91