Skip to content

Commit

Permalink
Add support for Laravel 10 + 11
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Apr 7, 2024
1 parent 98a5f73 commit 0a40995
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 145 deletions.
20 changes: 5 additions & 15 deletions .github/workflows/check-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,13 @@ concurrency:
cancel-in-progress: true

jobs:
code_l9:
name: "Code check - Laravel 9"
strategy:
matrix:
phpVersion: [ "8.1", "8.2", "8.3" ]
uses: wrk-flow/reusable-workflows/.github/workflows/php-check.yml@59e60b0bf283c13b2a5e1dc70641e62730d81bc0
with:
composerRequireDev: "laravel/framework:^9"
phpVersion: "${{ matrix.phpVersion }}"
secrets: inherit

code_l10:
name: "Code check - Laravel 10"
strategy:
matrix:
phpVersion: [ "8.1", "8.2", "8.3" ]
uses: wrk-flow/reusable-workflows/.github/workflows/php-check.yml@59e60b0bf283c13b2a5e1dc70641e62730d81bc0
uses: wrk-flow/reusable-workflows/.github/workflows/php-check.yml@09ffc469f54061cac1e9cebab2c5896676852b5d
with:
composerRequireDev: "laravel/framework:^10"
phpVersion: "${{ matrix.phpVersion }}"
Expand All @@ -47,7 +37,7 @@ jobs:
strategy:
matrix:
phpVersion: [ "8.2", "8.3" ]
uses: wrk-flow/reusable-workflows/.github/workflows/php-check.yml@59e60b0bf283c13b2a5e1dc70641e62730d81bc0
uses: wrk-flow/reusable-workflows/.github/workflows/php-check.yml@09ffc469f54061cac1e9cebab2c5896676852b5d
with:
composerRequireDev: "laravel/framework:^11"
phpVersion: "${{ matrix.phpVersion }}"
Expand All @@ -58,7 +48,7 @@ jobs:
strategy:
matrix:
phpVersion: [ "8.1", "8.2", "8.3" ]
uses: wrk-flow/reusable-workflows/.github/workflows/php-tests.yml@59e60b0bf283c13b2a5e1dc70641e62730d81bc0
uses: wrk-flow/reusable-workflows/.github/workflows/php-tests.yml@09ffc469f54061cac1e9cebab2c5896676852b5d
with:
composerRequireDev: "laravel/framework:^9"
phpVersion: "${{ matrix.phpVersion }}"
Expand All @@ -69,7 +59,7 @@ jobs:
strategy:
matrix:
phpVersion: [ "8.1", "8.2", "8.3" ]
uses: wrk-flow/reusable-workflows/.github/workflows/php-tests.yml@59e60b0bf283c13b2a5e1dc70641e62730d81bc0
uses: wrk-flow/reusable-workflows/.github/workflows/php-tests.yml@09ffc469f54061cac1e9cebab2c5896676852b5d
with:
composerRequireDev: "laravel/framework:^10"
phpVersion: "${{ matrix.phpVersion }}"
Expand All @@ -80,7 +70,7 @@ jobs:
strategy:
matrix:
phpVersion: [ "8.2", "8.3" ]
uses: wrk-flow/reusable-workflows/.github/workflows/php-tests.yml@59e60b0bf283c13b2a5e1dc70641e62730d81bc0
uses: wrk-flow/reusable-workflows/.github/workflows/php-tests.yml@09ffc469f54061cac1e9cebab2c5896676852b5d
with:
composerRequireDev: "laravel/framework:^11"
phpVersion: "${{ matrix.phpVersion }}"
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
],
"require": {
"php": ">=8.1",
"laravel/framework": "^v9.52.16 | ^v10.48.4 | ^v11.2.0",
"psr/log": "^2 | ^3",
"laravel/framework": "^v10.48.4 | ^v11.2.0",
"psr/log": "^3",
"psr/simple-cache": "^3.0"
},
"require-dev": {
"larastrict/conventions": "v0.1.1",
"mockery/mockery": "^1.6",
"nette/php-generator": "^v4.1.4",
"orchestra/testbench": "^v7.41 | ^v8.22.2 | ^v9.0.3",
"orchestra/testbench": "^v8.22.2 | ^v9.0.3",
"phpstan/phpstan-mockery": "^1.1.0",
"phpstan/phpstan-phpunit": "^v1.3.16"
},
Expand All @@ -36,6 +36,7 @@
"@php ./vendor/bin/testbench package:discover --ansi"
],
"analyse": "./vendor/bin/phpstan",
"analyse:b": "./vendor/bin/phpstan -b",
"check": [
"@lint",
"@test",
Expand All @@ -47,10 +48,8 @@
],
"lint:check": "./vendor/bin/ecs",
"lint:fix": "./vendor/bin/ecs --fix",
"lint:stan": "./vendor/bin/phpstan",
"lint:upgrade": "vendor/bin/rector process",
"lint:upgrade:check": "vendor/bin/rector process --dry-run",
"lint:stan:b": "./vendor/bin/phpstan -b",
"test": "./vendor/bin/phpunit",
"test:stubs": "STUBS_GENERATE=true ./vendor/bin/phpunit",
"test:coverage": "./vendor/bin/phpunit --coverage-text"
Expand Down
4 changes: 0 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ parameters:
count: 1
path: src/Http/Resources/JsonResourceCollection.php

-
message: "#^Method LaraStrict\\\\Log\\\\Channels\\\\ConsoleOutputChannel\\:\\:getLevel\\(\\) should return 100\\|200\\|250\\|300\\|400\\|500\\|550\\|600\\|'ALERT'\\|'alert'\\|'CRITICAL'\\|'critical'\\|'DEBUG'\\|'debug'\\|'EMERGENCY'\\|'emergency'\\|'ERROR'\\|'error'\\|'INFO'\\|'info'\\|'NOTICE'\\|'notice'\\|'WARNING'\\|'warning' but returns int\\|string\\.$#"
count: 1
path: src/Log/Channels/ConsoleOutputChannel.php

-
message: "#^Parameter \\$getValue of method LaraStrict\\\\Cache\\\\Contracts\\\\CacheMeServiceContract\\:\\:get\\(\\) expects Closure\\(mixed, mixed, mixed, mixed, mixed, mixed\\)\\: LaraStrict\\\\Providers\\\\Entities\\\\AppServiceProviderEntity, Closure\\(Illuminate\\\\Contracts\\\\Foundation\\\\Application\\)\\: LaraStrict\\\\Providers\\\\Entities\\\\AppServiceProviderEntity given\\.$#"
Expand Down
19 changes: 8 additions & 11 deletions src/Log/Channels/ConsoleOutputChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
use Illuminate\Log\LogManager;
use LaraStrict\Log\Handlers\ConsoleOutputHandler;
use LaraStrict\Log\Managers\ConsoleOutputManager;
use Monolog\Level;
use Monolog\Logger;
use Psr\Log\LogLevel;

/**
* @internal
* @phpstan-import-type LevelName from Logger
* @phpstan-import-type Level from Logger
*/
final class ConsoleOutputChannel extends LogManager
{
Expand All @@ -22,7 +20,6 @@ final class ConsoleOutputChannel extends LogManager
*/
public function __invoke(array $config): Logger
{

$handler = new ConsoleOutputHandler(
manager: $this->app->make(ConsoleOutputManager::class),
level: self::getLevel($config),
Expand All @@ -36,21 +33,21 @@ public function __invoke(array $config): Logger
}

/**
* @param array<string, mixed> $config
*
* @phpstan-return Level|LevelName|LogLevel::*
* @param array<string, mixed> $config
*/
private static function getLevel(array $config): int|string
private static function getLevel(array $config): Level
{
if (array_key_exists('level', $config)) {
$level = $config['level'];

if (is_string($level) || is_int($level)) {
return $level;
if (is_string($level) || is_int($level) || $level instanceof Level) {
// TODO: I have no clue hot to make PHPStan happy here
// @phpstan-ignore-next-line
return Logger::toMonologLevel($level);
}
}

return Logger::DEBUG;
return Level::Debug;
}

/**
Expand Down
33 changes: 17 additions & 16 deletions src/Log/Handlers/ConsoleOutputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@
use Illuminate\Console\View\Components\Factory;
use LaraStrict\Log\Managers\ConsoleOutputManager;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @internal
* @phpstan-import-type LevelName from Logger
* @phpstan-import-type Level from Logger
*/
final class ConsoleOutputHandler extends AbstractProcessingHandler
{
/**
* @phpstan-param Level|LevelName|LogLevel::* $level
* @param int|string|Level|LogLevel::* $level The minimum logging level at which this handler will be triggered
*
* @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level
*/
public function __construct(
private readonly ConsoleOutputManager $manager,
string|int $level = Logger::DEBUG,
string|int|Level $level = Level::Debug,
bool $bubble = true
) {
parent::__construct($level, $bubble);
}

protected function write(array $record): void
protected function write(LogRecord $record): void
{
$consoleOutputFactory = $this->manager->getOutputFactory();
$outputStyle = $this->manager->getOutputStyle();
Expand All @@ -43,27 +44,27 @@ protected function write(array $record): void
return;
}

$context = $record['context'];
$context = $record->context;
$hasContext = $context !== [];
$message = $record['message'];
$message = $record->message;

switch ($record['level']) {
case Logger::NOTICE:
case Logger::INFO:
case Level::Notice:
case Level::Info:
$consoleOutputFactory->info($message);
break;
case Logger::WARNING:
case Level::Warning:
$consoleOutputFactory->warn($message);
break;
case Logger::ALERT:
case Logger::EMERGENCY:
case Logger::CRITICAL:
case Level::Alert:
case Level::Emergency:
case Level::Critical:
$consoleOutputFactory->alert($message);
break;
case Logger::ERROR:
case Level::Error:
$consoleOutputFactory->error($message);
break;
case Logger::DEBUG:
case Level::Debug:
if ($outputStyle->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL) {
return;
}
Expand Down
15 changes: 9 additions & 6 deletions src/Validation/Rules/ArrayOrStringRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

namespace LaraStrict\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class ArrayOrStringRule implements Rule
final class ArrayOrStringRule implements ValidationRule
{
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return is_array($value) || is_string($value);
if (self::passes($value) === false) {
$fail('Given :attribute must by array or string');
}
}

public function message(): string
private function passes(mixed $value): bool
{
return 'Given :attribute must by array or string';
return is_array($value) || is_string($value);
}
}
19 changes: 13 additions & 6 deletions src/Validation/Rules/BooleanRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@

namespace LaraStrict\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class BooleanRule implements Rule
final class BooleanRule implements ValidationRule
{
public function passes($attribute, $value): bool
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return in_array($value, [true, false, 'true', 'false', 0, 1, '0', '1'], true);
if (self::passes($value) === false) {
$fail('Given :attribute is not a valid boolean: true, false, 0, 1 (can be string or numeric value).');
}
}

public function message(): string
private function passes(mixed $value): bool
{
return 'Given :attribute is not a valid boolean: true, false, 0, 1 (can be string or numeric value).';
if (is_array($value) === false) {
return false;
}

return in_array($value, [true, false, 'true', 'false', 0, 1, '0', '1'], true);
}
}
19 changes: 11 additions & 8 deletions src/Validation/Rules/NumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

namespace LaraStrict\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use LaraStrict\Core\Helpers\Value;

class NumberRule implements Rule
final class NumberRule implements ValidationRule
{
public function passes($attribute, $value): bool
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (self::passes($value) === false) {
$fail('Given :attribute is not a valid number or it exceeds int/float limits.');
}
}

private static function passes(mixed $value): bool
{
if (is_string($value) === false && is_numeric($value) === false) {
return false;
Expand All @@ -29,11 +37,6 @@ public function passes($attribute, $value): bool
return str_contains((string) $value, 'E+') === false;
}

public function message(): string
{
return 'Given :attribute is not a valid number or it exceeds int/float limits.';
}

/**
* @return ($value is non-empty-string ? bool : ($value is int ? true : false))
*/
Expand Down
15 changes: 9 additions & 6 deletions src/Validation/Rules/PerPageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@

namespace LaraStrict\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class PerPageRule implements Rule
final class PerPageRule implements ValidationRule
{
public function __construct(
private readonly int $max = 100
) {
}

public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return is_numeric($value) && $value > 0 && $value <= $this->max;
if ($this->passes($value) === false) {
$fail('Not a valid :attribute. Must be between 1 - ' . $this->max);
}
}

public function message()
private function passes(mixed $value): bool
{
return 'Not a valid :attribute. Must be between 1 - ' . $this->max;
return is_numeric($value) && $value > 0 && $value <= $this->max;
}
}
19 changes: 11 additions & 8 deletions src/Validation/Rules/RemoteUrlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace LaraStrict\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class RemoteUrlRule implements Rule
final class RemoteUrlRule implements ValidationRule
{
public function passes($attribute, $value): bool
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($this->passes($value) === false) {
$fail('Given :attribute is not a valid url (public IP or domain on http/s protocol)');
}
}

private function passes(mixed $value): bool
{
if (is_string($value) === false) {
return false;
Expand Down Expand Up @@ -37,9 +45,4 @@ public function passes($attribute, $value): bool
// Must contain top level domain
return preg_match('#^[\w\d\-.]{1,63}\.[a-z]{2,6}$#', $host) !== 0;
}

public function message(): string
{
return 'Given :attribute is not a valid url (public IP or domain on http/s protocol)';
}
}
Loading

0 comments on commit 0a40995

Please sign in to comment.