Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/ContainerBuilder.php (PHP)

Code before:
    }

    /**
     * Sets the resources for this configuration.
     *
     * @param array<string, ResourceInterface> $resources
     *
     * @return $this
     */
    public function setResources(array $resources): static
    {
        if (!$this->trackResources) {
            return $this;
        }

        $this->resources = $resources;

        return $this;
    }

    /**
     * Adds the object class hierarchy as resources.
     *
     * @param object|string $object An object instance or class name
     *
     * @return $this
     */
    public function addObjectResource(object|string $object): static
    {
        if ($this->trackResources) {
            if (\is_object($object)) {
                $object = $object::class;
            }
            if (!isset($this->classReflectors[$object])) {
                $this->classReflectors[$object] = new \ReflectionClass($object);
            }
            $class = $this->classReflectors[$object];

            foreach ($class->getInterfaceNames() as $name) {
                if (null === $interface = &$this->classReflectors[$name]) {
                    $interface = new \ReflectionClass($name);
                }
                $file = $interface->getFileName();
                if (false !== $file && file_exists($file)) {
                    $this->fileExists($file);
                }
            }
            do {
                $file = $class->getFileName();
                if (false !== $file && file_exists($file)) {
                    $this->fileExists($file);

Review comment (suggestion):
An empty prefix defines a path that could define any class. Ignoring it (as it it could define none) breaks the optimization

Code after:
        }
        if ($resource instanceof FileResource && $this->inVendors($resource->getResource())) {
            return $this;
        }
        if ($resource instanceof DirectoryResource && $this->inVendors($resource->getResource())) {
            return $this;
        }
        if ($resource instanceof ClassExistenceResource) {
            $class = $resource->getResource();

            $inVendor = false;
            foreach (spl_autoload_functions() as $autoloader) {
                if (!\is_array($autoloader)) {
                    continue;
                }

                if ($autoloader[0] instanceof DebugClassLoader) {
                    $autoloader = $autoloader[0]->getClassLoader();
                }

                if (!\is_array($autoloader) || !$autoloader[0] instanceof ClassLoader || !$autoloader[0]->findFile(__CLASS__)) {
                    continue;
                }

                foreach ($autoloader[0]->getPrefixesPsr4() as $prefix => $dirs) {
                    if ('' === $prefix || !str_starts_with($class, $prefix)) {
                        continue;
                    }

                    foreach ($dirs as $dir) {
                        if (!$dir = realpath($dir)) {
                            continue;
                        }

                        if (!$inVendor = $this->inVendors($dir)) {
                            break 3;
                        }
                    }
                }
            }

            if ($inVendor) {
                return $this;
            }
        }

        $this->resources[(string) $resource] = $resource;

        return $this;
    }