Merge pull request #516 from flightphp/aliasing-adjustments

fixed alias issue, levenshtein recommendations and coverage-check
pull/523/head
Kristaps Muižnieks 1 year ago committed by GitHub
commit cb027f548e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

3
.gitignore vendored

@ -6,4 +6,5 @@ composer.lock
coverage/ coverage/
.vscode/settings.json .vscode/settings.json
*.sublime-workspace *.sublime-workspace
.vscode/ .vscode/
clover.xml

@ -36,7 +36,8 @@
"ext-pdo_sqlite": "*", "ext-pdo_sqlite": "*",
"phpunit/phpunit": "^9.5", "phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.10", "phpstan/phpstan": "^1.10",
"phpstan/extension-installer": "^1.3" "phpstan/extension-installer": "^1.3",
"rregeer/phpunit-coverage-check": "^0.3.1"
}, },
"config": { "config": {
"allow-plugins": { "allow-plugins": {
@ -45,7 +46,7 @@
}, },
"scripts": { "scripts": {
"test": "phpunit", "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" "lint": "phpstan --no-progress -cphpstan.neon"
}, },
"suggest": { "suggest": {

@ -190,14 +190,32 @@ class Router
* @return string * @return string
*/ */
public function getUrlByAlias(string $alias, array $params = []): string { public function getUrlByAlias(string $alias, array $params = []): string {
while ($route = $this->current()) { $potential_aliases = [];
if ($route->matchAlias($alias)) { foreach($this->routes as $route) {
return $route->hydrateUrl($params); $potential_aliases[] = $route->alias;
} if ($route->matchAlias($alias)) {
$this->next(); 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);
} }
/** /**

@ -468,6 +468,13 @@ class RouterTest extends PHPUnit\Framework\TestCase
$this->request->method = 'POST'; $this->request->method = 'POST';
$this->check('123abc'); $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() { public function testRewindAndValid() {
$this->router->map('/path1', [$this, 'ok']); $this->router->map('/path1', [$this, 'ok']);
@ -491,7 +498,7 @@ class RouterTest extends PHPUnit\Framework\TestCase
public function testGetUrlByAliasNoMatches() { public function testGetUrlByAliasNoMatches() {
$this->router->map('/path1', [$this, 'ok'], false, 'path1'); $this->router->map('/path1', [$this, 'ok'], false, 'path1');
$this->expectException(\Exception::class); $this->expectException(\Exception::class);
$this->expectExceptionMessage('No route found with alias: path2'); $this->expectExceptionMessage('No route found with alias: \'path2\'');
$this->router->getUrlByAlias('path2'); $this->router->getUrlByAlias('path2');
} }

Loading…
Cancel
Save