From bb53114d78de19b11dcb0645bf11afeaecfb7dac Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Tue, 21 Nov 2023 10:23:01 +0100 Subject: [PATCH] apply suggestions --- .php-cs-fixer.php | 2 +- ci/.drone.yml | 2 +- package.json | 2 +- phpstan.neon | 16 +++++++--------- src/Controller/Control.php | 4 ++-- src/Controller/SampleFormController.php | 2 +- src/Data/Account.php | 4 ++-- src/Data/Team.php | 6 +++--- src/Http/HttpRequest.php | 3 +-- src/Validation/FunctionValidator.php | 4 ++-- src/Validation/SimpleFunctionValidator.php | 8 ++++---- src/Validation/Validation.php | 2 +- src/Validation/Validator.php | 2 +- src/Validation/Validators.php | 4 ++-- 14 files changed, 29 insertions(+), 32 deletions(-) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 265de93..77ef0e7 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -5,7 +5,7 @@ $finder = (new PhpCsFixer\Finder())->in(__DIR__); return (new PhpCsFixer\Config()) ->setRules([ '@PER-CS' => true, - '@PHP82Migration' => true, + '@PHP74Migration' => true, 'array_syntax' => ['syntax' => 'short'], 'braces_position' => [ 'classes_opening_brace' => 'same_line', diff --git a/ci/.drone.yml b/ci/.drone.yml index 2431032..8b7058d 100644 --- a/ci/.drone.yml +++ b/ci/.drone.yml @@ -21,7 +21,7 @@ steps: - image: composer:latest name: "php CI" commands: - - composer install && composer update + - composer install - vendor/bin/phpstan analyze - image: node:latest diff --git a/package.json b/package.json index a3ba0ad..79c9d46 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "build": "vite build", "test": "vite test", "format": "prettier --config .prettierrc 'front' --write", - "tsc": "node_modules/.bin/tsc" + "tsc": "tsc" }, "eslintConfig": { "extends": [ diff --git a/phpstan.neon b/phpstan.neon index 7801d9b..bc6c041 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,12 +4,10 @@ parameters: paths: - src - public - - sql - ignoreErrors: - - - message: '#.*#' - path: sql/database.php - - - message: '#.*#' - path: src/react-display-file.php - + scanFiles: + - config.php + - sql/database.php + - profiles/dev-config-profile.php + - profiles/prod-config-profile.php + excludePaths: + - src/react-display-file.php diff --git a/src/Controller/Control.php b/src/Controller/Control.php index e50eed0..2b428d9 100644 --- a/src/Controller/Control.php +++ b/src/Controller/Control.php @@ -14,7 +14,7 @@ class Control { /** * Runs given callback, if the request's json validates the given schema. * @param array $schema an array of `fieldName => Validators` which represents the request object schema - * @param callable $run the callback to run if the request is valid according to the given schema. + * @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema. * THe callback must accept an HttpRequest, and return an HttpResponse object. * @param bool $errorInJson if set to true, the returned response, in case of errors, will be a JsonHttpResponse, instead * of the ViewHttpResponse for an error view. @@ -38,7 +38,7 @@ class Control { * Runs given callback, if the given request data array validates the given schema. * @param array $data the request's data array. * @param array $schema an array of `fieldName => Validators` which represents the request object schema - * @param callable $run the callback to run if the request is valid according to the given schema. + * @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema. * THe callback must accept an HttpRequest, and return an HttpResponse object. * @param bool $errorInJson if set to true, the returned response, in case of errors, will be a JsonHttpResponse, instead * of the ViewHttpResponse for an error view. diff --git a/src/Controller/SampleFormController.php b/src/Controller/SampleFormController.php index 41b5f96..773a4db 100644 --- a/src/Controller/SampleFormController.php +++ b/src/Controller/SampleFormController.php @@ -31,7 +31,7 @@ class SampleFormController { /** * @param array $form - * @param callable $response + * @param callable(array>): ViewHttpResponse $response * @return HttpResponse */ private function submitForm(array $form, callable $response): HttpResponse { diff --git a/src/Data/Account.php b/src/Data/Account.php index 51d7aad..2a21bf1 100755 --- a/src/Data/Account.php +++ b/src/Data/Account.php @@ -29,7 +29,7 @@ class Account { private AccountUser $user; /** - * @var array account's teams + * @var Team[] account's teams */ private array $teams; @@ -43,7 +43,7 @@ class Account { * @param string $email * @param string $phoneNumber * @param AccountUser $user - * @param array $teams + * @param Team[] $teams * @param int $id */ public function __construct(string $email, string $phoneNumber, AccountUser $user, array $teams, int $id) { diff --git a/src/Data/Team.php b/src/Data/Team.php index a224bd4..aca4e0d 100755 --- a/src/Data/Team.php +++ b/src/Data/Team.php @@ -11,7 +11,7 @@ class Team { private Color $secondColor; /** - * @var array maps users with their role + * @var Member[] maps users with their role */ private array $members; @@ -20,7 +20,7 @@ class Team { * @param Url $picture * @param Color $mainColor * @param Color $secondColor - * @param array $members + * @param Member[] $members */ public function __construct(string $name, Url $picture, Color $mainColor, Color $secondColor, array $members) { $this->name = $name; @@ -59,7 +59,7 @@ class Team { } /** - * @return array + * @return Member[] */ public function listMembers(): array { return $this->members; diff --git a/src/Http/HttpRequest.php b/src/Http/HttpRequest.php index 6414d75..d199227 100644 --- a/src/Http/HttpRequest.php +++ b/src/Http/HttpRequest.php @@ -30,7 +30,7 @@ class HttpRequest implements ArrayAccess { * This is a simple function that only supports flat schemas (non-composed, the data must only be a k/v array pair.) * @param array $request the request's data * @param array $fails a reference to a failure array, that will contain the reported validation failures. - * @param array> $schema the schema to satisfy. a schema is a simple array with a string key (which is the top-level field name), and a set of validators + * @param array $schema the schema to satisfy. a schema is a simple array with a string key (which is the top-level field name), and a set of validators * @return HttpRequest|null the built HttpRequest instance, or null if a field is missing, or if any of the schema validator failed */ public static function from(array $request, array &$fails, array $schema): ?HttpRequest { @@ -65,7 +65,6 @@ class HttpRequest implements ArrayAccess { /** * @param $offset * @param $value - * @return mixed * @throws Exception */ public function offsetSet($offset, $value) { diff --git a/src/Validation/FunctionValidator.php b/src/Validation/FunctionValidator.php index f052ea6..4949d8b 100644 --- a/src/Validation/FunctionValidator.php +++ b/src/Validation/FunctionValidator.php @@ -4,12 +4,12 @@ namespace App\Validation; class FunctionValidator extends Validator { /** - * @var callable + * @var callable(string, mixed): ValidationFail[] */ private $validate_fn; /** - * @param callable $validate_fn the validate function. Must have the same signature as the {@link Validator::validate()} method. + * @param callable(string, mixed): ValidationFail[] $validate_fn the validate function. Must have the same signature as the {@link Validator::validate()} method. */ public function __construct(callable $validate_fn) { $this->validate_fn = $validate_fn; diff --git a/src/Validation/SimpleFunctionValidator.php b/src/Validation/SimpleFunctionValidator.php index 7514bae..cec52c0 100644 --- a/src/Validation/SimpleFunctionValidator.php +++ b/src/Validation/SimpleFunctionValidator.php @@ -7,17 +7,17 @@ namespace App\Validation; */ class SimpleFunctionValidator extends Validator { /** - * @var callable + * @var callable(mixed): bool */ private $predicate; /** - * @var callable + * @var callable(string): ValidationFail[] */ private $errorFactory; /** - * @param callable $predicate a function predicate with signature: `(string) => bool`, to validate the given string - * @param callable $errorsFactory a factory function with signature `(string) => array` to emit failures when the predicate fails + * @param callable(mixed): bool $predicate a function predicate with signature: `(string) => bool`, to validate the given string + * @param callable(string): ValidationFail[] $errorsFactory a factory function with signature `(string) => array` to emit failures when the predicate fails */ public function __construct(callable $predicate, callable $errorsFactory) { $this->predicate = $predicate; diff --git a/src/Validation/Validation.php b/src/Validation/Validation.php index 19144b7..f1392e6 100644 --- a/src/Validation/Validation.php +++ b/src/Validation/Validation.php @@ -10,7 +10,7 @@ class Validation { * Validate a value from validators, appending failures in the given errors array. * @param mixed $val the value to validate * @param string $valName the name of the value - * @param array $failures array to push when a validator fails + * @param ValidationFail[] $failures array to push when a validator fails * @param Validator ...$validators given validators * @return bool true if any of the given validators did fail */ diff --git a/src/Validation/Validator.php b/src/Validation/Validator.php index 4ad6131..8227e46 100644 --- a/src/Validation/Validator.php +++ b/src/Validation/Validator.php @@ -7,7 +7,7 @@ abstract class Validator { * validates a variable string * @param string $name the name of the tested value * @param mixed $val the value to validate - * @return array the errors the validator has reported + * @return ValidationFail[] the errors the validator has reported */ abstract public function validate(string $name, $val): array; diff --git a/src/Validation/Validators.php b/src/Validation/Validators.php index 6f72cf2..94aea23 100644 --- a/src/Validation/Validators.php +++ b/src/Validation/Validators.php @@ -9,7 +9,7 @@ class Validators { /** * @return Validator a validator that validates a given regex */ - public static function regex(string $regex, string $msg = null): Validator { + public static function regex(string $regex, ?string $msg = null): Validator { return new SimpleFunctionValidator( fn(string $str) => preg_match($regex, $str), fn(string $name) => [new FieldValidationFail($name, $msg == null ? "field does not validates pattern $regex" : $msg)] @@ -19,7 +19,7 @@ class Validators { /** * @return Validator a validator that validates strings that only contains numbers, letters, accents letters, `-` and `_`. */ - public static function name(string $msg = null): Validator { + public static function name(?string $msg = null): Validator { return self::regex("/^[0-9a-zA-Zà-üÀ-Ü_-]*$/", $msg); }