Skip to content

infer closure type from being passed into function's return type with callable signature #11317

Closed
phpstan/phpstan-src
#6604

Description

@heiglandreas

Bug report

I have a closure that passes a closure-parameter to an external method.

<?php

class A {
	/**
	 * @param array{string, int, bool} $array
	 */
	public function do(array $array): B
	{
		return new B;
	}
}

class B {}

class C {
	public function __construct(private A $a) {}

	/**
	 * @return Callable(array{string, int, bool}): B
	 */
	public function getCallable(): Callable
	{
		/**
		 * @param array{string, int, bool}
		 */
		return function (array $array): B 
		{
			return $this->a->do($array);
		};
	}
}

The closure return type has been declared with the correct parameter-type and inside the function the parameteres of the returned closure are also declared correctly.

Yet PHPStan complaints that the function called within the closure "expects array{string, int, bool}, array given".

I am sure I am missing something somewhere as I doubt that I am the only one stumbling over this.

Code snippet that reproduces the problem

https://phpstan.org/r/799ddd60-8c84-485c-bb0b-46b700bf2635

Expected output

No errors

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. heiglandreas commented on Jul 10, 2024

    @heiglandreas
    Author

    I figured I could "fix" this by adding a /** @var array{string, int, bool} $array */ right before passing the variable inside the closure. But that seems to be a hacky patch for something that should be solved differently as all the information should already be available.

    See https://phpstan.org/r/c6ef9c9b-8444-4b45-a3de-5493de78738a

  2. ondrejmirtes commented on Jul 10, 2024

    @ondrejmirtes
    Member

    Hello Andreas,
    PHPStan currently doesn't read PHPDocs above closures so this PHPDoc is not used at all:

    		/**
    		 * @param array{string, int, bool}
    		 */
    		return function (array $array): B 
    		{
    			return $this->a->do($array);
    		};

    And there's a feature request for this already: #3770

    The reason why we didn't add this yet is because PHPStan is pretty smart about inferring closure parameter and return type automatically based on where the closure is passed to. Some examples:

    User::doesntHave('accounts', 'and', function (Builder $query) {
        // Builder<Account> because it figures that out based on `User` and `accounts`
        \PHPStan\dumpType($query);
    });

    So in my experience this is much better than requiring the user to write @param and @return above their closures.

    I'm typing this all out because I have a similar suggestion here. When looking at the code sample, PHPStan could figure out what's going to be passed into the closure based on the @return above the method thanks to the fact the closure is directly in return statement:

    	/**
    	 * @return callable(array{string, int, bool}): B
    	 */
    	public function getCallable(): callable
    	{
    		return function (array $array): B 
    		{
    			\PHPStan\dumpType($array); // array{string, int, bool}
    			return $this->a->do($array);
    		};
    	}
    }

    WDYT?

  3. heiglandreas commented on Jul 11, 2024

    @heiglandreas
    Author

    Hey @ondrejmirtes.

    Adding the PHPDoc above the closure was merely a try to see whether it fixes the issue. I would be totally happy for PHPStan to infer the type from the @return annotation of the function returning the callable as you have sketched out above!

    But I did get you right that that is an idea how to solve that and not yet working code, didn't I? 'Cause currently the PHPSTan\dumpType() merely prints array...

  4. ondrejmirtes commented on Jul 11, 2024

    @ondrejmirtes
    Member

    Yes, it's just a suggestion how PHPStan could be improved 😊

  5. changed the title [-]Can't declare parameter expectations within closures[/-] [+]infer closure type from being passed into function's return type with callable signature[/+] on Jul 11, 2024
  6. phpstan-bot commented on Sep 4, 2024

    @phpstan-bot
    Contributor

    @heiglandreas After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -PHP 8.0 – 8.3 (1 error)
    +PHP 8.0 – 8.3 (2 errors)
     ==========
     
    +26: PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40
     28: Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.
     
    -PHP 7.2 – 7.4 (2 errors)
    +PHP 7.2 – 7.4 (3 errors)
     ==========
     
     16: Promoted properties are supported only on PHP 8.0 and later.
    +26: PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40
     28: Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.
    Full report

    PHP 8.0 – 8.3 (2 errors)

    Line Error
    26 PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40
    28 Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.

    PHP 7.2 – 7.4 (3 errors)

    Line Error
    16 Promoted properties are supported only on PHP 8.0 and later.
    26 PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40
    28 Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.
  7. phpstan-bot commented on Sep 7, 2024

    @phpstan-bot
    Contributor

    @heiglandreas After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -PHP 8.0 – 8.3 (1 error)
    +PHP 8.0 – 8.3 (2 errors)
     ==========
     
    +24: PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40
     28: Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.
     
    -PHP 7.2 – 7.4 (2 errors)
    +PHP 7.2 – 7.4 (3 errors)
     ==========
     
     16: Promoted properties are supported only on PHP 8.0 and later.
    +24: PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40
     28: Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.
    Full report

    PHP 8.0 – 8.3 (2 errors)

    Line Error
    24 PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40
    28 Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.

    PHP 7.2 – 7.4 (3 errors)

    Line Error
    16 Promoted properties are supported only on PHP 8.0 and later.
    24 PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40
    28 Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.
  8. phpstan-bot commented on Sep 8, 2024

    @phpstan-bot
    Contributor

    @heiglandreas After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -PHP 8.0 – 8.3 (1 error)
    +PHP 8.0 – 8.3 (2 errors)
     ==========
     
    +24: PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40 on line 2
     28: Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.
     
    -PHP 7.2 – 7.4 (2 errors)
    +PHP 7.2 – 7.4 (3 errors)
     ==========
     
     16: Promoted properties are supported only on PHP 8.0 and later.
    +24: PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40 on line 2
     28: Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.
    Full report

    PHP 8.0 – 8.3 (2 errors)

    Line Error
    24 PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40 on line 2
    28 Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.

    PHP 7.2 – 7.4 (3 errors)

    Line Error
    16 Promoted properties are supported only on PHP 8.0 and later.
    24 PHPDoc tag @param has invalid value (array{string, int, bool}): Unexpected token "\n\t\t ", expected variable at offset 40 on line 2
    28 Parameter #1 $array of method A::do() expects array{string, int, bool}, array given.
  9. ondrejmirtes commented on Nov 3, 2024

    @ondrejmirtes
    Member

    It's very easy to implement. We need to update this place: https://github.com/phpstan/phpstan-src/blob/88d5b8cf0cb2da8873dc33222b5f8b4727f0756a/src/Analyser/NodeScopeResolver.php#L757-L761

    And call processClosureNode / processArrowFunctionNode with the method's return type as $passedToType.

  10. added a commit that references this issue on Feb 17, 2026
  11. phpstan-bot commented on Jul 7, 2026

    @phpstan-bot
    Contributor

    @ondrejmirtes After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:

    @@ @@
    +10: Call to function array_map() on a separate line has no effect.
     11: Dumped type: int
    Full report
    Line Error
    10 Call to function array_map() on a separate line has no effect.
    11 Dumped type: int
  12. github-actions commented on Oct 29, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  13. locked as resolved and limited conversation to collaborators on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions