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

Code before:
    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);
                }
                foreach ($class->getTraitNames() as $name) {
                    $this->addObjectResource($name);
                }
            } while ($class = $class->getParentClass());
        }

        return $this;
    }

Review comment (bug):
This logic looks wrong to me. Configuring a namespace prefixed to match a given directory does not mean that all classes matching that prefix must be defined in that folder.
For instance, all our mailer bridges have a namespace prefix that can also be matched by the prefix of the `symfony/mailer` package. And nothing prevents the project-level code to also define classes with that namespace (best practices is to keep your project-level code in a different namespace than your vendors, but that's not a requirement, and I know some projects do weird things).

For existing classes, we can optimize the class existence resource if the class is defined in the vendor folder (by checking the actual class definition, not folders in which it is _maybe_ defined). For non-existent classes, we should not optimize the resource (or we should optimize it only based on **all** possible autoloading locations)

Code after:

            $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;
    }

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