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

Code before:

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

        return $this;
    }

    /**
     * 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);
                }
            }

Review comment (performance):
Those `continue` should be `break` instead.

If we have other autoloaders than Composer ones, they might define classes in other ways, and so we cannot optimize the ClassExistenceResource away.

Code after:
            return $this;
        }
        if ($resource instanceof FileExistenceResource && $this->inVendors($resource->getResource())) {
            return $this;
        }
        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;