Skip to content

Commit

Permalink
Merge pull request #271 from symfony-cmf/static-typing
Browse files Browse the repository at this point in the history
static type declarations
  • Loading branch information
dbu authored Jan 24, 2022
2 parents cc05a17 + 3dbedc9 commit e869573
Show file tree
Hide file tree
Showing 55 changed files with 595 additions and 1,231 deletions.
23 changes: 8 additions & 15 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:

jobs:
test:
name: "PHP ${{ matrix.php-version }}, Symfony ${{ matrix.symfony-require }} ${{ matrix.dependencies }}"
name: "PHP ${{ matrix.php-version }}, Symfony ${{ matrix.symfony-version }} ${{ matrix.dependencies }}"
runs-on: "ubuntu-20.04"
env:
SYMFONY_DEPRECATIONS_HELPER: weak
Expand All @@ -20,24 +20,14 @@ jobs:
fail-fast: false
matrix:
include:
- php-version: "7.2"
dependencies: "lowest"
symfony-require: "4.4.*"

- php-version: "7.4"
symfony-require: "4.4.*"

- php-version: "7.4"
symfony-require: "5.0.*"

- php-version: "8.0"
symfony-require: "5.0.*"
dependencies: "lowest"

- php-version: "8.0"
symfony-require: "6.0.*"
symfony-version: "6"

- php-version: "8.1"
symfony-require: "6.0.*"
symfony-version: "6"

steps:
- name: "Checkout project"
Expand All @@ -52,14 +42,17 @@ jobs:

- name: "Require Specific Symfony Version"
if: "${{ matrix.symfony-version }}"
run: "composer require --no-update symfony/symfony:${{ matrix.symfony-version }}"
run: composer require --no-update symfony/flex && composer config extra.symfony.require ${{ matrix.symfony-version}}

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v1"
with:
dependency-versions: "${{ matrix.dependencies }}"
composer-options: "--prefer-dist"

- name: "Install phpunit"
run: vendor/bin/simple-phpunit install

- name: "Execute test cases"
run: |
if [[ $SYMFONY_PHPUNIT_VERSION == '' ]]; then unset SYMFONY_PHPUNIT_VERSION; fi;
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ Changelog

* [BC Break] Removed deprecated VersatileRouterInterface::supports, as only string route names are
allowed since Symfony 6.
* [BC Break] As with Symfony itself, the route name now must be a `string`. As noted in
the changes for CMF Routing 2.3, to generate a route from an object is to use
`RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` (`cmf_routing_object`) as
route name and pass the route object in the parameters with key
`RouteObjectInterface::ROUTE_OBJECT` (`_route_object`).
* [BC Break] Added static type declarations to interfaces and classes.
* Revoked the deprecation on Router::match because Symfony keeps offering the match without request
object.
* Support Symfony 6
* Support Symfony 6, dropped support for older Symfony versions.

2.3.4
-----
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
}
],
"require": {
"php": "^7.2 || ^8.0",
"symfony/routing": "^4.4 || ^5.0 || ^6.0",
"symfony/http-kernel": "^4.4 || ^5.0 || ^6.0",
"php": "^8.0",
"symfony/routing": "^6.0",
"symfony/http-kernel": "^6.0",
"psr/log": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"doctrine/annotations": "^1.5",
"symfony/phpunit-bridge": "^5.4 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.0 || ^6.0",
"symfony/config": "^4.4 || ^5.0 || ^6.0",
"symfony/event-dispatcher": "^4.4 || ^5.0 || ^6.0",
"symfony-cmf/testing": "^3@dev"
"symfony/phpunit-bridge": "^6.0",
"symfony/dependency-injection": "^6.0",
"symfony/config": "^6.0",
"symfony/event-dispatcher": "^6.0",
"symfony-cmf/testing": "dev-master as 4.2"
},
"suggest": {
"symfony/event-dispatcher": "DynamicRouter can optionally trigger an event at the start of matching. Minimal version (^4.4 || ^5.0)"
"symfony/event-dispatcher": "DynamicRouter can optionally trigger an event at the start of matching. Minimal version ^6.0"
},
"autoload": {
"psr-4": {
Expand Down
40 changes: 15 additions & 25 deletions src/Candidates/Candidates.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,24 @@
class Candidates implements CandidatesInterface
{
/**
* @var array
* @var string[]
*/
protected $locales;
protected array $locales;

/**
* A limit to apply to the number of candidates generated.
*
* This is to prevent abusive requests with a lot of "/". The limit is per
* batch, that is if a locale matches you could get as many as 2 * $limit
* candidates if the URL has that many slashes.
*
* @var int
*/
protected $limit;
protected int $limit;

/**
* @param array $locales The locales to support
* @param int $limit A limit to apply to the candidates generated
* @param string[] $locales The locales to support
* @param int $limit A limit to apply to the candidates generated
*/
public function __construct(array $locales = [], $limit = 20)
public function __construct(array $locales = [], int $limit = 20)
{
$this->setLocales($locales);
$this->limit = $limit;
Expand All @@ -52,9 +50,9 @@ public function __construct(array $locales = [], $limit = 20)
/**
* Set the locales to support by this strategy.
*
* @param array $locales The locales to support
* @param string[] $locales The locales to support
*/
public function setLocales(array $locales)
public function setLocales(array $locales): void
{
$this->locales = $locales;
}
Expand All @@ -64,7 +62,7 @@ public function setLocales(array $locales)
*
* Always returns true.
*/
public function isCandidate($name)
public function isCandidate(string $name): bool
{
return true;
}
Expand All @@ -74,14 +72,11 @@ public function isCandidate($name)
*
* Does nothing.
*/
public function restrictQuery($queryBuilder)
public function restrictQuery(object $queryBuilder): void
{
}

/**
* {@inheritdoc}
*/
public function getCandidates(Request $request)
public function getCandidates(Request $request): array
{
$url = $request->getPathInfo();
$candidates = $this->getCandidatesFor($url);
Expand All @@ -97,11 +92,9 @@ public function getCandidates(Request $request)
/**
* Determine the locale of this URL.
*
* @param string $url The url to determine the locale from
*
* @return string|bool The locale if $url starts with one of the allowed locales
*/
protected function determineLocale($url)
protected function determineLocale(string $url): string|bool
{
if (!count($this->locales)) {
return false;
Expand All @@ -120,13 +113,10 @@ protected function determineLocale($url)
*
* $prefix is prepended to every candidate generated.
*
* @param string $url The URL to split
* @param string $prefix A prefix to prepend to every pattern
*
* @return array Paths that could represent routes that match $url and are
* child of $prefix
* @return string[] Paths that could represent routes that match $url and are
* child of $prefix
*/
protected function getCandidatesFor($url, $prefix = '')
protected function getCandidatesFor(string $url, string $prefix = ''): array
{
$candidates = [];
if ('/' !== $url) {
Expand Down
12 changes: 4 additions & 8 deletions src/Candidates/CandidatesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@
interface CandidatesInterface
{
/**
* @return array a list of paths
* @return string[] a list of paths
*/
public function getCandidates(Request $request);
public function getCandidates(Request $request): array;

/**
* Determine if $name is a valid candidate, e.g. in getRouteByName.
*
* @param string $name
*
* @return bool
*/
public function isCandidate($name);
public function isCandidate(string $name): bool;

/**
* Provide a best effort query restriction to limit a query to only find
* routes that are supported.
*
* @param object $queryBuilder A query builder suited for the storage backend
*/
public function restrictQuery($queryBuilder);
public function restrictQuery(object $queryBuilder): void;
}
Loading

0 comments on commit e869573

Please sign in to comment.