Skip to content

ParameterTypeHint: False positive when using PHP 8.5 closure in constant expression #1852

@stlrnz

Description

@stlrnz

Consider the following code implementing the PHP 8.5 closure in constant expression feature in combination with an attribute.

<?php

declare(strict_types=1);

namespace Demo;

use Attribute;
use Closure;

#[Attribute]
class DemoAttribute
{
    public function __construct(Closure $run)
    {
    }
}

class DemoClass
{
    public function __construct(
        #[DemoAttribute(run: static function (bool $closureParam): bool {
            return $closureParam;
        })]
        bool $param
    )
    {
    }
}

The SlevomatCodingStandard.TypeHints.ParameterTypeHint falsely detects $closureParam as method parameter and reports a missing type hint.

 20 | ERROR | Method \Demo\DemoClass::__construct() does not have parameter
    |       | type hint nor @param annotation for its parameter $closureParam.
    |       | (SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint)

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