Skip to content

Commit

Permalink
Add psalm, rename Exception to RoutingException
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Mar 11, 2021
1 parent ab169b4 commit 430476d
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ jobs:

- name: Run test suite
run: vendor/bin/phpunit tests

- name: Run static analysis
run: vendor/bin/psalm
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.idea
composer.lock
vendor
coverage
coverage
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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_
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"php": ">=8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
"phpunit/phpunit": "^8.0",
"vimeo/psalm": "^4.6"
},
"autoload": {
"psr-4": {
Expand Down
19 changes: 19 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<UnresolvableInclude errorLevel="suppress" />
</issueHandlers>
</psalm>
4 changes: 2 additions & 2 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php → src/RoutingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Parable\Routing;

class Exception extends \Exception
class RoutingException extends \Exception
{
}
6 changes: 3 additions & 3 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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']);
Expand All @@ -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']);
Expand Down

0 comments on commit 430476d

Please sign in to comment.