1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\DependencyInjection;
4:
5: use Nette\Bootstrap\Extensions\PhpExtension;
6: use Nette\DI\Config\Adapters\PhpAdapter;
7: use Nette\DI\Definitions\Statement;
8: use Nette\DI\Extensions\ExtensionsExtension;
9: use Nette\DI\Helpers;
10: use Nette\Schema\Context as SchemaContext;
11: use Nette\Schema\Elements\AnyOf;
12: use Nette\Schema\Elements\Structure;
13: use Nette\Schema\Elements\Type;
14: use Nette\Schema\Expect;
15: use Nette\Schema\Processor;
16: use Nette\Schema\Schema;
17: use Nette\Utils\Strings;
18: use Nette\Utils\Validators;
19: use Phar;
20: use PhpParser\Parser;
21: use PHPStan\BetterReflection\BetterReflection;
22: use PHPStan\BetterReflection\Reflector\Reflector;
23: use PHPStan\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber;
24: use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
25: use PHPStan\Command\CommandHelper;
26: use PHPStan\File\FileHelper;
27: use PHPStan\Node\Printer\Printer;
28: use PHPStan\Php\PhpVersion;
29: use PHPStan\Reflection\PhpVersionStaticAccessor;
30: use PHPStan\Reflection\ReflectionProvider;
31: use PHPStan\Reflection\ReflectionProviderStaticAccessor;
32: use PHPStan\ShouldNotHappenException;
33: use PHPStan\Type\ObjectType;
34: use function array_diff_key;
35: use function array_intersect;
36: use function array_key_exists;
37: use function array_keys;
38: use function array_map;
39: use function array_merge;
40: use function array_slice;
41: use function array_unique;
42: use function count;
43: use function dirname;
44: use function extension_loaded;
45: use function getenv;
46: use function implode;
47: use function ini_get;
48: use function is_array;
49: use function is_file;
50: use function is_readable;
51: use function is_string;
52: use function spl_object_id;
53: use function sprintf;
54: use function str_ends_with;
55: use function substr;
56:
57: /**
58: * @api
59: */
60: final class ContainerFactory
61: {
62:
63: private FileHelper $fileHelper;
64:
65: private string $rootDirectory;
66:
67: private string $configDirectory;
68:
69: private static ?int $lastInitializedContainerId = null;
70:
71: private bool $journalContainer = false;
72:
73: /** @api */
74: public function __construct(private string $currentWorkingDirectory)
75: {
76: $this->fileHelper = new FileHelper($currentWorkingDirectory);
77:
78: $rootDir = __DIR__ . '/../..';
79: $originalRootDir = $this->fileHelper->normalizePath($rootDir);
80: if (extension_loaded('phar')) {
81: $pharPath = Phar::running(false);
82: if ($pharPath !== '') {
83: $rootDir = dirname($pharPath);
84: }
85: }
86: $this->rootDirectory = $this->fileHelper->normalizePath($rootDir);
87: $this->configDirectory = $originalRootDir . '/conf';
88: }
89:
90: public function setJournalContainer(): void
91: {
92: $this->journalContainer = true;
93: }
94:
95: /**
96: * @param string[] $additionalConfigFiles
97: * @param string[] $analysedPaths
98: * @param string[] $composerAutoloaderProjectPaths
99: * @param string[] $analysedPathsFromConfig
100: * @param array<mixed> $additionalParameters
101: */
102: public function create(
103: string $tempDirectory,
104: array $additionalConfigFiles,
105: array $analysedPaths,
106: array $composerAutoloaderProjectPaths = [],
107: array $analysedPathsFromConfig = [],
108: string $usedLevel = CommandHelper::DEFAULT_LEVEL,
109: ?string $generateBaselineFile = null,
110: ?string $cliAutoloadFile = null,
111: ?string $singleReflectionFile = null,
112: ?string $singleReflectionInsteadOfFile = null,
113: array $additionalParameters = [],
114: ): Container
115: {
116: [$allConfigFiles, $projectConfig] = $this->detectDuplicateIncludedFiles(
117: array_merge([__DIR__ . '/../../conf/parametersSchema.neon'], $additionalConfigFiles),
118: [
119: 'rootDir' => $this->rootDirectory,
120: 'currentWorkingDirectory' => $this->currentWorkingDirectory,
121: 'env' => getenv(),
122: ],
123: );
124:
125: $configurator = new Configurator(new LoaderFactory(
126: $this->fileHelper,
127: $this->rootDirectory,
128: $this->currentWorkingDirectory,
129: $generateBaselineFile,
130: $projectConfig['expandRelativePaths'],
131: ), $this->journalContainer);
132: $configurator->defaultExtensions = [
133: 'php' => PhpExtension::class,
134: 'extensions' => ExtensionsExtension::class,
135: ];
136: $configurator->setDebugMode(true);
137: $configurator->setTempDirectory($tempDirectory);
138: $configurator->addParameters(array_merge([
139: 'rootDir' => $this->rootDirectory,
140: 'currentWorkingDirectory' => $this->currentWorkingDirectory,
141: 'cliArgumentsVariablesRegistered' => ini_get('register_argc_argv') === '1',
142: 'tmpDir' => $tempDirectory,
143: 'additionalConfigFiles' => $additionalConfigFiles,
144: 'allConfigFiles' => $allConfigFiles,
145: 'composerAutoloaderProjectPaths' => $composerAutoloaderProjectPaths,
146: 'generateBaselineFile' => $generateBaselineFile,
147: 'usedLevel' => $usedLevel,
148: 'cliAutoloadFile' => $cliAutoloadFile,
149: 'env' => getenv(),
150: ], $additionalParameters));
151: $configurator->addDynamicParameters([
152: 'singleReflectionFile' => $singleReflectionFile,
153: 'singleReflectionInsteadOfFile' => $singleReflectionInsteadOfFile,
154: 'analysedPaths' => $analysedPaths,
155: 'analysedPathsFromConfig' => $analysedPathsFromConfig,
156: ]);
157: $configurator->addConfig($this->configDirectory . '/config.neon');
158: foreach ($additionalConfigFiles as $additionalConfigFile) {
159: $configurator->addConfig($additionalConfigFile);
160: }
161:
162: $configurator->setAllConfigFiles($allConfigFiles);
163:
164: $container = $configurator->createContainer()->getByType(Container::class);
165: $this->validateParameters($container->getParameters(), $projectConfig['parametersSchema']);
166: self::postInitializeContainer($container);
167:
168: return $container;
169: }
170:
171: /** @internal */
172: public static function postInitializeContainer(Container $container): void
173: {
174: $containerId = spl_object_id($container);
175: if ($containerId === self::$lastInitializedContainerId) {
176: return;
177: }
178:
179: self::$lastInitializedContainerId = $containerId;
180:
181: /** @var SourceLocator $sourceLocator */
182: $sourceLocator = $container->getService('betterReflectionSourceLocator');
183:
184: /** @var Reflector $reflector */
185: $reflector = $container->getService('betterReflectionReflector');
186:
187: /** @var Parser $phpParser */
188: $phpParser = $container->getService('phpParserDecorator');
189:
190: BetterReflection::populate(
191: $container->getByType(PhpVersion::class)->getVersionId(),
192: $sourceLocator,
193: $reflector,
194: $phpParser,
195: $container->getByType(PhpStormStubsSourceStubber::class),
196: $container->getByType(Printer::class),
197: );
198:
199: ReflectionProviderStaticAccessor::registerInstance($container->getByType(ReflectionProvider::class));
200: PhpVersionStaticAccessor::registerInstance($container->getByType(PhpVersion::class));
201: ObjectType::resetCaches();
202: $container->getService('typeSpecifier');
203:
204: BleedingEdgeToggle::setBleedingEdge($container->getParameter('featureToggles')['bleedingEdge']);
205: ReportUnsafeArrayStringKeyCastingToggle::setLevel($container->getParameter('reportUnsafeArrayStringKeyCasting'));
206: }
207:
208: public function getCurrentWorkingDirectory(): string
209: {
210: return $this->currentWorkingDirectory;
211: }
212:
213: public function getRootDirectory(): string
214: {
215: return $this->rootDirectory;
216: }
217:
218: public function getConfigDirectory(): string
219: {
220: return $this->configDirectory;
221: }
222:
223: /**
224: * @param string[] $configFiles
225: * @param array<string, mixed> $loaderParameters
226: * @return array{list<string>, array<mixed>}
227: * @throws DuplicateIncludedFilesException
228: */
229: private function detectDuplicateIncludedFiles(
230: array $configFiles,
231: array $loaderParameters,
232: ): array
233: {
234: $neonAdapter = new NeonCachedFileReader([]);
235: $phpAdapter = new PhpAdapter();
236: $allConfigFiles = [];
237: $configArray = [];
238: foreach ($configFiles as $configFile) {
239: [$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($this->fileHelper, $neonAdapter, $phpAdapter, $configFile, $loaderParameters, null);
240: $allConfigFiles = array_merge($allConfigFiles, $tmpConfigFiles);
241:
242: /** @var array<mixed> $configArray */
243: $configArray = \Nette\Schema\Helpers::merge($tmpConfigArray, $configArray);
244: }
245:
246: $normalized = array_map(fn (string $file): string => $this->fileHelper->normalizePath($file), $allConfigFiles);
247:
248: $deduplicated = array_unique($normalized);
249: if (count($normalized) <= count($deduplicated)) {
250: return [$normalized, $configArray];
251: }
252:
253: $duplicateFiles = array_unique(array_diff_key($normalized, $deduplicated));
254:
255: throw new DuplicateIncludedFilesException($duplicateFiles);
256: }
257:
258: /**
259: * @param array<string, string> $loaderParameters
260: * @return array{list<string>, array<mixed>}
261: */
262: private static function getConfigFiles(
263: FileHelper $fileHelper,
264: NeonCachedFileReader $neonAdapter,
265: PhpAdapter $phpAdapter,
266: string $configFile,
267: array $loaderParameters,
268: ?string $generateBaselineFile,
269: ): array
270: {
271: if ($generateBaselineFile === $fileHelper->normalizePath($configFile)) {
272: return [[], []];
273: }
274: if (!is_file($configFile) || !is_readable($configFile)) {
275: return [[], []];
276: }
277:
278: if (str_ends_with($configFile, '.php')) {
279: $data = $phpAdapter->load($configFile);
280: } else {
281: $data = $neonAdapter->load($configFile);
282: }
283: $allConfigFiles = [$configFile];
284: if (isset($data['includes'])) {
285: Validators::assert($data['includes'], 'list', sprintf("section 'includes' in file '%s'", $configFile));
286: $includes = Helpers::expand($data['includes'], $loaderParameters);
287: foreach ($includes as $include) {
288: $include = self::expandIncludedFile($include, $configFile);
289: [$tmpConfigFiles, $tmpConfigArray] = self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $include, $loaderParameters, $generateBaselineFile);
290: $allConfigFiles = array_merge($allConfigFiles, $tmpConfigFiles);
291:
292: /** @var array<mixed> $data */
293: $data = \Nette\Schema\Helpers::merge($tmpConfigArray, $data);
294: }
295: }
296:
297: return [$allConfigFiles, $data];
298: }
299:
300: private static function expandIncludedFile(string $includedFile, string $mainFile): string
301: {
302: return Strings::match($includedFile, '#([a-z]+:)?[/\\\\]#Ai') !== null // is absolute
303: ? $includedFile
304: : dirname($mainFile) . '/' . $includedFile;
305: }
306:
307: /**
308: * @param array<mixed> $parameters
309: * @param array<mixed> $parametersSchema
310: */
311: private function validateParameters(array $parameters, array $parametersSchema): void
312: {
313: if (!(bool) $parameters['__validate']) {
314: return;
315: }
316:
317: $schema = $this->processArgument(
318: new Statement('schema', [
319: new Statement('structure', [$parametersSchema]),
320: ]),
321: );
322: $processor = new Processor();
323: $processor->onNewContext[] = static function (SchemaContext $context): void {
324: $context->path = ['parameters'];
325: };
326: $processor->process($schema, $parameters);
327:
328: if (
329: array_key_exists('phpVersion', $parameters)
330: && is_array($parameters['phpVersion'])
331: ) {
332: $phpVersion = $parameters['phpVersion'];
333:
334: if ($phpVersion['max'] < $phpVersion['min']) {
335: throw new InvalidPhpVersionException('Invalid PHP version range: phpVersion.max should be greater or equal to phpVersion.min.');
336: }
337: }
338:
339: foreach ($parameters['ignoreErrors'] ?? [] as $ignoreError) {
340: if (is_string($ignoreError)) {
341: continue;
342: }
343:
344: $atLeastOneOf = ['message', 'messages', 'rawMessage', 'rawMessages', 'identifier', 'identifiers', 'path', 'paths'];
345: if (array_intersect($atLeastOneOf, array_keys($ignoreError)) === []) {
346: throw new InvalidIgnoredErrorException('An ignoreErrors entry must contain at least one of the following fields: ' . implode(', ', $atLeastOneOf) . '.');
347: }
348:
349: foreach ([
350: ['rawMessage', 'rawMessages', 'message', 'messages'],
351: ['identifier', 'identifiers'],
352: ['path', 'paths'],
353: ] as $incompatibleFields) {
354: foreach ($incompatibleFields as $index => $field1) {
355: $fieldsToCheck = array_slice($incompatibleFields, $index + 1);
356: foreach ($fieldsToCheck as $field2) {
357: if (array_key_exists($field1, $ignoreError) && array_key_exists($field2, $ignoreError)) {
358: throw new InvalidIgnoredErrorException(sprintf('An ignoreErrors entry cannot contain both %s and %s fields.', $field1, $field2));
359: }
360: }
361: }
362: }
363:
364: if (array_key_exists('count', $ignoreError) && !array_key_exists('path', $ignoreError)) {
365: throw new InvalidIgnoredErrorException('An ignoreErrors entry with count field must also contain path field.');
366: }
367: }
368: }
369:
370: /**
371: * @param Statement[] $statements
372: */
373: private function processSchema(array $statements, bool $required = true): Schema
374: {
375: if (count($statements) === 0) {
376: throw new ShouldNotHappenException();
377: }
378:
379: $parameterSchema = null;
380: foreach ($statements as $statement) {
381: $processedArguments = array_map(fn ($argument) => $this->processArgument($argument), $statement->arguments);
382: if ($parameterSchema === null) {
383: /** @var Type|AnyOf|Structure $parameterSchema */
384: $parameterSchema = Expect::{$statement->getEntity()}(...$processedArguments);
385: } else {
386: $parameterSchema->{$statement->getEntity()}(...$processedArguments);
387: }
388: }
389:
390: if ($required) {
391: $parameterSchema->required();
392: }
393:
394: return $parameterSchema;
395: }
396:
397: /**
398: * @param mixed $argument
399: * @return mixed
400: */
401: private function processArgument($argument, bool $required = true)
402: {
403: if ($argument instanceof Statement) {
404: if ($argument->entity === 'schema') {
405: $arguments = [];
406: foreach ($argument->arguments as $schemaArgument) {
407: if (!$schemaArgument instanceof Statement) {
408: throw new ShouldNotHappenException('schema() should contain another statement().');
409: }
410:
411: $arguments[] = $schemaArgument;
412: }
413:
414: if (count($arguments) === 0) {
415: throw new ShouldNotHappenException('schema() should have at least one argument.');
416: }
417:
418: return $this->processSchema($arguments, $required);
419: }
420:
421: return $this->processSchema([$argument], $required);
422: } elseif (is_array($argument)) {
423: $processedArray = [];
424: foreach ($argument as $key => $val) {
425: $required = $key[0] !== '?';
426: $key = $required ? $key : substr($key, 1);
427: $processedArray[$key] = $this->processArgument($val, $required);
428: }
429:
430: return $processedArray;
431: }
432:
433: return $argument;
434: }
435:
436: }
437: