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

nette / di / 4405606637

pending completion
4405606637

push

github

David Grudl
more self explanatory message for factory and service mismatch (closes #199) (#284)

8 of 8 new or added lines in 1 file covered. (100.0%)

2118 of 2238 relevant lines covered (94.64%)

0.95 hits per line

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

90.74
/src/DI/Definitions/Definition.php
1
<?php
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
declare(strict_types=1);
9

10
namespace Nette\DI\Definitions;
11

12
use Nette;
13

14

15
/**
16
 * Definition used by ContainerBuilder.
17
 */
18
abstract class Definition
19
{
20
        use Nette\SmartObject;
21

22
        private ?string $name = null;
23
        private ?string $type = null;
24
        private array $tags = [];
25

26
        /** @var bool|string[] */
27
        private bool|array $autowired = true;
28

29
        /** @var ?callable */
30
        private $notifier;
31

32

33
        /**
34
         * @internal  This is managed by ContainerBuilder and should not be called by user
35
         */
36
        final public function setName(string $name): static
1✔
37
        {
38
                if ($this->name) {
1✔
39
                        throw new Nette\InvalidStateException('Name already has been set.');
×
40
                }
41

42
                $this->name = $name;
1✔
43
                return $this;
1✔
44
        }
45

46

47
        final public function getName(): ?string
48
        {
49
                return $this->name;
1✔
50
        }
51

52

53
        final public function isAnonymous(): bool
54
        {
55
                return !$this->name || ctype_digit($this->name);
1✔
56
        }
57

58

59
        public function getDescriptor(): string
60
        {
61
                if (!$this->isAnonymous()) {
1✔
62
                        return "Service '$this->name'" . ($this->type ? " of type $this->type" : '');
1✔
63

64
                } elseif ($this->type) {
1✔
65
                        return "Service of type $this->type";
1✔
66

67
                } elseif ($this->name) {
1✔
68
                        return "Service '$this->name'";
1✔
69

70
                } else {
71
                        return 'Service ?';
1✔
72
                }
73
        }
74

75

76
        protected function setType(?string $type): static
1✔
77
        {
78
                if ($this->autowired && $this->notifier && $this->type !== $type) {
1✔
79
                        ($this->notifier)();
1✔
80
                }
81

82
                if ($type === null) {
1✔
83
                        $this->type = null;
1✔
84
                } elseif (!class_exists($type) && !interface_exists($type)) {
1✔
85
                        throw new Nette\InvalidArgumentException(sprintf(
1✔
86
                                "[%s]\nClass or interface '%s' not found.",
1✔
87
                                $this->getDescriptor(),
1✔
88
                                $type,
89
                        ));
90
                } else {
91
                        $this->type = Nette\DI\Helpers::normalizeClass($type);
1✔
92
                }
93

94
                return $this;
1✔
95
        }
96

97

98
        final public function getType(): ?string
99
        {
100
                return $this->type;
1✔
101
        }
102

103

104
        final public function setTags(array $tags): static
1✔
105
        {
106
                $this->tags = $tags;
1✔
107
                return $this;
1✔
108
        }
109

110

111
        final public function getTags(): array
112
        {
113
                return $this->tags;
1✔
114
        }
115

116

117
        final public function addTag(string $tag, mixed $attr = true): static
1✔
118
        {
119
                $this->tags[$tag] = $attr;
1✔
120
                return $this;
1✔
121
        }
122

123

124
        final public function getTag(string $tag): mixed
1✔
125
        {
126
                return $this->tags[$tag] ?? null;
1✔
127
        }
128

129

130
        final public function setAutowired(bool|string|array $state = true): static
1✔
131
        {
132
                if ($this->notifier && $this->autowired !== $state) {
1✔
133
                        ($this->notifier)();
1✔
134
                }
135

136
                $this->autowired = is_string($state) || is_array($state)
1✔
137
                        ? (array) $state
1✔
138
                        : (bool) $state;
1✔
139
                return $this;
1✔
140
        }
141

142

143
        /** @return bool|string[] */
144
        final public function getAutowired(): bool|array
145
        {
146
                return $this->autowired;
1✔
147
        }
148

149

150
        public function setExported(bool $state = true): static
151
        {
152
                return $this->addTag('nette.exported', $state);
×
153
        }
154

155

156
        public function isExported(): bool
157
        {
158
                return (bool) $this->getTag('nette.exported');
×
159
        }
160

161

162
        public function __clone()
163
        {
164
                $this->notifier = $this->name = null;
1✔
165
        }
1✔
166

167

168
        /********************* life cycle ****************d*g**/
169

170

171
        abstract public function resolveType(Nette\DI\Resolver $resolver): void;
172

173

174
        abstract public function complete(Nette\DI\Resolver $resolver): void;
175

176

177
        abstract public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void;
178

179

180
        final public function setNotifier(?callable $notifier): void
1✔
181
        {
182
                $this->notifier = $notifier;
1✔
183
        }
1✔
184

185

186
        /********************* deprecated stuff from former ServiceDefinition ****************d*g**/
187

188

189
        /** @deprecated Use setType() */
190
        public function setClass(?string $type)
1✔
191
        {
192
                return $this->setType($type);
1✔
193
        }
194

195

196
        /** @deprecated Use getType() */
197
        public function getClass(): ?string
198
        {
199
                return $this->getType();
×
200
        }
201

202

203
        /** @deprecated Use getAutowired() */
204
        public function isAutowired()
205
        {
206
                return $this->autowired;
×
207
        }
208
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc