10000 bug #18799 Use levenshtein level for better Bundle matching (j0k3r) · symfony/symfony@2731787 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2731787

Browse files
committed
bug #18799 Use levenshtein level for better Bundle matching (j0k3r)
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
2 parents 20d694e + ac7f74e commit 2731787

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/ControllerNameParser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ private function findAlternative($nonExistentBundleName)
142142
$lev = levenshtein($nonExistentBundleName, $bundleName);
143143
if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) {
144144
$alternative = $bundleName;
145+
$shortest = $lev;
145146
}
146147
}
147148

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function testBuild()
5959
{
6060
$parser = $this->createParser();
6161

62-
$this->assertEquals('FooBundle:Default:index', $parser->build('TestBundle\FooBundle\Controller\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
63-
$this->assertEquals('FooBundle:Sub\Default:index', $parser->build('TestBundle\FooBundle\Controller\Sub\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
62+
$this->assertEquals('FoooooBundle:Default:index', $parser->build('TestBundle\FooBundle\Controller\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
63+
$this->assertEquals('FoooooBundle:Sub\Defau E1A7 lt:index', $parser->build('TestBundle\FooBundle\Controller\Sub\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string');
6464

6565
try {
6666
$parser->build('TestBundle\FooBundle\Controller\DefaultController::index');
@@ -132,8 +132,9 @@ public function testInvalidBundleName($bundleName, $suggestedBundleName)
132132
public function getInvalidBundleNameTests()
133133
{
134134
return array(
135-
array('FoodBundle:Default:index', 'FooBundle:Default:index'),
136-
array('CrazyBundle:Default:index', false),
135+
'Alternative will be found using levenshtein' => array('FoodBundle:Default:index', 'FooBundle:Default:index'),
136+
'Alternative will be found using partial match' => array('FabpotFooBund:Default:index', 'FabpotFooBundle:Default:index'),
137+
'Bundle does not exist at all' => array('CrazyBundle:Default:index', false),
137138
);
138139
}
139140

@@ -162,6 +163,7 @@ private function createParser()
162163
$bundles = array(
163164
'SensioFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'),
164165
'SensioCmsFooBundle' => $this->getBundle('TestBundle\Sensio\Cms\FooBundle', 'SensioCmsFooBundle'),
166+
'FoooooBundle' => $this->getBundle('TestBundle\FooBundle', 'FoooooBundle'),
165167
'FooBundle' => $this->getBundle('TestBundle\FooBundle', 'FooBundle'),
166168
'FabpotFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'),
167169
);

0 commit comments

Comments
 (0)
0