Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Sep 17, 2023
1 parent 479ad35 commit f2d7766
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 26 deletions.
11 changes: 11 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__);

$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR12' => true,
])
->setFinder($finder);
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"}
Expand Down
35 changes: 35 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0"?>
<!-- @see https://pear.php.net/manual/en/package.php.php-codesniffer.annotated-ruleset.php -->
<ruleset name="PHPCS Rules">

<description>PHPCS ruleset</description>

<file>src</file>

<!-- Show progress of the run -->
<arg value= "p"/>

<!-- Show sniff codes in all reports -->
<arg value= "s"/>

<!-- Our base rule: set to PSR12 -->
<rule ref="PSR12">
<exclude name="PSR12.Operators.OperatorSpacing.NoSpaceBefore"/>
<exclude name="PSR12.Operators.OperatorSpacing.NoSpaceAfter"/>
<exclude name="PSR12.Traits.UseDeclaration.MultipleImport"/>
<exclude name="Generic.Files.LineLength.TooLong"/>
<exclude name="PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose"/>
<exclude name="PSR12.Classes.OpeningBraceSpace.Found"/>
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen"/>
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose"/>
</rule>

<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>tests/</exclude-pattern>
</rule>

<rule ref="Internal.NoCodeFound">
<exclude-pattern>tests/</exclude-pattern>
</rule>

</ruleset>
26 changes: 26 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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
24 changes: 12 additions & 12 deletions src/Console/Commands/ModelValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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') {

Expand Down
2 changes: 1 addition & 1 deletion src/CrudService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
10 changes: 5 additions & 5 deletions src/ModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public function getAttributeNames(): array
*/
public function getComputed()
{
return ( $this->computed ?? null );
return ($this->computed ?? null);
}

/**
Expand All @@ -473,7 +473,7 @@ public function getComputed()
*/
public function getUnchangeable()
{
return ( $this->unchangeable ?? null );
return ($this->unchangeable ?? null);
}

/**
Expand All @@ -483,7 +483,7 @@ public function getUnchangeable()
*/
public function getUnique()
{
return ( $this->unique ?? null );
return ($this->unique ?? null);
}

/**
Expand All @@ -493,7 +493,7 @@ public function getUnique()
*/
public function getJsonNested()
{
return ( $this->jsonNested ?? null );
return ($this->jsonNested ?? null);
}

/**
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Providers/EloquentValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/ValidatorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit f2d7766

Please sign in to comment.