• 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

96.3
/src/DI/Definitions/AccessorDefinition.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
use Nette\DI\Helpers;
14
use Nette\Utils\Type;
15

16

17
/**
18
 * Accessor definition.
19
 */
20
final class AccessorDefinition extends Definition
21
{
22
        private const MethodGet = 'get';
23

24
        private ?Reference $reference = null;
25

26

27
        public function setImplement(string $interface): static
1✔
28
        {
29
                if (!interface_exists($interface)) {
1✔
30
                        throw new Nette\InvalidArgumentException(sprintf(
1✔
31
                                "[%s]\nInterface '%s' not found.",
1✔
32
                                $this->getDescriptor(),
1✔
33
                                $interface,
34
                        ));
35
                }
36

37
                $rc = new \ReflectionClass($interface);
1✔
38

39
                $method = $rc->getMethods()[0] ?? null;
1✔
40
                if (
41
                        !$method
1✔
42
                        || $method->isStatic()
1✔
43
                        || $method->getName() !== self::MethodGet
1✔
44
                        || count($rc->getMethods()) > 1
1✔
45
                ) {
46
                        throw new Nette\InvalidArgumentException(sprintf(
1✔
47
                                "[%s]\nInterface %s must have just one non-static method get().",
1✔
48
                                $this->getDescriptor(),
1✔
49
                                $interface,
50
                        ));
51
                } elseif ($method->getNumberOfParameters()) {
1✔
52
                        throw new Nette\InvalidArgumentException(sprintf(
1✔
53
                                "[%s]\nMethod %s::get() must have no parameters.",
1✔
54
                                $this->getDescriptor(),
1✔
55
                                $interface,
56
                        ));
57
                }
58

59
                Helpers::ensureClassType(Type::fromReflection($method), "return type of $interface::get()", $this->getDescriptor());
1✔
60
                return parent::setType($interface);
1✔
61
        }
62

63

64
        public function getImplement(): ?string
65
        {
66
                return $this->getType();
1✔
67
        }
68

69

70
        public function setReference(string|Reference $reference): static
1✔
71
        {
72
                if ($reference instanceof Reference) {
1✔
73
                        $this->reference = $reference;
×
74
                } else {
75
                        $this->reference = str_starts_with($reference, '@')
1✔
76
                                ? new Reference(substr($reference, 1))
1✔
77
                                : Reference::fromType($reference);
1✔
78
                }
79

80
                return $this;
1✔
81
        }
82

83

84
        public function getReference(): ?Reference
85
        {
86
                return $this->reference;
1✔
87
        }
88

89

90
        public function resolveType(Nette\DI\Resolver $resolver): void
1✔
91
        {
92
        }
1✔
93

94

95
        public function complete(Nette\DI\Resolver $resolver): void
1✔
96
        {
97
                if (!$this->reference) {
1✔
98
                        if (!$this->getType()) {
1✔
99
                                throw new Nette\DI\ServiceCreationException('Type is missing in definition of service.');
×
100
                        }
101

102
                        $method = new \ReflectionMethod($this->getType(), self::MethodGet);
1✔
103
                        $this->setReference(Type::fromReflection($method)->getSingleName());
1✔
104
                }
105

106
                $this->reference = $resolver->normalizeReference($this->reference);
1✔
107
        }
1✔
108

109

110
        public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
1✔
111
        {
112
                $class = (new Nette\PhpGenerator\ClassType)
1✔
113
                        ->addImplement($this->getType());
1✔
114

115
                $class->addProperty('container')
1✔
116
                        ->setPrivate();
1✔
117

118
                $class->addMethod('__construct')
1✔
119
                        ->addBody('$this->container = $container;')
1✔
120
                        ->addParameter('container')
1✔
121
                        ->setType($generator->getClassName());
1✔
122

123
                $rm = new \ReflectionMethod($this->getType(), self::MethodGet);
1✔
124

125
                $class->addMethod(self::MethodGet)
1✔
126
                        ->setBody('return $this->container->getService(?);', [$this->reference->getValue()])
1✔
127
                        ->setReturnType((string) Type::fromReflection($rm));
1✔
128

129
                $method->setBody('return new class ($this) ' . $class . ';');
1✔
130
        }
1✔
131
}
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