Skip to content

Wrong Manager returned for similar Namespaces #404

Description

@friedrichroell

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions