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

Code before:
 *
 * @internal
 */
final class StreamerDumper
{
    private ?Filesystem $fs = null;

    public function __construct(
        private PropertyMetadataLoaderInterface $propertyMetadataLoader,
        private string $cacheDir,
        private ?ConfigCacheFactoryInterface $cacheFactory = null,
    ) {
    }

    /**
     * Dumps the generated content to the given path, optionally using config cache.
     *
     * @param callable(): string $generateContent
     */
    public function dump(Type $type, string $path, callable $generateContent): void
    {
        if ($this->cacheFactory) {
            $this->cacheFactory->cache(
                $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

Review comment (suggestion):
```suggestion
                    $resourceClasses = $this->getResourceClassNames($type);
```

Code after:
 *
 * @internal
 */
final class StreamerDumper
{
    private ?Filesystem $fs = null;

    public function __construct(
        private PropertyMetadataLoaderInterface $propertyMetadataLoader,
        private string $cacheDir,
        private ?ConfigCacheFactoryInterface $cacheFactory = null,
    ) {
    }

    /**
     * Dumps the generated content to the given path, optionally using config cache.
     *
     * @param callable(): string $generateContent
     */
    public function dump(Type $type, string $path, callable $generateContent): void
    {
        if ($this->cacheFactory) {
            $this->cacheFactory->cache(
                $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