Skip to content

addRule() $validator PHPDoc rejects the documented "Custom rules and conditions" callable usage #350

@vsillik

Description

@vsillik

Version: 3.2.9

Bug Description

The @param PHPDoc added to BaseControl::addRule() and Rules::addRule() in v3.2.9 (callable(Control): bool)|string is too narrow. It rejects the validator signature shown in the official documentation, and it does not match the package's own bundled validators in Nette\Forms\Validator. Consumer code that follows the docs verbatim now produces PHPStan errors on level 0+.

Steps To Reproduce

Copying the official example from the Custom rules and conditions docs verbatim into a project with PHPStan:

<?php declare(strict_types = 1);

namespace App;

use Nette\Forms\Controls\BaseControl;
use Nette\Forms\Form;

final class Repro
{
    public static function validateDivisibility(BaseControl $input, int $arg): bool
    {
        return $input->getValue() % $arg === 0;
    }

    public function build(): void
    {
        $form = new Form;
        $form->addInteger('num')
            ->addRule(
                [self::class, 'validateDivisibility'],
                'The value must be a multiple of %d',
                8,
            );
    }
}

PHPStan output (nette/forms 3.2.9, phpstan 2.1.56):

 ------ ----------------------------------------------------------------------
  Line   src/Repro.php
 ------ ----------------------------------------------------------------------
  22     Parameter #1 $validator of method
         Nette\Forms\Controls\TextInput::addRule() expects
         (callable(Nette\Forms\Control): bool)|string,
         array{'App\Repro', 'validateDivisibility'} given.
         🪪  argument.type
         💡  Type #1 from the union: Parameter #2 $arg of passed callable is
         required but accepting callable does not have that parameter. It will
         be called without it.
 ------ ----------------------------------------------------------------------

Expected Behavior

The official documentation example should pass static analysis.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions