After I set in phpstan.neon parameter :
level: 7
I got errors :
30 Method App\Rules\ItemModelRules::getValidationRulesArray() has parameter $skipFieldsArray with no value type specified in iterable type array.
š” See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
30 Method App\Rules\ItemModelRules::getValidationRulesArray() return type has no value type specified in iterable type array.
š” See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
61 Method App\Rules\ItemModelRules::getValidationMessagesArray() return type has no value type specified in iterable type array.
š” See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
On a file with code:
<?php
namespace App\Rules;
use App\Enums\ItemPriority;
use App\Enums\ItemStatus;
use App\Models\Item;
use App\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Validation\Rule;
/*
Class-container for rules/messages of items validation
*
*/
class ItemModelRules
{
/*
return validation rules array for item model
*
@param int - $itemId - ID of item
*
* which fields must be skipped from array
@param array<string> $skipFieldsArray
*
* @returns array<string, string>
*
*/
public static function getValidationRulesArray(int $itemId = null, array $skipFieldsArray = []): array // THAT is where 1st and 2nd errors are pointing
{
$itemTable = (new Item)->getTable();
$validationRulesArray = [
'parent_id' => 'nullable|exists:' . $itemTable . ',id',
...
'description' => 'string|required',
];
foreach ($skipFieldsArray as $field) {
if (!empty($validationRulesArray[$field])) {
$validationRulesArray = Arr::except($validationRulesArray, $field);
}
}
return $validationRulesArray;
}
/*
* Returns custom Messages for validation errors
*
* @returns array<string, string>
*/
public static function getValidationMessagesArray(): array // THAT is where 3rd errors are pointing
{
return [
'parent_id.invalid' => 'Parent Id is invalid. Must be valid reference to items table',
'user_id.invalid' => 'User Id is invalid. Must be valid reference to users table',
];
}
}
I read refereced help and seems I used proper syntax in methods definition... How that can be fixed ?
"laravel/framework": "^10.34.2",
"nunomaduro/larastan": "^2.6.4",
FIXED CODE :
I remade file App\Rules\ItemModelRules like :
class ItemModelRules
{
/*
* @param int - $itemId - ID of item
*
* which fields must be skipped from array
* @param array<int, string> $skipFieldsArray
*
* @return array<string, string>
*
*/
public static function getValidationRulesArray(int $itemId = null, array $skipFieldsArray = []): array
{
$itemTable = (new Item)->getTable();
$validationRulesArray = [
'parent_id' => 'nullable|exists:' . $itemTable . ',id',
...
'description' => 'string|required',
];
foreach ($skipFieldsArray as $field) {
if ( ! empty($validationRulesArray[$field])) {
$validationRulesArray = Arr::except($validationRulesArray, $field);
}
}
return $validationRulesArray;
}
/*
* Returns custom Messages for validation errors
*
* @return array<string, string>
*/
public static function getValidationMessagesArray(): array
{
return [
'parent_id.invalid' => 'Parent Id is invalid. Must be valid reference to items table',
'user_id.invalid' => 'User Id is invalid. Must be valid reference to users table',
];
}
}
But anyway I got similar errors pointing at declaration of my methods...
Line ItemModelRules.php
------ ------------------------------------------------------------------------------------------------------------------------------------------------
29 Method App\Rules\ItemModelRules::getValidationRulesArray() has parameter $skipFieldsArray with no value type specified in iterable type array.
š” See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
29 Method App\Rules\ItemModelRules::getValidationRulesArray() return type has no value type specified in iterable type array.
š” See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
60 Method App\Rules\ItemModelRules::getValidationMessagesArray() return type has no value type specified in iterable type array.
š” See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
Did I miss something ?
Thanks in advance!
The return
issue is that you are using @returns
but not @return
. @return
is only valid: https://phpstan.org/writing-php-code/phpdoc-types.
The other error, if I am not confused, is that you did not specify a key
and value
, but just value
.
So, your final PHPDoc should be like this:
/**
* @param int $itemId ID of item
* @param array<int, string> $skipFieldsArray which fields must be skipped from array
*
* @return array<string, string>
*/
/**
* Returns custom Messages for validation errors
*
* @return array<string, string>
*/