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

Code before:
    {
        return array_values($this->resources);
    }

    /**
     * @return $this
     */
    public function addResource(ResourceInterface $resource): static
    {
        if (!$this->trackResources) {
            return $this;
        }

        if ($resource instanceof GlobResource && $this->inVendors($resource->getPrefix())) {
            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;
                        }

Review comment (performance):
should we extract the check of ClassExistenceResource being in vendors into a private method instead of this early check of any other type ? It might reduce the risk of mistakes when wanting to optimize other Resource implementations in the future.

Code after:
    {
        return array_values($this->resources);
    }

    /**
     * @return $this
     */
    public function addResource(ResourceInterface $resource): static
    {
        if (!$this->trackResources) {
            return $this;
        }

        if ($resource instanceof GlobResource && $this->inVendors($resource->getPrefix())) {
            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) {
            $this->resources[(string) $resource] = $resource;

            return $this;
        }

        $class = $resource->getResource();

        if (!(new ClassExistenceResource($class, false))->isFresh(1)) {
            if (!$this->inVendors((new \ReflectionClass($class))->getFileName())) {
                $this->resources[$class] = $resource;
            }

            return $this;
        }

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

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