fixed alias issue, levenshtein recommendations and coverage-check

pull/516/head
n0nag0n 1 year ago
parent 4b98a610eb
commit b388a26765

3
.gitignore vendored

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

@ -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": {
@ -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": {

@ -190,14 +190,32 @@ class Router
* @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);
}
/**

@ -468,6 +468,13 @@ class RouterTest extends PHPUnit\Framework\TestCase
$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']);
@ -491,7 +498,7 @@ class RouterTest extends PHPUnit\Framework\TestCase
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');
}

Loading…
Cancel
Save