• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / di / 40512459939

27 Oct 2026 01:29PM UTC coverage: 90.575% (-0.3%) from 90.867%
40512459939

push

github

dg
removed Statement compatibility shims from expression classes WIP

8 of 8 new or added lines in 6 files covered. (100.0%)

48 existing lines in 8 files now uncovered.

2739 of 3024 relevant lines covered (90.58%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/src/DI/Definitions/Statement.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\DI\Definitions;
9

10
use Nette;
11
use Nette\DI;
12
use Nette\DI\Compiler\Resolver;
13
use Nette\DI\{Definition, Expression, ServiceCreationException};
14
use Nette\DI\Expressions\Reference;
15
use Nette\Utils\Validators;
16
use function is_array, is_string, sprintf;
17

18

19
/**
20
 * Assignment or calling statement.
21
 *
22
 * @property string|array{string|Definition|Expression,string}|Definition|Reference|null $entity
23
 */
24
final class Statement extends Expression
25
{
26
        use Nette\SmartObject;
27

28
        public function __construct(
29
                private string|array|Definition|Reference|null $entity,
1 ✔
30
                /** @var array<mixed> */
31
                public array $arguments = [],
1 ✔
32
        ) {
33
                if (
34
                        $entity !== null
1 ✔
35
                        && !is_string($entity) // Class, @service, not, tags, types, PHP literal, entity::member
1 ✔
36
                        && !$entity instanceof Definition
1 ✔
37
                        && !$entity instanceof Reference
1 ✔
38
                        && !(is_array($entity)
1 ✔
39
                                && array_keys($entity) === [0, 1]
1 ✔
40
                                && (is_string($entity[0])
1 ✔
41
                                        || $entity[0] instanceof Expression
1 ✔
42
                                        || $entity[0] instanceof Definition)
1 ✔
43
                        )
44
                ) {
45
                        throw new Nette\InvalidArgumentException('Argument is not valid Statement entity.');
1 ✔
46
                }
47

48
                // normalize Class::method to [Class, method]
49
                if (is_string($entity) && str_contains($entity, '::') && !str_contains($entity, '?')) {
1 ✔
50
                        $entity = explode('::', $entity, 2);
1 ✔
51
                }
52

53
                if (is_string($entity) && str_starts_with($entity, '@')) { // normalize @service to Reference
1 ✔
54
                        $entity = new Reference(substr($entity, 1));
1 ✔
55
                } elseif (is_array($entity) && is_string($entity[0]) && str_starts_with($entity[0], '@')) {
1 ✔
56
                        $entity[0] = new Reference(substr($entity[0], 1));
1 ✔
57
                }
58

59
                $this->entity = $entity;
1 ✔
60
        }
1 ✔
61

62

63
        /** @return string|array{string|Definition|Expression, string}|Definition|Reference|null */
64
        public function getEntity(): string|array|Definition|Reference|null
65
        {
66
                return $this->entity;
1 ✔
67
        }
68

69

70
        public function resolveType(Resolver $resolver): ?string
71
        {
72
                $entity = $this->normalizeEntity($resolver);
1 ✔
73
                return $this->specialize($entity)->resolveType($resolver);
1 ✔
74
        }
75

76

77
        /**
78
         * Returns a completed (resolved and autowired) version of the statement. The original statement is left unchanged.
79
         */
80
        public function complete(Resolver $resolver): DI\Expression
81
        {
82
                $entity = $this->normalizeEntity($resolver);
1 ✔
83
                if ($entity instanceof Reference && $this->arguments) {
1 ✔
84
                        throw new ServiceCreationException(sprintf(
1 ✔
85
                                'Parameters were passed to reference @%s, although references cannot have any parameters.',
86
                                $entity->getValue(),
1 ✔
87
                        ));
88
                }
89
                return $this->specialize($entity)->complete($resolver);
1 ✔
90
        }
91

92

93
        /**
94
         * Maps the statement's entity + arguments to the corresponding specialized expression node.
95
         * The shared mapping used by complete() and generateCode().
96
         */
97
        private function specialize(string|array|Reference $entity): DI\Expression
98
        {
99
                switch (true) {
100
                        case is_string($entity) && str_contains($entity, '?'): // PHP literal
1 ✔
101
                                return new DI\Expressions\PhpCode($entity, $this->arguments);
1 ✔
102

103
                        case in_array($entity, DI\Expressions\SpecialFunction::Functions, true): // not(), int(), ...
1 ✔
104
                                return new DI\Expressions\SpecialFunction($entity, $this->arguments);
1 ✔
105

106
                        case is_string($entity): // create class
1 ✔
107
                                return new DI\Expressions\Instantiation($entity, $this->arguments);
1 ✔
108

109
                        case $entity instanceof Reference: // produce the referenced service fresh via the container factory
1 ✔
110
                                return new Reference($entity->getValue(), shared: false);
1 ✔
111

112
                        case is_array($entity) && is_string($entity[1]) && str_starts_with($entity[1], '$'): // property access
1 ✔
113
                                $name = substr($entity[1], 1);
1 ✔
114
                                $append = str_ends_with($name, '[]');
1 ✔
115
                                if ($append) {
1 ✔
116
                                        $name = substr($name, 0, -2);
1 ✔
117
                                }
118

119
                                Validators::assert($this->arguments, 'list:0..1', "setup arguments for '$entity[1]'");
1 ✔
120
                                if ($append && !$this->arguments) {
1 ✔
121
                                        throw new ServiceCreationException(sprintf('Missing argument for $%s[].', $name));
1 ✔
122
                                }
123

124
                                $mode = match (true) {
1 ✔
125
                                        $append => DI\Expressions\PropertyMode::Append,
1 ✔
126
                                        (bool) $this->arguments => DI\Expressions\PropertyMode::Assign,
1 ✔
UNCOV
127
                                        default => DI\Expressions\PropertyMode::Read,
×
128
                                };
129
                                return new DI\Expressions\PropertyAccess($entity[0], $name, $mode, $this->arguments[0] ?? null);
1 ✔
130

131
                        default: // is_array: function, static or method call
132
                                return new DI\Expressions\Call(
1 ✔
133
                                        $entity[0] === '' ? null : $entity[0],
1 ✔
134
                                        $entity[1],
1 ✔
135
                                        $this->arguments,
1 ✔
136
                                );
137
                }
138
        }
139

140

141
        /**
142
         * Returns normalized entity: literal, Class, Reference, [Class, member], [, globalFunc], [Reference, member], [Statement, member]
143
         * @return string|array{string|Expression, string}|Reference|null
144
         */
145
        private function normalizeEntity(Resolver $resolver): string|array|Reference|null
146
        {
147
                $entity = $this->entity;
1 ✔
148
                if (is_array($entity)) {
1 ✔
149
                        $item = &$entity[0];
1 ✔
150
                } else {
151
                        $item = &$entity;
1 ✔
152
                }
153

154
                if ($item instanceof Definition) {
1 ✔
155
                        $name = $item->getName(throw: false);
1 ✔
156
                        if ($name === null || $resolver->getContainerBuilder()->getDefinition($name) !== $item) {
1 ✔
157
                                throw new ServiceCreationException(sprintf("Service '%s' does not match the expected service.", $name));
1 ✔
158
                        }
159
                        $item = new Reference($name);
1 ✔
160
                }
161

162
                return $entity;
1 ✔
163
        }
164

165

166
        public function transformValues(callable $cb): static
167
        {
168
                return new self($cb($this->entity), $cb($this->arguments));
1 ✔
169
        }
170

171

172
        /**
173
         * Formats PHP code for class instantiating, function calling or property setting.
174
         */
175
        public function generateCode(DI\Compiler\PhpGenerator $generator): string
176
        {
UNCOV
177
                if ($this->entity === null || $this->entity instanceof Definition) {
×
UNCOV
178
                        throw new Nette\InvalidStateException;
×
179
                }
180

UNCOV
181
                return $this->specialize($this->entity)->generateCode($generator);
×
182
        }
183
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc