persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Wrong Manager returned for similar Namespaces

Open friedrichroell opened this issue 1 year ago • 3 comments

Bug Report

Q A
Version 4.0.0

Summary

The wrong Manager can be selected if configured Managers have similar namespaces.

Current behavior

The wrong Manager is returned when two or more Managers with similar namespaces are configured.

Expected behavior

The correct Manager should be selected.

How to reproduce

Configure Manager A with the namespace:

App\Entity

and Manager B with:

App\EntityFoo


MappingDriverChain::isTransient will select A for a B-configured Manager because:

str_starts_with($className, $namespace) returns true for both.

A solution could be to find the best match rather than selecting the first driver that matches the namespace prefix.

Solution proposal

Find the best namespace overlap:

public function isTransient(string $className): bool
{
    $driver = $this->findBestMatchingDriver($className) ?? $this->defaultDriver;

    return $driver ? $driver->isTransient($className) : true;
}

private function findBestMatchingDriver(string $className): ?MappingDriver
{
    $bestDriver = null;
    $maxOverlap = 0;

    foreach ($this->drivers as $namespace => $driver) {
        if ( ! str_starts_with($className, $namespace)) {
            continue;
        }
        
        $currentOverlap = similar_text($className, $namespace);
        
        if ($currentOverlap > $maxOverlap) {
            $maxOverlap = $currentOverlap;
            $bestDriver = $driver;
        }
    }

    return $bestDriver;
}

I could create a pull request with tests if this approach is heading in the right direction. A huge thank you to all contributors!

friedrichroell avatar Nov 04 '24 09:11 friedrichroell

Wouldn't doing something similar as https://github.com/doctrine/persistence/blob/2f078b0568cd58c5f5bf0cdf17bbfe3b3f1d7d64/src/Persistence/Mapping/Driver/SymfonyFileLocator.php#L137 achieve the same result?

Also, does this affect 4.0.0 only, or should it be contributed to 3.x as a bugfix?

greg0ire avatar Nov 04 '24 11:11 greg0ire

What about this case? (although this might be a misconfiguration in the first place)

Class: App\Entity\Foo\Bar

Namespace Driver A App\Entity Namespace Driver B App\Entity\Foo

It will match both even with an appended backslash

--

This should be contributed to 3.x as well

friedrichroell avatar Nov 04 '24 12:11 friedrichroell

This case does not look realistic… I mean who would nest namespaces?

greg0ire avatar Nov 04 '24 21:11 greg0ire