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

nette / di / 6739042234

02 Nov 2023 10:31PM UTC coverage: 93.846% (+41.8%) from 52.037%
6739042234

push

github

dg
Option 'class' is allowed again

Partially reverts commit 046f89cc3.

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

2257 of 2405 relevant lines covered (93.85%)

0.94 hits per line

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

98.18
/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
        /** @var Reference|null */
25
        private $reference;
26

27

28
        /** @return static */
29
        public function setImplement(string $interface)
1✔
30
        {
31
                if (!interface_exists($interface)) {
1✔
32
                        throw new Nette\InvalidArgumentException(sprintf(
1✔
33
                                "Service '%s': Interface '%s' not found.",
1✔
34
                                $this->getName(),
1✔
35
                                $interface
36
                        ));
37
                }
38

39
                $rc = new \ReflectionClass($interface);
1✔
40

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

61
                try {
62
                        Helpers::ensureClassType(Type::fromReflection($method), "return type of $interface::get()");
1✔
63
                } catch (Nette\DI\ServiceCreationException $e) {
1✔
64
                        trigger_error($e->getMessage(), E_USER_DEPRECATED);
1✔
65
                }
66

67
                return parent::setType($interface);
1✔
68
        }
69

70

71
        public function getImplement(): ?string
72
        {
73
                return $this->getType();
1✔
74
        }
75

76

77
        /**
78
         * @param  string|Reference  $reference
79
         * @return static
80
         */
81
        public function setReference($reference)
82
        {
83
                if ($reference instanceof Reference) {
1✔
84
                        $this->reference = $reference;
×
85
                } else {
86
                        $this->reference = substr($reference, 0, 1) === '@'
1✔
87
                                ? new Reference(substr($reference, 1))
1✔
88
                                : Reference::fromType($reference);
1✔
89
                }
90

91
                return $this;
1✔
92
        }
93

94

95
        public function getReference(): ?Reference
96
        {
97
                return $this->reference;
1✔
98
        }
99

100

101
        public function resolveType(Nette\DI\Resolver $resolver): void
1✔
102
        {
103
        }
1✔
104

105

106
        public function complete(Nette\DI\Resolver $resolver): void
1✔
107
        {
108
                if (!$this->reference) {
1✔
109
                        $interface = $this->getType();
1✔
110
                        $method = new \ReflectionMethod($interface, self::MethodGet);
1✔
111
                        $type = Type::fromReflection($method) ?? Helpers::getReturnTypeAnnotation($method);
1✔
112
                        $this->setReference(Helpers::ensureClassType($type, "return type of $interface::get()"));
1✔
113
                }
114

115
                $this->reference = $resolver->normalizeReference($this->reference);
1✔
116
        }
1✔
117

118

119
        public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
1✔
120
        {
121
                $class = (new Nette\PhpGenerator\ClassType)
1✔
122
                        ->addImplement($this->getType());
1✔
123

124
                $class->addProperty('container')
1✔
125
                        ->setPrivate();
1✔
126

127
                $class->addMethod('__construct')
1✔
128
                        ->addBody('$this->container = $container;')
1✔
129
                        ->addParameter('container')
1✔
130
                        ->setType($generator->getClassName());
1✔
131

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

134
                $class->addMethod(self::MethodGet)
1✔
135
                        ->setBody('return $this->container->getService(?);', [$this->reference->getValue()])
1✔
136
                        ->setReturnType((string) Type::fromReflection($rm));
1✔
137

138
                $method->setBody('return new class ($this) ' . $class . ';');
1✔
139
        }
1✔
140
}
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