Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use levenshtein level for better Bundle matching #18799

Merged
merged 1 commit into from May 17, 2016
Merged

Use levenshtein level for better Bundle matching #18799

merged 1 commit into from May 17, 2016

Conversation

j0k3r
Copy link
Contributor

@j0k3r j0k3r commented May 17, 2016

Q A
Branch? 2.7
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

I've targetted 2.7 branch since this was introduced in the 2.6 version but the 2.6 isn't maintain anymore.

TL;DR: I found unused code in bad bundle exception when Symfony try to find the best bundle to fix the typo. Should we remove that code (and got a potential lower matching bundle name) or keep it and make it work?


I've noticed that a part of the code wasn't used when determining which bundle typo was written on bad bundle exception, from #11210.

$alternative = null;
$shortest = null;
foreach ($bundleNames as $bundleName) {
    // if there's a partial match, return it immediately
    if (false !== strpos($bundleName, $nonExistentBundleName)) {
        return $bundleName;
    }

    $lev = levenshtein($nonExistentBundleName, $bundleName);
    if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) {
        $alternative = $bundleName;
    }
}

In this snippet, the $shortest wasn't update in the foreach. Reading the code, I guess it was supposed to add an even better accuracy when multiple bundle matche the typo'd bundle name.

Which mean when an alternative is found, we have to assign the level $lev from that match to $shortest.

if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) {
    $alternative = $bundleName;
    $shortest = $lev;
}

Let say you have these bundles: FoooooBundle and FooBundle and you request the bundle FoodBundle.

  • Without $shortest updated, you'll got a suggestion with FoooooBundle (first matching bundle found)
  • With $shortest upadted, you'll got a suggestion with FooBundle (because it has a better level than FoooooBundle)

This isn't a bug fix since this is only supposed to help developper but not the final user.

Question is: should we keep that level comparison or just remove it?

@javiereguiluz
Copy link
Member

I'd say this is a bug because the variable wasn't updated in the loop. I'm 👍 for the change/fix. Thanks @j0k3r.

@stof
Copy link
Member

stof commented May 17, 2016

👍

@fabpot
Copy link
Member

fabpot commented May 17, 2016

Thank you @j0k3r.

@fabpot fabpot merged commit ac7f74e into symfony:2.7 May 17, 2016
fabpot added a commit that referenced this pull request May 17, 2016
This PR was merged into the 2.7 branch.

Discussion
----------

Use levenshtein level for better Bundle matching

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

_I've targetted 2.7 branch since this was introduced in the 2.6 version but the 2.6 isn't maintain anymore._

**TL;DR:** I found unused code in bad bundle exception when Symfony try to find the best bundle to fix the typo. Should we remove that code (and got a potential lower matching bundle name) or keep it and make it work?

-----

I've noticed that a part of the code wasn't used when determining which bundle typo was written on _bad bundle exception_, from #11210.

```php
$alternative = null;
$shortest = null;
foreach ($bundleNames as $bundleName) {
    // if there's a partial match, return it immediately
    if (false !== strpos($bundleName, $nonExistentBundleName)) {
        return $bundleName;
    }

    $lev = levenshtein($nonExistentBundleName, $bundleName);
    if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) {
        $alternative = $bundleName;
    }
}
```

In this snippet, the `$shortest` wasn't update in the `foreach`. Reading the code, I guess it was supposed to add an even better accuracy when multiple bundle matche the typo'd bundle name.

Which mean when an alternative is found, we have to assign the level `$lev` from that match to `$shortest`.

```php
if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) {
    $alternative = $bundleName;
    $shortest = $lev;
}
```

Let say you have these bundles: `FoooooBundle` and `FooBundle` and you request the bundle `FoodBundle`.

- Without `$shortest` updated, you'll got a suggestion with `FoooooBundle` (first matching bundle found)
- With `$shortest` upadted, you'll got a suggestion with `FooBundle` (because it has a better level than `FoooooBundle`)

This isn't a _bug fix_ since this is only supposed to help developper but not the final user.

**Question is**: should we keep that level comparison or just remove it?

Commits
-------

ac7f74e Use levenshtein level for better Bundle matching
@j0k3r j0k3r deleted the shortest-levenshtein2 branch May 18, 2016 07:47
@fabpot fabpot mentioned this pull request May 26, 2016
This was referenced Jun 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants