Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Apr 13, 2021
2 parents f705dae + 54f1269 commit 14cd73b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.0'
coverage: pcov

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}-v1
key: dependencies-composer-${{ hashFiles('composer.json') }}-v3

- name: Install dependencies
run: composer install --no-suggest --no-interaction --verbose

- name: Install linting tools
run: composer bin linting install --no-suggest --no-interaction --verbose

- name: Check platform requirements
run: composer check-platform-reqs --verbose

Expand Down Expand Up @@ -119,11 +116,11 @@ jobs:
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-${{ matrix.dependency-version }}-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}-v1
key: dependencies-${{ matrix.dependency-version }}-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}-v3

- name: Install dependencies
run: |
composer remove 'phpunit/phpunit' 'ergebnis/composer-normalize' 'orchestra/testbench' 'infection/infection' --dev --no-update --no-interaction --verbose
composer remove 'infection/infection' 'phpunit/phpunit' 'ergebnis/composer-normalize' 'orchestra/testbench' 'orchestra/testbench' 'phpstan/phpstan' 'phpunit/phpunit' 'timacdonald/php-style' 'vimeo/psalm' --dev --no-update --no-interaction --verbose
composer require 'illuminate/support:${{ matrix.laravel }}' 'orchestra/testbench:${{ matrix.testbench }}' --no-update --no-interaction --verbose
composer update --${{ matrix.dependency-version }} --no-suggest --no-interaction --verbose
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
vendor/
composer.lock
/vendor
/composer.lock
/.phpunit.result.cache
/.php_cs.cache
/coverage
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
}
],
"require": {
"php": ">=7.1",
"php": "^7.1 || ^8.0",
"illuminate/support": "~5.5.0 || ~5.6.0 || ~5.7.0 || ~5.8.0 || ^6.0 || ^7.0 || ^8.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4.1",
"ergebnis/composer-normalize": "^2.0",
"infection/infection": "^0.16",
"infection/infection": "^0.21",
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.0"
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^9.0",
"timacdonald/php-style": "dev-master",
"vimeo/psalm": "^4.0"
},
"config": {
"preferred-install": "dist",
Expand All @@ -45,13 +47,11 @@
"scripts": {
"fix": [
"clear",
"@composer bin linting install",
"@composer normalize",
"./vendor/bin/php-cs-fixer fix"
],
"lint": [
"clear",
"@composer bin linting install",
"@composer normalize --dry-run",
"./vendor/bin/php-cs-fixer fix --dry-run",
"./vendor/bin/psalm --threads=8",
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ parameters:
paths:
- src
- tests
ignoreErrors:
excludePaths:
- tests/Middleware/OptionalRequired.php
8 changes: 2 additions & 6 deletions src/HasParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace TiMacDonald\Middleware;

use function assert;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use function is_string;
use ReflectionMethod;
use ReflectionParameter;
use TypeError;
Expand Down Expand Up @@ -159,6 +157,7 @@ private static function validateArgumentMap(Collection $parameters, Collection $

private static function validateParametersAreOptional(Collection $parameters): void
{
/** @var ?ReflectionParameter */
$missingRequiredParameter = $parameters->reject(static function (ReflectionParameter $parameter): bool {
return $parameter->isDefaultValueAvailable() || $parameter->isVariadic();
})
Expand All @@ -168,8 +167,6 @@ private static function validateParametersAreOptional(Collection $parameters): v
return;
}

assert($missingRequiredParameter instanceof ReflectionParameter);

throw new TypeError('Missing required argument $'.$missingRequiredParameter->getName().' for middleware '.static::class.'::handle()');
}

Expand All @@ -189,6 +186,7 @@ private static function validateArgumentMapIsAnAssociativeArray(Collection $argu

private static function validateNoUnexpectedArguments(Collection $parameters, Collection $arguments): void
{
/** @var ?string */
$unexpectedArgument = $arguments->keys()
->first(static function (string $name) use ($parameters): bool {
return ! $parameters->has($name);
Expand All @@ -198,8 +196,6 @@ private static function validateNoUnexpectedArguments(Collection $parameters, Co
return;
}

assert(is_string($unexpectedArgument));

throw new TypeError('Unknown argument $'.$unexpectedArgument.' passed to middleware '.static::class.'::handle()');
}
}
13 changes: 11 additions & 2 deletions tests/HasParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,26 @@ public function testListDoesNotAcceptSubArray(): void
Basic::in(['laravel', ['vue', 'react']]);
}

public function testListDetectsRequiredParametersThatHaveNotBeenProvided(): void
public function testListDetectsRequiredParametersThatHaveNotBeenProvidedAfterAnOptional(): void
{
if (PHP_MAJOR_VERSION >= 8) {
$this->markTestSkipped('Cannot have optional parameter before required parameter in PHP >=8.0.');
}

$this->expectException(TypeError::class);
$this->expectExceptionMessage('Missing required argument $required for middleware Tests\\Middleware\\OptionalRequired::handle()');

OptionalRequired::in(['laravel']);
}

public function testListDetectsRequiredParametersThatHaveNotBeenProvided(): void
{
$this->expectException(TypeError::class);
$this->expectExceptionMessage('Missing required argument $required for middleware Tests\\Middleware\\Required::handle()');

Required::in([]);
}

public function testListDoesNotAcceptAssociativeArray(): void
{
$this->expectException(TypeError::class);
Expand Down Expand Up @@ -185,7 +194,7 @@ public function testMap(): void
$result = Variadic::with(['variadic' => false]);
$this->assertSame('Tests\\Middleware\\Variadic:0', $result);

// Required parameters after optional parameters are no longer allowed as of PHP 8.0.
// Cannot have optional parameter before required parameter in PHP >=8.0.
if (PHP_MAJOR_VERSION < 8) {
$result = OptionalRequired::with(['required' => 'laravel']);
$this->assertSame('Tests\\Middleware\\OptionalRequired:default,laravel', $result);
Expand Down
16 changes: 10 additions & 6 deletions tests/Middleware/OptionalRequired.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

use Closure;
use Illuminate\Http\Request;
use const PHP_MAJOR_VERSION;
use TiMacDonald\Middleware\HasParameters;

class OptionalRequired
{
use HasParameters;

public function handle(Request $request, Closure $next, string $optional = 'default', string $required): void
// Cannot have optional parameter before required parameter in PHP >=8.0.
if (PHP_MAJOR_VERSION < 8) {
class OptionalRequired
{
//
use HasParameters;

public function handle(Request $request, Closure $next, string $optional = 'default', string $required): void
{
//
}
}
}
7 changes: 0 additions & 7 deletions vendor-bin/linting/composer.json

This file was deleted.

0 comments on commit 14cd73b

Please sign in to comment.