Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,20 @@ public function __construct(
?TemplateTypeMap $resolvedTemplateTypeMap = null,
private array $templateTags = [],
?TrinaryLogic $isPure = null,
?bool $isCommonCallable = null,
)
{
$this->parameters = $parameters ?? [];
$this->returnType = $returnType ?? new MixedType();
$this->isCommonCallable = $parameters === null && $returnType === null;
$this->templateTypeMap = $templateTypeMap ?? TemplateTypeMap::createEmpty();
$this->resolvedTemplateTypeMap = $resolvedTemplateTypeMap ?? TemplateTypeMap::createEmpty();
$this->isPure = $isPure ?? TrinaryLogic::createMaybe();

if ($isCommonCallable !== null) {
$this->isCommonCallable = $isCommonCallable;
} else {
$this->isCommonCallable = $parameters === null && $returnType === null;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function getArray(): Type

if ($this->degradeClosures) {
$itemTypes = [];
$itemTypes[] = new CallableType();
$itemTypes[] = new CallableType(isCommonCallable: false);
foreach ($this->valueTypes as $valueType) {
if ($valueType instanceof ClosureType) {
continue;
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ private static function findTestFiles(): iterable
yield __DIR__ . '/../Rules/Methods/data/bug-4801.php';
yield __DIR__ . '/../Rules/Arrays/data/narrow-superglobal.php';
yield __DIR__ . '/../Rules/Methods/data/bug-12927.php';
yield __DIR__ . '/../Rules/Properties/data/bug-14012.php';
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/degrade-closures-return-types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DegradeClosures;

use function PHPStan\Testing\assertType;

$arr = [];
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function ():int {return 1;};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function () {};
$arr[] = static function ():bool {return true;};
assertType('array{Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): 1, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): true}', $arr);

$arr[] = static function () {};
assertType('non-empty-list<callable(): mixed>&oversized-array', $arr);
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ public function testPromotedProperties(): void
$this->analyse([__DIR__ . '/data/promoted-properties-missing-typehint.php'], []);
}

public function testBug14012(): void
{
$this->analyse([__DIR__ . '/data/bug-14012.php'], []);
}

}
43 changes: 43 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-14012.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Bug14012;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use function PHPStan\Testing\assertType;

final class ExpectationMethodResolver
{
/**
* @var array{
* hasMethod: \Closure(Scope, Node\Arg, Node\Arg): Node\Expr,
* hasProperty: \Closure(Scope, Node\Arg, Node\Arg): Node\Expr,
* isArray: \Closure(Scope, Node\Arg): Node\Expr,
* isBool: \Closure(Scope, Node\Arg): Node\Expr,
* isCallable: \Closure(Scope, Node\Arg): Node\Expr,
* isCountable: \Closure(Scope, Node\Arg): Node\Expr,
* isFalse: \Closure(Scope, Node\Arg): Node\Expr,
* isFloat: \Closure(Scope, Node\Arg): Node\Expr,
* isInstanceOf: \Closure(Scope, Node\Arg, Node\Arg): Node\Expr,
* isInt: \Closure(Scope, Node\Arg): Node\Expr,
* isIterable: \Closure(Scope, Node\Arg): Node\Expr,
* isList: \Closure(Scope, Node\Arg): Node\Expr,
* isMap: \Closure(Scope, Node\Arg): Node\Expr,
* isNaturalInt: \Closure(Scope, Node\Arg): Node\Expr,
* isNegativeInt: \Closure(Scope, Node\Arg): Node\Expr,
* isNonEmptyString: \Closure(Scope, Node\Arg): Node\Expr,
* isNull: \Closure(Scope, Node\Arg): Node\Expr,
* isNumeric: \Closure(Scope, Node\Arg): Node\Expr,
* isObject: \Closure(Scope, Node\Arg): Node\Expr,
* isPositiveInt: \Closure(Scope, Node\Arg): Node\Expr,
* isResource: \Closure(Scope, Node\Arg): Node\Expr,
* isSameAs: \Closure(Scope, Node\Arg, Node\Arg): Node\Expr,
* isScalar: \Closure(Scope, Node\Arg): Node\Expr,
* isString: \Closure(Scope, Node\Arg): Node\Expr,
* isTrue: \Closure(Scope, Node\Arg): Node\Expr,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also assert what's the type of the property here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* }
*/
public static array $resolvers = [];
}

assertType("non-empty-array<'hasMethod'|'hasProperty'|'isArray'|'isBool'|'isCallable'|'isCountable'|'isFalse'|'isFloat'|'isInstanceOf'|'isInt'|'isIterable'|'isList'|'isMap'|'isNaturalInt'|'isNegativeInt'|'isNonEmptyString'|'isNull'|'isNumeric'|'isObject'|'isPositiveInt'|'isResource'|'isSameAs'|'isScalar'|'isString'|'isTrue', callable(): mixed>&oversized-array", ExpectationMethodResolver::$resolvers);
Loading