Repository: symfony/symfony
File: src/Symfony/Component/JsonStreamer/StreamerDumper.php (PHP)

Code before:
                $path,
                function (ConfigCacheInterface $cache) use ($generateContent, $type) {
                    $resourceClasses = array_keys($this->getResourceClassNames($type));
                    $cache->write(
                        $generateContent(),
                        array_map(fn (string $c) => new ReflectionClassResource(new \ReflectionClass($c)), $resourceClasses),
                    );
                },
            );

            return;
        }

        $this->fs ??= new Filesystem();

        if (!$this->fs->exists($this->cacheDir)) {
            $this->fs->mkdir($this->cacheDir);
        }

        if (!$this->fs->exists($path)) {
            $this->fs->dumpFile($path, $generateContent());
        }
    }

    /**
     * Retrieves resource class names (as keys) required for caching based on the provided type.
     *
     * @param array<class-string, true> $classNames
     * @param array<string, mixed>      $context
     *
     * @return array<class-string, true>
     */
    private function getResourceClassNames(Type $type, array $classNames = [], array $context = []): array
    {
        $context['original_type'] ??= $type;

        foreach ($type->traverse() as $t) {
            if ($t instanceof ObjectType) {
                if (isset($classNames[$t->getClassName()])) {
                    continue;
                }

                $classNames[$t->getClassName()] = true;

                foreach ($this->propertyMetadataLoader->load($t->getClassName(), [], $context) as $property) {
                    $classNames += $this->getResourceClassNames($property->getType(), $classNames, $context);
                }
            }

            if ($t instanceof GenericType) {
                foreach ($t->getVariableTypes() as $variableType) {

Review comment (suggestion):
```suggestion
     * Retrieves resource class names required for caching based on the provided type.
```

Code after:
                $path,
                function (ConfigCacheInterface $cache) use ($generateContent, $type) {
                    $resourceClasses = $this->getResourceClassNames($type);
                    $cache->write(
                        $generateContent(),
                        array_map(fn (string $c) => new ReflectionClassResource(new \ReflectionClass($c)), $resourceClasses),
                    );
                },
            );

            return;
        }

        $this->fs ??= new Filesystem();

        if (!$this->fs->exists($this->cacheDir)) {
            $this->fs->mkdir($this->cacheDir);
        }

        if (!$this->fs->exists($path)) {
            $this->fs->dumpFile($path, $generateContent());
        }
    }

    /**
     * Retrieves resource class names required for caching based on the provided type.
     *
     * @param array<class-string, class-string> $classNames
     * @param array<string, mixed>              $context
     *
     * @return array<class-string, class-string>
     */
    private function getResourceClassNames(Type $type, array $classNames = [], array $context = []): array
    {
        $context['original_type'] ??= $type;

        foreach ($type->traverse() as $t) {
            if ($t instanceof ObjectType) {
                if (isset($classNames[$className = $t->getClassName()])) {
                    continue;
                }

                $classNames[$className] = $className;

                foreach ($this->propertyMetadataLoader->load($className, [], $context) as $property) {
                    $classNames += $this->getResourceClassNames($property->getType(), $classNames, $context);
                }
            }

            if ($t instanceof GenericType) {
                foreach ($t->getVariableTypes() as $variableType) {