From 430476d0778021ca523bff24f3ce9f2d94f79f23 Mon Sep 17 00:00:00 2001 From: Robin de Graaf Date: Thu, 11 Mar 2021 13:44:56 +0000 Subject: [PATCH] Add psalm, rename Exception to RoutingException --- .github/workflows/tests.yml | 3 +++ .gitignore | 3 ++- CHANGELOG.md | 7 +++++++ Makefile | 8 ++++++-- composer.json | 3 ++- psalm.xml | 19 +++++++++++++++++++ src/Route.php | 4 ++-- src/Router.php | 4 ++-- src/{Exception.php => RoutingException.php} | 2 +- tests/RouteTest.php | 6 +++--- tests/RouterTest.php | 6 +++--- 11 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 psalm.xml rename src/{Exception.php => RoutingException.php} (60%) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5d3a185..d181c60 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -32,3 +32,6 @@ jobs: - name: Run test suite run: vendor/bin/phpunit tests + + - name: Run static analysis + run: vendor/bin/psalm diff --git a/.gitignore b/.gitignore index 41f9a33..9c59449 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ +.DS_Store .idea composer.lock vendor -coverage \ No newline at end of file +coverage diff --git a/CHANGELOG.md b/CHANGELOG.md index d48a4df..f4a648d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Parable Routing +## 0.4.0 + +_Changes_ + +- Add static analysis using psalm. +- `Exception` has been renamed to `RoutingException` for clarity. + ## 0.3.1 _Fixes_ diff --git a/Makefile b/Makefile index 79c8f34..289792f 100644 --- a/Makefile +++ b/Makefile @@ -4,16 +4,20 @@ dependencies: --no-plugins \ --no-scripts +psalm: + vendor/bin/psalm --clear-cache + vendor/bin/psalm + tests: dependencies vendor/bin/phpunit --verbose tests coverage: dependencies rm -rf ./coverage - vendor/bin/phpunit --coverage-html ./coverage tests + XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests tests-clean: vendor/bin/phpunit --verbose tests coverage-clean: rm -rf ./coverage - vendor/bin/phpunit --coverage-html ./coverage tests \ No newline at end of file + XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests diff --git a/composer.json b/composer.json index cda673d..d9cc2e9 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "php": ">=8.0" }, "require-dev": { - "phpunit/phpunit": "^8.0" + "phpunit/phpunit": "^8.0", + "vimeo/psalm": "^4.6" }, "autoload": { "psr-4": { diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..8679d7a --- /dev/null +++ b/psalm.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + diff --git a/src/Route.php b/src/Route.php index dc2d8a8..9c5b09f 100644 --- a/src/Route.php +++ b/src/Route.php @@ -88,11 +88,11 @@ public function setParameterValues(ParameterValues $values): void $parameters = $this->getParameters(); if (count($values->getAll()) !== count($parameters)) { - throw new Exception('Number of values do not match Route parameters.'); + throw new RoutingException('Number of values do not match Route parameters.'); } if (array_diff($values->getNames(), $parameters)) { - throw new Exception('Values names do not match Route parameters.'); + throw new RoutingException('Values names do not match Route parameters.'); } $this->parameterValues = $values; diff --git a/src/Router.php b/src/Router.php index 5538c7e..871313c 100644 --- a/src/Router.php +++ b/src/Router.php @@ -74,7 +74,7 @@ public function buildRouteUrl(string $name, array $parameters = []): string $route = $this->getRouteByName($name); if ($route === null) { - throw new Exception(sprintf("Route '%s' not found.", $name)); + throw new RoutingException(sprintf("Route '%s' not found.", $name)); } if (!$route->hasParameters()) { @@ -87,7 +87,7 @@ public function buildRouteUrl(string $name, array $parameters = []): string $parameter = '{' . $parameterName . '}'; if (!str_contains($url, $parameter)) { - throw new Exception(sprintf( + throw new RoutingException(sprintf( "Parameter '%s' not found in url '%s'.", $parameterName, $url diff --git a/src/Exception.php b/src/RoutingException.php similarity index 60% rename from src/Exception.php rename to src/RoutingException.php index 8ac4a78..fc84b82 100644 --- a/src/Exception.php +++ b/src/RoutingException.php @@ -2,6 +2,6 @@ namespace Parable\Routing; -class Exception extends \Exception +class RoutingException extends \Exception { } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index d462208..4ea5bfa 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -2,7 +2,7 @@ namespace Parable\Routing\Tests; -use Parable\Routing\Exception; +use Parable\Routing\RoutingException; use Parable\Routing\Route; use Parable\Routing\Route\Metadata; use PHPUnit\Framework\TestCase; @@ -61,7 +61,7 @@ public function testParameteredRouteAndParameterValues(): void public function testSetValuesThrowsOnInvalidCount(): void { - $this->expectException(Exception::class); + $this->expectException(RoutingException::class); $this->expectExceptionMessage('Number of values do not match Route parameters.'); $route = new Route( @@ -76,7 +76,7 @@ public function testSetValuesThrowsOnInvalidCount(): void public function testSetValuesThrowsOnInvalidValueNames(): void { - $this->expectException(Exception::class); + $this->expectException(RoutingException::class); $this->expectExceptionMessage('Values names do not match Route parameters.'); $route = new Route( diff --git a/tests/RouterTest.php b/tests/RouterTest.php index f7628b7..c59f094 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -2,10 +2,10 @@ namespace Parable\Routing\Tests; -use Parable\Routing\Exception; use Parable\Routing\Route; use Parable\Routing\Route\ParameterValues; use Parable\Routing\Router; +use Parable\Routing\RoutingException; use Parable\Routing\Tests\Classes\Controller; use PHPUnit\Framework\TestCase; @@ -251,7 +251,7 @@ public function testBuildRouteUrl(): void public function testBuildRouteUrlThrowsOnUnknownName(): void { - $this->expectException(Exception::class); + $this->expectException(RoutingException::class); $this->expectExceptionMessage("Route 'nope' not found."); $this->router->buildRouteUrl('nope', ['id' => 2, 'name' => 'stuff']); @@ -261,7 +261,7 @@ public function testBuildRouteUrlThrowsOnUrlWithWrongParameters(): void { $this->setUpDefaultRoutesAndAssert(); - $this->expectException(Exception::class); + $this->expectException(RoutingException::class); $this->expectExceptionMessage("Parameter 'id2' not found in url '/complex/{id}/{name}'."); $this->router->buildRouteUrl('complex', ['id2' => 2, 'name2' => 'stuff']);