Skip to content

Commit cb027f5

Browse files
authored
Merge pull request #516 from flightphp/aliasing-adjustments
fixed alias issue, levenshtein recommendations and coverage-check
2 parents 4b98a61 + b388a26 commit cb027f5

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ composer.lock
66
coverage/
77
.vscode/settings.json
88
*.sublime-workspace
9-
.vscode/
9+
.vscode/
10+
clover.xml

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"ext-pdo_sqlite": "*",
3737
"phpunit/phpunit": "^9.5",
3838
"phpstan/phpstan": "^1.10",
39-
"phpstan/extension-installer": "^1.3"
39+
"phpstan/extension-installer": "^1.3",
40+
"rregeer/phpunit-coverage-check": "^0.3.1"
4041
},
4142
"config": {
4243
"allow-plugins": {
@@ -45,7 +46,7 @@
4546
},
4647
"scripts": {
4748
"test": "phpunit",
48-
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage",
49+
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
4950
"lint": "phpstan --no-progress -cphpstan.neon"
5051
},
5152
"suggest": {

flight/net/Router.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,32 @@ public function route(Request $request)
190190
* @return string
191191
*/
192192
public function getUrlByAlias(string $alias, array $params = []): string {
193-
while ($route = $this->current()) {
194-
if ($route->matchAlias($alias)) {
195-
return $route->hydrateUrl($params);
196-
}
197-
$this->next();
198-
}
193+
$potential_aliases = [];
194+
foreach($this->routes as $route) {
195+
$potential_aliases[] = $route->alias;
196+
if ($route->matchAlias($alias)) {
197+
return $route->hydrateUrl($params);
198+
}
199+
200+
}
201+
202+
// use a levenshtein to find the closest match and make a recommendation
203+
$closest_match = '';
204+
$closest_match_distance = 0;
205+
foreach($potential_aliases as $potential_alias) {
206+
$levenshtein_distance = levenshtein($alias, $potential_alias);
207+
if($levenshtein_distance > $closest_match_distance) {
208+
$closest_match = $potential_alias;
209+
$closest_match_distance = $levenshtein_distance;
210+
}
211+
}
212+
213+
$exception_message = 'No route found with alias: \'' . $alias . '\'.';
214+
if($closest_match !== '') {
215+
$exception_message .= ' Did you mean \'' . $closest_match . '\'?';
216+
}
199217

200-
throw new Exception('No route found with alias: ' . $alias);
218+
throw new Exception($exception_message);
201219
}
202220

203221
/**

tests/RouterTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,13 @@ public function testGroupNestedRoutesWithCustomMethods()
468468
$this->request->method = 'POST';
469469
$this->check('123abc');
470470
}
471+
472+
public function testGetUrlByAliasBadReferenceButCatchRecommendation() {
473+
$this->router->map('/path1', [$this, 'ok'], false, 'path1');
474+
$this->expectException(\Exception::class);
475+
$this->expectExceptionMessage('No route found with alias: \'path2\'. Did you mean \'path1\'?');
476+
$this->router->getUrlByAlias('path2');
477+
}
471478

472479
public function testRewindAndValid() {
473480
$this->router->map('/path1', [$this, 'ok']);
@@ -491,7 +498,7 @@ public function testRewindAndValid() {
491498
public function testGetUrlByAliasNoMatches() {
492499
$this->router->map('/path1', [$this, 'ok'], false, 'path1');
493500
$this->expectException(\Exception::class);
494-
$this->expectExceptionMessage('No route found with alias: path2');
501+
$this->expectExceptionMessage('No route found with alias: \'path2\'');
495502
$this->router->getUrlByAlias('path2');
496503
}
497504

0 commit comments

Comments
 (0)