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

nette / php-generator / 20359118414

19 Dec 2025 03:43AM UTC coverage: 93.55% (-0.06%) from 93.614%
20359118414

push

github

dg
Factory::fromClassReflection() refactoring

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

18 existing lines in 2 files now uncovered.

1813 of 1938 relevant lines covered (93.55%)

0.94 hits per line

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

96.92
/src/PhpGenerator/PhpFile.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\PhpGenerator;
11

12
use Nette;
13
use function count;
14

15

16
/**
17
 * Definition of a PHP file.
18
 *
19
 * Generates:
20
 * - opening tag (<?php)
21
 * - doc comments
22
 * - one or more namespaces
23
 */
24
final class PhpFile
25
{
26
        use Traits\CommentAware;
27

28
        /** @var PhpNamespace[] */
29
        private array $namespaces = [];
30
        private bool $strictTypes = false;
31

32

33
        public static function fromCode(string $code): self
1✔
34
        {
35
                return (new Factory)->fromCode($code);
1✔
36
        }
37

38

39
        /**
40
         * Adds a namespace, class-like type, or function to the file. If the item has a namespace,
41
         * it will be added to that namespace (creating it if needed).
42
         */
43
        public function add(ClassType|InterfaceType|TraitType|EnumType|GlobalFunction|PhpNamespace $item): static
1✔
44
        {
45
                if ($item instanceof PhpNamespace) {
1✔
46
                        if (isset($this->namespaces[$name = $item->getName()])) {
1✔
47
                                throw new Nette\InvalidStateException("Namespace '$name' already exists in the file.");
1✔
48
                        }
49
                        $this->namespaces[$name] = $item;
1✔
50
                        $this->refreshBracketedSyntax();
1✔
51

52
                } elseif ($item instanceof GlobalFunction) {
1✔
53
                        $this->addNamespace('')->add($item);
1✔
54

55
                } else {
56
                        $this->addNamespace($item->getNamespace()?->getName() ?? '')->add($item);
1✔
57
                }
58

59
                return $this;
1✔
60
        }
61

62

63
        /**
64
         * Adds a class to the file. If it already exists, throws an exception.
65
         * As a parameter, pass the full name with namespace.
66
         */
67
        public function addClass(string $name): ClassType
1✔
68
        {
69
                return $this
70
                        ->addNamespace(Helpers::extractNamespace($name))
1✔
71
                        ->addClass(Helpers::extractShortName($name));
1✔
72
        }
73

74

75
        /**
76
         * Adds an interface to the file. If it already exists, throws an exception.
77
         * As a parameter, pass the full name with namespace.
78
         */
79
        public function addInterface(string $name): InterfaceType
1✔
80
        {
81
                return $this
82
                        ->addNamespace(Helpers::extractNamespace($name))
1✔
83
                        ->addInterface(Helpers::extractShortName($name));
1✔
84
        }
85

86

87
        /**
88
         * Adds a trait to the file. If it already exists, throws an exception.
89
         * As a parameter, pass the full name with namespace.
90
         */
91
        public function addTrait(string $name): TraitType
1✔
92
        {
93
                return $this
94
                        ->addNamespace(Helpers::extractNamespace($name))
1✔
95
                        ->addTrait(Helpers::extractShortName($name));
1✔
96
        }
97

98

99
        /**
100
         * Adds an enum to the file. If it already exists, throws an exception.
101
         * As a parameter, pass the full name with namespace.
102
         */
103
        public function addEnum(string $name): EnumType
1✔
104
        {
105
                return $this
106
                        ->addNamespace(Helpers::extractNamespace($name))
1✔
107
                        ->addEnum(Helpers::extractShortName($name));
1✔
108
        }
109

110

111
        /**
112
         * Adds a function to the file. If it already exists, throws an exception.
113
         * As a parameter, pass the full name with namespace.
114
         */
115
        public function addFunction(string $name): GlobalFunction
1✔
116
        {
117
                return $this
118
                        ->addNamespace(Helpers::extractNamespace($name))
1✔
119
                        ->addFunction(Helpers::extractShortName($name));
1✔
120
        }
121

122

123
        /**
124
         * Adds a namespace to the file. If it already exists, it returns the existing one.
125
         */
126
        public function addNamespace(string|PhpNamespace $namespace): PhpNamespace
1✔
127
        {
128
                if ($namespace instanceof PhpNamespace) {
1✔
129
                        trigger_error('Passing PhpNamespace object to ' . __METHOD__ . '() is deprecated, use add() instead.', E_USER_DEPRECATED);
1✔
130
                        $res = $this->namespaces[$namespace->getName()] = $namespace;
1✔
131
                } else {
132
                        $res = $this->namespaces[$namespace] ??= new PhpNamespace($namespace);
1✔
133
                }
134
                $this->refreshBracketedSyntax();
1✔
135
                return $res;
1✔
136
        }
137

138

139
        /**
140
         * Removes the namespace from the file.
141
         */
142
        public function removeNamespace(string|PhpNamespace $namespace): static
1✔
143
        {
144
                if ($namespace instanceof PhpNamespace) {
1✔
UNCOV
145
                        trigger_error('Passing PhpNamespace object to ' . __METHOD__ . '() is deprecated, use string name instead.', E_USER_DEPRECATED);
×
UNCOV
146
                        $name = $namespace->getName();
×
147
                } else {
148
                        $name = $namespace;
1✔
149
                }
150

151
                unset($this->namespaces[$name]);
1✔
152
                return $this;
1✔
153
        }
154

155

156
        /** @return PhpNamespace[] */
157
        public function getNamespaces(): array
158
        {
159
                return $this->namespaces;
1✔
160
        }
161

162

163
        /** @return (ClassType|InterfaceType|TraitType|EnumType)[] */
164
        public function getClasses(): array
165
        {
166
                $classes = [];
1✔
167
                foreach ($this->namespaces as $n => $namespace) {
1✔
168
                        $n .= $n ? '\\' : '';
1✔
169
                        foreach ($namespace->getClasses() as $c => $class) {
1✔
170
                                $classes[$n . $c] = $class;
1✔
171
                        }
172
                }
173

174
                return $classes;
1✔
175
        }
176

177

178
        /** @return GlobalFunction[] */
179
        public function getFunctions(): array
180
        {
181
                $functions = [];
1✔
182
                foreach ($this->namespaces as $n => $namespace) {
1✔
183
                        $n .= $n ? '\\' : '';
1✔
184
                        foreach ($namespace->getFunctions() as $f => $function) {
1✔
185
                                $functions[$n . $f] = $function;
1✔
186
                        }
187
                }
188

189
                return $functions;
1✔
190
        }
191

192

193
        /**
194
         * Adds a use statement to the file, to the global namespace.
195
         */
196
        public function addUse(string $name, ?string $alias = null, string $of = PhpNamespace::NameNormal): static
1✔
197
        {
198
                $this->addNamespace('')->addUse($name, $alias, $of);
1✔
199
                return $this;
1✔
200
        }
201

202

203
        /**
204
         * Adds declare(strict_types=1) to output.
205
         */
206
        public function setStrictTypes(bool $state = true): static
1✔
207
        {
208
                $this->strictTypes = $state;
1✔
209
                return $this;
1✔
210
        }
211

212

213
        public function hasStrictTypes(): bool
214
        {
215
                return $this->strictTypes;
1✔
216
        }
217

218

219
        public function __toString(): string
220
        {
221
                return (new Printer)->printFile($this);
1✔
222
        }
223

224

225
        private function refreshBracketedSyntax(): void
226
        {
227
                foreach ($this->namespaces as $namespace) {
1✔
228
                        $namespace->setBracketedSyntax(count($this->namespaces) > 1 && isset($this->namespaces['']));
1✔
229
                }
230
        }
1✔
231
}
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