Skip to content

Commit

Permalink
Merge pull request #516 from flightphp/aliasing-adjustments
Browse files Browse the repository at this point in the history
fixed alias issue, levenshtein recommendations and coverage-check
  • Loading branch information
krmu authored Jan 13, 2024
2 parents 4b98a61 + b388a26 commit cb027f5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ composer.lock
coverage/
.vscode/settings.json
*.sublime-workspace
.vscode/
.vscode/
clover.xml
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"ext-pdo_sqlite": "*",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.10",
"phpstan/extension-installer": "^1.3"
"phpstan/extension-installer": "^1.3",
"rregeer/phpunit-coverage-check": "^0.3.1"
},
"config": {
"allow-plugins": {
Expand All @@ -45,7 +46,7 @@
},
"scripts": {
"test": "phpunit",
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage",
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
"lint": "phpstan --no-progress -cphpstan.neon"
},
"suggest": {
Expand Down
32 changes: 25 additions & 7 deletions flight/net/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,32 @@ public function route(Request $request)
* @return string
*/
public function getUrlByAlias(string $alias, array $params = []): string {
while ($route = $this->current()) {
if ($route->matchAlias($alias)) {
return $route->hydrateUrl($params);
}
$this->next();
}
$potential_aliases = [];
foreach($this->routes as $route) {
$potential_aliases[] = $route->alias;
if ($route->matchAlias($alias)) {
return $route->hydrateUrl($params);
}

}

// use a levenshtein to find the closest match and make a recommendation
$closest_match = '';
$closest_match_distance = 0;
foreach($potential_aliases as $potential_alias) {
$levenshtein_distance = levenshtein($alias, $potential_alias);
if($levenshtein_distance > $closest_match_distance) {
$closest_match = $potential_alias;
$closest_match_distance = $levenshtein_distance;
}
}

$exception_message = 'No route found with alias: \'' . $alias . '\'.';
if($closest_match !== '') {
$exception_message .= ' Did you mean \'' . $closest_match . '\'?';
}

throw new Exception('No route found with alias: ' . $alias);
throw new Exception($exception_message);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,13 @@ public function testGroupNestedRoutesWithCustomMethods()
$this->request->method = 'POST';
$this->check('123abc');
}

public function testGetUrlByAliasBadReferenceButCatchRecommendation() {
$this->router->map('/path1', [$this, 'ok'], false, 'path1');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('No route found with alias: \'path2\'. Did you mean \'path1\'?');
$this->router->getUrlByAlias('path2');
}

public function testRewindAndValid() {
$this->router->map('/path1', [$this, 'ok']);
Expand All @@ -491,7 +498,7 @@ public function testRewindAndValid() {
public function testGetUrlByAliasNoMatches() {
$this->router->map('/path1', [$this, 'ok'], false, 'path1');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('No route found with alias: path2');
$this->expectExceptionMessage('No route found with alias: \'path2\'');
$this->router->getUrlByAlias('path2');
}

Expand Down

0 comments on commit cb027f5

Please sign in to comment.