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

nette / di / 40354306213

26 Oct 2026 03:56PM UTC coverage: 90.858% (+19.0%) from 71.881%
40354306213

push

github

dg
removed Statement compatibility shims from expression classes WIP

3 of 3 new or added lines in 2 files covered. (100.0%)

188 existing lines in 38 files now uncovered.

2733 of 3008 relevant lines covered (90.86%)

0.91 hits per line

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

85.92
/src/DI/Extensions/InjectExtension.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\Extensions;
9

10
use Nette;
11
use Nette\DI;
12
use Nette\DI\Attributes\Hook;
13
use Nette\DI\{ContainerBuilder, Definitions, Phase};
14
use Nette\Utils\Reflection;
15
use function sprintf;
16

17

18
/**
19
 * Calls inject methods and fills #[Inject] properties.
20
 */
21
final class InjectExtension extends DI\CompilerExtension
22
{
23
        public const TagInject = 'nette.inject';
24

25
        #[\Deprecated('use InjectExtension::TagInject')]
26
        public const TAG_INJECT = self::TagInject;
27

28

29
        public function getConfigSchema(): Nette\Schema\Schema
30
        {
31
                return Nette\Schema\Expect::structure([]);
1 ✔
32
        }
33

34

35
        #[Hook(Phase::Modify, after: '*')]
36
        public function doProcessInjectAttributes(ContainerBuilder $builder): void
37
        {
38
                foreach ($builder->getAll() as $def) {
1 ✔
39
                        if ($def->getTag(self::TagInject)) {
1 ✔
40
                                $def = $def instanceof Definitions\FactoryDefinition
1 ✔
41
                                        ? $def->getResultDefinition()
1 ✔
42
                                        : $def;
1 ✔
43
                                if ($def instanceof Definitions\ServiceDefinition) {
1 ✔
44
                                        $this->updateDefinition($def);
1 ✔
45
                                }
46
                        }
47
                }
UNCOV
48
        }
×
49

50

51
        private function updateDefinition(Definitions\ServiceDefinition $def): void
52
        {
53
                $resolvedType = $def->getCreator()->resolveType(new DI\Compiler\Resolver($this->getContainerBuilder()));
1 ✔
54
                $class = $resolvedType && $def->getType() && is_subclass_of($resolvedType, $def->getType())
1 ✔
55
                        ? $resolvedType
1 ✔
56
                        : $def->getType();
1 ✔
57
                if (!$class) {
1 ✔
58
                        return;
×
59
                }
60

61
                $setups = $def->getSetup();
1 ✔
62

63
                foreach (self::getInjectProperties($class) as $property => $type) {
1 ✔
64
                        $builder = $this->getContainerBuilder();
1 ✔
65
                        $inject = new DI\Expressions\PropertyAccess(
1 ✔
66
                                di\self(),
1 ✔
67
                                $property,
68
                                DI\Expressions\PropertyMode::Assign,
1 ✔
69
                                di\wire((string) $type),
1 ✔
70
                        );
71
                        foreach ($setups as $key => $setup) {
1 ✔
72
                                if ($setup->getEntity() == $inject->getEntity()) { // intentionally ==
1 ✔
73
                                        $inject = $setup;
×
74
                                        $builder = null;
×
75
                                        unset($setups[$key]);
×
76
                                }
77
                        }
78

79
                        if ($builder) {
1 ✔
80
                                self::checkType($class, $property, $type, $builder);
1 ✔
81
                        }
82
                        array_unshift($setups, $inject);
×
83
                }
84

85
                foreach (array_reverse(self::getInjectMethods($class)) as $method) {
1 ✔
86
                        $inject = di\self()->method($method);
1 ✔
87
                        foreach ($setups as $key => $setup) {
1 ✔
88
                                if ($setup->getEntity() == $inject->getEntity()) { // intentionally ==
×
89
                                        $inject = $setup;
×
90
                                        unset($setups[$key]);
×
91
                                }
92
                        }
93

94
                        array_unshift($setups, $inject);
1 ✔
95
                }
96

97
                $def->setSetup($setups);
1 ✔
UNCOV
98
        }
×
99

100

101
        /**
102
         * Returns list of inject method names, ordered from parent to child class.
103
         * @return string[]
104
         * @internal
105
         */
106
        public static function getInjectMethods(string $class): array
107
        {
108
                $classes = [];
1 ✔
109
                foreach (get_class_methods($class) as $name) {
1 ✔
110
                        if (str_starts_with($name, 'inject')) {
1 ✔
111
                                $classes[$name] = (new \ReflectionMethod($class, $name))->getDeclaringClass()->name;
1 ✔
112
                        }
113
                }
114

115
                $methods = array_keys($classes);
1 ✔
116
                uksort($classes, fn(string $a, string $b): int => $classes[$a] === $classes[$b]
1 ✔
117
                        ? array_search($a, $methods, strict: true) <=> array_search($b, $methods, strict: true)
1 ✔
118
                        : (is_a($classes[$a], $classes[$b], allow_string: true) ? 1 : -1));
1 ✔
119
                return array_keys($classes);
1 ✔
120
        }
121

122

123
        /**
124
         * Returns list of injectable properties with their types.
125
         * @return array<string, class-string>
126
         * @internal
127
         */
128
        public static function getInjectProperties(string $class): array
129
        {
130
                $res = [];
1 ✔
131
                foreach ((new \ReflectionClass($class))->getProperties() as $rp) {
1 ✔
132
                        if ($rp->getAttributes(DI\Attributes\Inject::class)) {
1 ✔
133
                                if (!$rp->isPublic() || $rp->isStatic() || $rp->isReadOnly()) {
1 ✔
134
                                        throw new Nette\InvalidStateException(sprintf(
1 ✔
135
                                                'Property %s for injection must not be static, readonly and must be public.',
136
                                                Reflection::toString($rp),
1 ✔
137
                                        ));
138
                                }
139

140
                                $res[$rp->getName()] = DI\Helpers::ensureClassType(
1 ✔
141
                                        Nette\Utils\Type::fromReflection($rp),
1 ✔
142
                                        'type of property ' . Reflection::toString($rp),
1 ✔
143
                                );
144
                        }
145
                }
146

147
                ksort($res);
1 ✔
148
                return $res;
1 ✔
149
        }
150

151

152
        /**
153
         * Calls inject methods and fills inject properties on the given service.
154
         */
155
        public static function callInjects(DI\Container $container, object $service): void
156
        {
157
                foreach (self::getInjectMethods($service::class) as $method) {
1 ✔
158
                        $container->callMethod([$service, $method]);
1 ✔
159
                }
160

161
                foreach (self::getInjectProperties($service::class) as $property => $type) {
1 ✔
162
                        self::checkType($service, $property, $type, $container);
1 ✔
163
                        $service->$property = $container->getByType($type);
1 ✔
164
                }
165
        }
1 ✔
166

167

168
        private static function checkType(
169
                object|string $class,
170
                string $name,
171
                ?string $type,
172
                DI\Container|DI\ContainerBuilder $container,
173
        ): void
174
        {
175
                if (!$container->getByType($type, throw: false)) {
1 ✔
176
                        throw new Nette\DI\MissingServiceException(sprintf(
1 ✔
177
                                'Service of type %s required by %s not found. Did you add it to configuration file?',
178
                                $type,
1 ✔
179
                                Reflection::toString(new \ReflectionProperty($class, $name)),
1 ✔
180
                        ));
181
                }
182
        }
1 ✔
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