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

orisai / phpstan-nette / 43698240932

15 Nov 2026 05:30AM UTC coverage: 78.278% (-0.9%) from 79.16%
43698240932

push

github

mabar
Record the measured cost of the reflection cache bypass

21744 of 27778 relevant lines covered (78.28%)

84.06 hits per line

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

58.77
/src/Configuration/ConfigurationGuard.php
1
<?php declare(strict_types = 1);
2

3
namespace OriPhpstan\Nette\Configuration;
4

5
use Latte\Engine;
6
use OriPhpstan\Nette\Forms\Catalog\Stub\FormModifierCatalog;
7
use OriPhpstan\Nette\Forms\Catalog\Stub\FormReplicatorCatalog;
8
use OriPhpstan\Nette\Forms\Catalog\Stub\FormRuleTypeCatalog;
9
use OriPhpstan\Nette\Forms\Catalog\Stub\FormValueTypeCatalog;
10
use OriPhpstan\Nette\Latte\Version\ShapeFamily;
11
use OriPhpstan\Nette\Support\BootstrapFilesLoader;
12
use OriPhpstan\Nette\Support\ProjectInstalledVersions;
13
use PHPStan\Reflection\ReflectionProvider;
14
use function array_keys;
15
use function in_array;
16
use function is_array;
17
use function is_file;
18
use function is_readable;
19
use function sprintf;
20
use function strtolower;
21
use function version_compare;
22

23
/**
24
 * @phpstan-import-type NetteConfig from Configuration
25
 */
26
final class ConfigurationGuard
27
{
28

29
        private const BUILT_IN_CATALOGS = [
30
                FormValueTypeCatalog::class,
31
                FormModifierCatalog::class,
32
                FormRuleTypeCatalog::class,
33
                FormReplicatorCatalog::class,
34
        ];
35

36
        private const FORMULAS = [
37
                'vendor-two-candidate',
38
                'vendor-layout-walk',
39
                'samedir-single',
40
                'dirname-lcfirst',
41
                'dirname-templates-lcfirst',
42
                'dirname-templates-lcfirst-fallback',
43
                'dirname-property-lcfirst',
44
        ];
45

46
        private const FORMULA_OPTIONS = ['formula', 'sharedFallback', 'nameProperty'];
47

48
        private const REQUIRED_FORMULA_OPTIONS = [
49
                'dirname-templates-lcfirst-fallback' => 'sharedFallback',
50
                'dirname-property-lcfirst' => 'nameProperty',
51
        ];
52

53
        /** @var NetteConfig */
54
        private array $config;
55

56
        /** @var list<string> */
57
        private array $fileExtensions;
58

59
        private ReflectionProvider $reflectionProvider;
60

61
        private ProjectInstalledVersions $installedVersions;
62

63
        private ?BootstrapFilesLoader $bootstrapFilesLoader;
64

65
        private bool $validated = false;
66

67
        /**
68
         * @param list<string> $fileExtensions
69
         */
70
        public function __construct(
71
                Configuration $configuration,
72
                array $fileExtensions,
73
                ReflectionProvider $reflectionProvider,
74
                ProjectInstalledVersions $installedVersions,
75
                ?BootstrapFilesLoader $bootstrapFilesLoader = null
76
        )
77
        {
78
                $this->bootstrapFilesLoader = $bootstrapFilesLoader;
488 ✔
79
                $this->config = $configuration->toArray();
488 ✔
80
                $this->fileExtensions = $fileExtensions;
488 ✔
81
                $this->reflectionProvider = $reflectionProvider;
488 ✔
82
                $this->installedVersions = $installedVersions;
488 ✔
83
        }
84

85
        public function validate(): void
86
        {
87
                if ($this->validated) {
516 ✔
88
                        return;
174 ✔
89
                }
90

91
                $latte = $this->config['latte'];
378 ✔
92
                if ($latte['enabled'] && !in_array('latte', $this->fileExtensions, true)) {
378 ✔
93
                        throw new InvalidConfiguration('orisai.nette.latte.enabled requires "latte" in fileExtensions.');
×
94
                }
95

96
                if ($latte['narrowing']['enabled'] && !$latte['enabled']) {
378 ✔
97
                        throw new InvalidConfiguration('orisai.nette.latte.narrowing.enabled requires orisai.nette.latte.enabled.');
×
98
                }
99

100
                if ($latte['enabled']) {
378 ✔
101
                        $this->validateLatteVersionPairs();
322 ✔
102
                }
103

104
                $loaders = [
360 ✔
105
                        'dic.containerLoader' => $this->config['dic']['containerLoader'],
360 ✔
106
                        'latte.engineLoader' => $latte['engineLoader'],
360 ✔
107
                        'latte.templateFactoryContainerLoader' => $latte['templateFactoryContainerLoader'],
360 ✔
108
                ];
360 ✔
109
                foreach ($loaders as $key => $path) {
360 ✔
110
                        if ($path !== null && (!is_file($path) || !is_readable($path))) {
360 ✔
111
                                throw new InvalidConfiguration(sprintf('orisai.nette.%s "%s" is not a readable file.', $key, $path));
×
112
                        }
113
                }
114

115
                // The coordinator validates before PHPStan runs %bootstrapFiles%, and a catalog class may be
116
                // loadable only through an autoloader they register.
117
                if ($this->config['forms']['catalogs'] !== [] && $this->bootstrapFilesLoader !== null) {
360 ✔
118
                        $this->bootstrapFilesLoader->load();
×
119
                }
120

121
                foreach ($this->config['forms']['catalogs'] as $catalog) {
360 ✔
122
                        if (!$this->reflectionProvider->hasClass($catalog)) {
×
123
                                throw new InvalidConfiguration(
×
124
                                        sprintf('orisai.nette.forms.catalogs: class "%s" does not exist.', $catalog),
×
125
                                );
×
126
                        }
127
                }
128

129
                $this->validateCatalogMethods();
360 ✔
130

131
                foreach ($latte['discovery']['formulas'] as $class => $formula) {
360 ✔
132
                        $options = is_array($formula) ? $formula : ['formula' => $formula];
×
133
                        $name = $options['formula'] ?? null;
×
134
                        if (!in_array($name, self::FORMULAS, true)) {
×
135
                                throw new InvalidConfiguration(sprintf(
×
136
                                        'orisai.nette.latte.discovery.formulas: unknown formula "%s" for %s.',
×
137
                                        (string) $name,
×
138
                                        $class,
×
139
                                ));
×
140
                        }
141

142
                        foreach (array_keys($options) as $option) {
×
143
                                if (!in_array($option, self::FORMULA_OPTIONS, true)) {
×
144
                                        throw new InvalidConfiguration(sprintf(
×
145
                                                'orisai.nette.latte.discovery.formulas: unknown option "%s" for %s.',
×
146
                                                $option,
×
147
                                                $class,
×
148
                                        ));
×
149
                                }
150
                        }
151

152
                        $required = self::REQUIRED_FORMULA_OPTIONS[$name] ?? null;
×
153
                        if ($required !== null && !isset($options[$required])) {
×
154
                                throw new InvalidConfiguration(sprintf(
×
155
                                        'orisai.nette.latte.discovery.formulas: formula "%s" for %s requires option "%s".',
×
156
                                        $name,
×
157
                                        $class,
×
158
                                        $required,
×
159
                                ));
×
160
                        }
161
                }
162

163
                $this->validated = true;
360 ✔
164
        }
165

166
        // UIExtension exists from nette/application 3.1.6 and FormsExtension from nette/forms 3.1.7;
167
        // application 3.2.0-3.2.6 and forms 3.2.0-3.2.6 declare a conflict with Latte 3.1.
168
        private function validateLatteVersionPairs(): void
169
        {
170
                $latte = $this->installedVersions->getVersion('latte/latte') ?? Engine::VERSION;
322 ✔
171
                if (!ShapeFamily::supports($latte)) {
322 ✔
172
                        throw new InvalidConfiguration(sprintf(
4 ✔
173
                                'orisai.nette.latte.enabled requires a supported latte/latte version (2.11, 3.0 or 3.1); installed %s.',
4 ✔
174
                                $this->prettyVersion('latte/latte') ?? $latte,
4 ✔
175
                        ));
4 ✔
176
                }
177

178
                if (self::isBelow($latte, '3.0.0')) {
318 ✔
179
                        return;
2 ✔
180
                }
181

182
                $forms = $this->installedVersions->getVersion('nette/forms');
316 ✔
183
                $application = $this->installedVersions->getVersion('nette/application');
316 ✔
184

185
                if ($forms !== null && self::isBelow($forms, '3.1.7')) {
316 ✔
186
                        throw new InvalidConfiguration(
4 ✔
187
                                sprintf(
4 ✔
188
                                        'Latte 3 requires nette/forms >= 3.1.7 (FormsExtension); installed %s.',
4 ✔
189
                                        $this->prettyVersion('nette/forms') ?? $forms,
4 ✔
190
                                ),
4 ✔
191
                        );
4 ✔
192
                }
193

194
                if ($application !== null && self::isBelow($application, '3.1.6')) {
312 ✔
195
                        throw new InvalidConfiguration(
2 ✔
196
                                sprintf(
2 ✔
197
                                        'Latte 3 requires nette/application >= 3.1.6 (UIExtension); installed %s.',
2 ✔
198
                                        $this->prettyVersion('nette/application') ?? $application,
2 ✔
199
                                ),
2 ✔
200
                        );
2 ✔
201
                }
202

203
                if (
204
                        !self::isBelow($latte, '3.1.0')
310 ✔
205
                        && (
206
                                ($forms !== null && self::isBelow($forms, '3.2.7'))
310 ✔
207
                                || ($application !== null && self::isBelow($application, '3.2.7'))
310 ✔
208
                        )
209
                ) {
210
                        throw new InvalidConfiguration(sprintf(
8 ✔
211
                                'Latte 3.1 requires nette/forms >= 3.2.7 and nette/application >= 3.2.7; installed %s/%s.',
8 ✔
212
                                $this->prettyVersion('nette/forms') ?? $forms ?? 'none',
8 ✔
213
                                $this->prettyVersion('nette/application') ?? $application ?? 'none',
8 ✔
214
                        ));
8 ✔
215
                }
216
        }
217

218
        private function prettyVersion(string $package): ?string
219
        {
220
                return $this->installedVersions->getPrettyVersion($package);
18 ✔
221
        }
222

223
        private static function isBelow(string $version, string $minimum): bool
224
        {
225
                return version_compare($version, $minimum) < 0;
318 ✔
226
        }
227

228
        private function validateCatalogMethods(): void
229
        {
230
                if ($this->config['forms']['catalogs'] === []) {
360 ✔
231
                        return;
360 ✔
232
                }
233

234
                // Inherited methods count too, so a user catalog extending a built-in one is rejected as a duplicate.
235
                $declaredBy = [];
×
236
                foreach ([...self::BUILT_IN_CATALOGS, ...$this->config['forms']['catalogs']] as $catalogName) {
×
237
                        if (!$this->reflectionProvider->hasClass($catalogName)) {
×
238
                                continue;
×
239
                        }
240

241
                        $catalog = $this->reflectionProvider->getClass($catalogName);
×
242
                        foreach ($catalog->getNativeReflection()->getMethods() as $method) {
×
243
                                $name = strtolower($method->getName());
×
244
                                if (isset($declaredBy[$name]) && $declaredBy[$name] !== $catalog->getName()) {
×
245
                                        throw new InvalidConfiguration(sprintf(
×
246
                                                'orisai.nette.forms.catalogs: method "%s" is declared by both %s and %s.',
×
247
                                                $method->getName(),
×
248
                                                $declaredBy[$name],
×
249
                                                $catalog->getName(),
×
250
                                        ));
×
251
                                }
252

253
                                $declaredBy[$name] = $catalog->getName();
×
254
                        }
255
                }
256
        }
257

258
        public function isFormsEnabled(): bool
259
        {
260
                return $this->config['forms']['enabled'];
144 ✔
261
        }
262

263
        public function isComponentEnabled(): bool
264
        {
265
                return $this->config['component']['enabled'];
×
266
        }
267

268
        public function isLatteEnabled(): bool
269
        {
270
                return $this->config['latte']['enabled'];
301 ✔
271
        }
272

273
        public function isLatteNarrowingEnabled(): bool
274
        {
275
                return $this->isLatteEnabled() && $this->config['latte']['narrowing']['enabled'];
70 ✔
276
        }
277

278
        public function isLatteDiscoveryEnabled(): bool
279
        {
280
                return $this->isLatteEnabled() && $this->config['latte']['discovery']['enabled'];
188 ✔
281
        }
282

283
        public function hasTemplateFactoryContainerLoader(): bool
284
        {
285
                return $this->config['latte']['templateFactoryContainerLoader'] !== null;
42 ✔
286
        }
287

288
        public function isDicEnabled(): bool
289
        {
290
                return $this->config['dic']['containerLoader'] !== null;
14 ✔
291
        }
292

293
        public function isBridgeEnabled(): bool
294
        {
295
                return $this->isFormsEnabled() && $this->isLatteEnabled();
127 ✔
296
        }
297

298
}
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