Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be explicit about internal and final components #275

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/BadRouteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use function sprintf;

/** @final */
class BadRouteException extends LogicException
{
public static function alreadyRegistered(string $route, string $method): self
Expand Down
1 change: 1 addition & 0 deletions src/DataGenerator/CharCountBased.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use function count;
use function implode;

/** @final */
class CharCountBased extends RegexBasedAbstract
{
protected function getApproxChunkSize(): int
Expand Down
1 change: 1 addition & 0 deletions src/DataGenerator/GroupCountBased.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use function max;
use function str_repeat;

/** @final */
class GroupCountBased extends RegexBasedAbstract
{
protected function getApproxChunkSize(): int
Expand Down
1 change: 1 addition & 0 deletions src/DataGenerator/GroupPosBased.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use function count;
use function implode;

/** @final */
class GroupPosBased extends RegexBasedAbstract
{
protected function getApproxChunkSize(): int
Expand Down
1 change: 1 addition & 0 deletions src/DataGenerator/MarkBased.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use function implode;

/** @final */
class MarkBased extends RegexBasedAbstract
{
protected function getApproxChunkSize(): int
Expand Down
2 changes: 2 additions & 0 deletions src/DataGenerator/RegexBasedAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
use function round;

/**
* @internal
*
* @phpstan-import-type StaticRoutes from DataGenerator
* @phpstan-import-type DynamicRouteChunk from DataGenerator
* @phpstan-import-type DynamicRoutes from DataGenerator
* @phpstan-import-type RouteData from DataGenerator
* @phpstan-import-type ExtraParameters from DataGenerator
*/
abstract class RegexBasedAbstract implements DataGenerator

Check failure on line 28 in src/DataGenerator/RegexBasedAbstract.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

FastRoute\DataGenerator\RegexBasedAbstract was marked "@internal"
{
/** @var StaticRoutes */
protected array $staticRoutes = [];
Expand Down
1 change: 1 addition & 0 deletions src/Dispatcher/CharCountBased.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function end;
use function preg_match;

/** @final */
class CharCountBased extends RegexBasedAbstract
{
/** @inheritDoc */
Expand Down
1 change: 1 addition & 0 deletions src/Dispatcher/GroupCountBased.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use function count;
use function preg_match;

/** @final */
class GroupCountBased extends RegexBasedAbstract
{
/** @inheritDoc */
Expand Down
1 change: 1 addition & 0 deletions src/Dispatcher/GroupPosBased.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use function assert;
use function preg_match;

/** @final */
class GroupPosBased extends RegexBasedAbstract
{
/** @inheritDoc */
Expand Down
1 change: 1 addition & 0 deletions src/Dispatcher/MarkBased.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use function preg_match;

/** @final */
class MarkBased extends RegexBasedAbstract
{
/** @inheritDoc */
Expand Down
2 changes: 2 additions & 0 deletions src/Dispatcher/RegexBasedAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use FastRoute\Dispatcher\Result\NotMatched;

/**
* @internal
*
* @phpstan-import-type StaticRoutes from DataGenerator
* @phpstan-import-type DynamicRouteChunk from DataGenerator
* @phpstan-import-type DynamicRouteChunks from DataGenerator
* @phpstan-import-type DynamicRoutes from DataGenerator
* @phpstan-import-type RouteData from DataGenerator
*/
abstract class RegexBasedAbstract implements Dispatcher

Check failure on line 21 in src/Dispatcher/RegexBasedAbstract.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

FastRoute\Dispatcher\RegexBasedAbstract was marked "@internal"
{
/** @var StaticRoutes */
protected array $staticRouteMap = [];
Expand Down
6 changes: 5 additions & 1 deletion src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
use function preg_match;
use function preg_quote;

/** @phpstan-import-type ExtraParameters from DataGenerator */
/**
* @internal
*
* @phpstan-import-type ExtraParameters from DataGenerator
*/
class Route

Check failure on line 15 in src/Route.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

FastRoute\Route was marked "@internal"
{
public readonly string $regex;

Expand Down
1 change: 1 addition & 0 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/**
* @phpstan-import-type RouteData from DataGenerator
* @phpstan-import-type ExtraParameters from DataGenerator
* @final
*/
class RouteCollector
{
Expand Down
6 changes: 4 additions & 2 deletions src/RouteParser/Std.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use function preg_match_all;
use function preg_split;
use function rtrim;
use function str_contains;
use function strlen;
use function strpos;
use function substr;
use function trim;

Expand All @@ -26,6 +26,8 @@
* Parses route strings of the following form:
*
* "/user/{name}[/{id:[0-9]+}]"
*
* @final
*/
class Std implements RouteParser
{
Expand Down Expand Up @@ -135,7 +137,7 @@ private function parsePlaceholders(string $route): array
private function guardAgainstCapturingGroupUsage(string $regex, string $variableName): void
{
// Needs to have at least a ( to contain a capturing group
if (strpos($regex, '(') === false) {
if (! str_contains($regex, '(')) {
return;
}

Expand Down
26 changes: 0 additions & 26 deletions test/DummyRouteCollector.php

This file was deleted.

52 changes: 43 additions & 9 deletions test/RouteCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@

namespace FastRoute\Test;

use FastRoute\DataGenerator;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

class RouteCollectorTest extends TestCase
use function assert;
use function count;
use function is_string;

final class RouteCollectorTest extends TestCase
{
#[PHPUnit\Test]
public function shortcutsCanBeUsedToRegisterRoutes(): void
{
$r = new DummyRouteCollector();
$dataGenerator = self::dummyDataGenerator();
$r = new RouteCollector(new Std(), $dataGenerator);

$r->any('/any', 'any');
$r->delete('/delete', 'delete');
Expand All @@ -33,13 +41,15 @@ public function shortcutsCanBeUsedToRegisterRoutes(): void
['OPTIONS', '/options', 'options', ['_route' => '/options']],
];

self::assertSame($expected, $r->routes);
self::assertObjectHasProperty('routes', $dataGenerator);
self::assertSame($expected, $dataGenerator->routes);
}

#[PHPUnit\Test]
public function routesCanBeGrouped(): void
{
$r = new DummyRouteCollector();
$dataGenerator = self::dummyDataGenerator();
$r = new RouteCollector(new Std(), $dataGenerator);

$r->delete('/delete', 'delete');
$r->get('/get', 'get');
Expand All @@ -49,7 +59,7 @@ public function routesCanBeGrouped(): void
$r->put('/put', 'put');
$r->options('/options', 'options');

$r->addGroup('/group-one', static function (DummyRouteCollector $r): void {
$r->addGroup('/group-one', static function (RouteCollector $r): void {
$r->delete('/delete', 'delete');
$r->get('/get', 'get');
$r->head('/head', 'head');
Expand All @@ -58,7 +68,7 @@ public function routesCanBeGrouped(): void
$r->put('/put', 'put');
$r->options('/options', 'options');

$r->addGroup('/group-two', static function (DummyRouteCollector $r): void {
$r->addGroup('/group-two', static function (RouteCollector $r): void {
$r->delete('/delete', 'delete');
$r->get('/get', 'get');
$r->head('/head', 'head');
Expand All @@ -69,10 +79,10 @@ public function routesCanBeGrouped(): void
});
});

$r->addGroup('/admin', static function (DummyRouteCollector $r): void {
$r->addGroup('/admin', static function (RouteCollector $r): void {
$r->get('-some-info', 'admin-some-info');
});
$r->addGroup('/admin-', static function (DummyRouteCollector $r): void {
$r->addGroup('/admin-', static function (RouteCollector $r): void {
$r->get('more-info', 'admin-more-info');
});

Expand Down Expand Up @@ -102,6 +112,30 @@ public function routesCanBeGrouped(): void
['GET', '/admin-more-info', 'admin-more-info', ['_route' => '/admin-more-info']],
];

self::assertSame($expected, $r->routes);
self::assertObjectHasProperty('routes', $dataGenerator);
self::assertSame($expected, $dataGenerator->routes);
}

private static function dummyDataGenerator(): DataGenerator
{
return new class implements DataGenerator
{
/** @var list<array{string, string, mixed, array<string, bool|float|int|string>}> */
public array $routes = [];

/** @inheritDoc */
public function getData(): array
{
return [[], []];
}

/** @inheritDoc */
public function addRoute(string $httpMethod, array $routeData, mixed $handler, array $extraParameters = []): void
{
assert(count($routeData) === 1 && is_string($routeData[0]));

$this->routes[] = [$httpMethod, $routeData[0], $handler, $extraParameters];
}
};
}
}
Loading