diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..1344ecb --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,11 @@ +exclude('vendor') + ->in(__DIR__); + +$config = new PhpCsFixer\Config(); +return $config->setRules([ + '@PSR12' => true, + ]) + ->setFinder($finder); diff --git a/composer.json b/composer.json index 039cdc6..9cad152 100644 --- a/composer.json +++ b/composer.json @@ -9,9 +9,9 @@ "laravel/framework": "^8.0|^9.0|^10.0" }, "require-dev": { - "nunomaduro/larastan": "^1.0", - "laravel/pint": "^1.0", - "orchestra/testbench": "^7.6" + "phpstan/phpstan": "^1.10", + "friendsofphp/php-cs-fixer": "^3.26", + "squizlabs/php_codesniffer": "^3.7" }, "autoload": { "psr-4": {"AnourValar\\EloquentValidation\\": "src/"} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..f7ab15d --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,35 @@ + + + + + PHPCS ruleset + + src + + + + + + + + + + + + + + + + + + + + + tests/ + + + + tests/ + + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..cc3f270 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,26 @@ +parameters: + + paths: + - src + + # The level 9 is the highest level + level: 5 + + ignoreErrors: + - '#Unsafe usage of new static\(\)#' + - '#Right side of \&\& is always false#' + - '#Call to static method#' + - '#Call to an undefined method#' + + excludePaths: + + checkFunctionNameCase: true + checkInternalClassCaseSensitivity: true + checkAlwaysTrueInstanceof: true + reportMaybesInMethodSignatures: true + reportStaticMethodSignatures: true + checkUninitializedProperties: true + checkDynamicProperties: true + reportAlwaysTrueInLastCondition: true + reportWrongPhpDocTypeInVarTag: true + checkMissingCallableSignature: true diff --git a/src/Console/Commands/ModelValidateCommand.php b/src/Console/Commands/ModelValidateCommand.php index 075bf12..ab6dd0c 100644 --- a/src/Console/Commands/ModelValidateCommand.php +++ b/src/Console/Commands/ModelValidateCommand.php @@ -46,7 +46,7 @@ public function handle() $bar->setMessage($model); if (! $this->option('ignore-configuration')) { - $this->checkConfiguration(new $model); + $this->checkConfiguration(new $model()); } foreach ($model::all() as $item) { @@ -126,7 +126,7 @@ protected function validate(\Illuminate\Database\Eloquent\Model $model): void try { $model->validate(); } catch (\Illuminate\Validation\ValidationException $e) { - dump( $e->validator->errors()->all(), $e->validator->getData() ); + dump($e->validator->errors()->all(), $e->validator->getData()); throw $e; } } @@ -163,18 +163,18 @@ protected function checkConfiguration(\Illuminate\Database\Eloquent\Model $model if ($name == 'unique') { - $flat = []; - foreach ($value as &$batch) { - sort($batch); - $flat = array_merge($flat, $batch); - } - unset($batch); + $flat = []; + foreach ($value as &$batch) { + sort($batch); + $flat = array_merge($flat, $batch); + } + unset($batch); - if (count($value) != count(array_unique($value, SORT_REGULAR))) { - throw new \LogicException("[$modelName] Duplicates for \"$name\""); - } + if (count($value) != count(array_unique($value, SORT_REGULAR))) { + throw new \LogicException("[$modelName] Duplicates for \"$name\""); + } - $collection = array_merge($collection, $flat); + $collection = array_merge($collection, $flat); } elseif ($name == 'attribute_names') { diff --git a/src/CrudService.php b/src/CrudService.php index c326935..6a2ce17 100644 --- a/src/CrudService.php +++ b/src/CrudService.php @@ -54,7 +54,7 @@ public function execute( } $counters['created'] += $model->create($query)->exists ? 1 : 0; - } elseif (! empty($query['_delete'])) { + } elseif (! empty($query['_delete'])) { // DELETE $curr = null; if (is_numeric($id)) { diff --git a/src/ModelTrait.php b/src/ModelTrait.php index 6bafa99..6ba5c99 100644 --- a/src/ModelTrait.php +++ b/src/ModelTrait.php @@ -463,7 +463,7 @@ public function getAttributeNames(): array */ public function getComputed() { - return ( $this->computed ?? null ); + return ($this->computed ?? null); } /** @@ -473,7 +473,7 @@ public function getComputed() */ public function getUnchangeable() { - return ( $this->unchangeable ?? null ); + return ($this->unchangeable ?? null); } /** @@ -483,7 +483,7 @@ public function getUnchangeable() */ public function getUnique() { - return ( $this->unique ?? null ); + return ($this->unique ?? null); } /** @@ -493,7 +493,7 @@ public function getUnique() */ public function getJsonNested() { - return ( $this->jsonNested ?? null ); + return ($this->jsonNested ?? null); } /** @@ -539,7 +539,7 @@ protected function handleUnique( $newAttributes = $this->attributes; foreach ($uniques as $unique) { - $builder = new $this; + $builder = new $this(); if (! $this->isDirty($unique) && $this->exists) { continue; diff --git a/src/Providers/EloquentValidationServiceProvider.php b/src/Providers/EloquentValidationServiceProvider.php index 60d597d..8bc5f77 100644 --- a/src/Providers/EloquentValidationServiceProvider.php +++ b/src/Providers/EloquentValidationServiceProvider.php @@ -51,7 +51,7 @@ public function boot() private function addScalarRule(): void { \Validator::extend('scalar', function ($attribute, $value, $parameters, $validator) { - return ( is_scalar($value) || is_null($value) || (is_object($value) && method_exists($value, '__toString')) ); + return (is_scalar($value) || is_null($value) || (is_object($value) && method_exists($value, '__toString'))); }); \Validator::replacer('scalar', function ($message, $attribute, $rule, $parameters, $validator) { @@ -120,7 +120,7 @@ private function addArrayKeysRule(): void } $parameters = array_unique($parameters); - return ( is_array($value) && ! array_diff_key($value, array_combine($parameters, $parameters)) ); + return (is_array($value) && ! array_diff_key($value, array_combine($parameters, $parameters))); }); \Validator::replacer('array_keys', function ($message, $attribute, $rule, $parameters, $validator) { @@ -137,7 +137,7 @@ private function addArrayKeysRule(): void private function addArrayKeysOnlyRule(): void { \Validator::extend('array_keys_only', function ($attribute, $value, $parameters, $validator) { - return ( is_array($value) && ! array_diff_key($value, array_combine($parameters, $parameters)) ); + return (is_array($value) && ! array_diff_key($value, array_combine($parameters, $parameters))); }); \Validator::replacer('array_keys_only', function ($message, $attribute, $rule, $parameters, $validator) { diff --git a/src/ValidatorHelper.php b/src/ValidatorHelper.php index 6c87a9f..230a4fc 100644 --- a/src/ValidatorHelper.php +++ b/src/ValidatorHelper.php @@ -88,7 +88,7 @@ public function mutateArray( } } - if ($parentKeys && ( (is_string($item) && trim($item) === '') || $item === [] )) { + if ($parentKeys && ((is_string($item) && trim($item) === '') || $item === [])) { foreach ((array) $nullable as $nullableKey) { if (! $this->isMatching($nullableKey, $path)) { continue; @@ -130,7 +130,7 @@ public function mutateArray( continue; } - if (in_array($cast, ['bool']) && ! in_array($item, [true, false, 0, 1, '0', '1', '', null], true)) { + if (in_array($cast, ['bool']) && ! in_array($item, [true, false, 0, 1, '0', '1', '', null], true)) { continue; }