skip to Main Content

After I set in phpstan.neon parameter :

level: 7

I got errors :

30     Method AppRulesItemModelRules::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 AppRulesItemModelRules::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 AppRulesItemModelRules::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 AppRules;

use AppEnumsItemPriority;
use AppEnumsItemStatus;
use AppModelsItem;
use AppModelsUser;
use IlluminateSupportArr;
use IlluminateValidationRule;

/*
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 AppRulesItemModelRules 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 AppRulesItemModelRules::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 AppRulesItemModelRules::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 AppRulesItemModelRules::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!

2

Answers


  1. 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>
     */
    
    Login or Signup to reply.
  2. The problem could also be that you start your doc blocks with /* instead of /**
    If i recall correctly only with a double asterisk it is considerd a valid PHPDoc

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search