Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Jan 26, 2026

closes phpstan/phpstan#14012


Updated Description

after this PR we still have errors when callables have been degraded, but as we degrade less often/later this should be less of a problem.

I also checked the reproducers of #4684 so they are equal fast

@staabm
Copy link
Contributor Author

staabm commented Jan 26, 2026

We could raise the threshold to degenerate later.. need to test this in the origin slow snippets though

@ondrejmirtes
Copy link
Member

An idea - we could degrade it only for literal array in code, but we wouldn't need to degrade it for array shapes? Meaning ConstantArrayTypeBuilder in TypeNodeResolver would be created in a different mode.

Also maybe the limit for degrading closures could be higher?

@staabm
Copy link
Contributor Author

staabm commented Jan 26, 2026

An idea - we could degrade it only for literal array in code, but we wouldn't need to degrade it for array shapes? Meaning ConstantArrayTypeBuilder in TypeNodeResolver would be created in a different mode.

I don't see how this is possible, because the worst case scenario is built up over one assign per line
see https://github.com/GrzegorzDrozd/phpstan-performance-issue-reproduction/blob/master/src/index_bad.php

@staabm staabm marked this pull request as draft January 26, 2026 17:32
@staabm
Copy link
Contributor Author

staabm commented Jan 26, 2026

I had to discard the previous fix because it discarded all perf benefits we got previosuly with #4684

@staabm staabm marked this pull request as ready for review January 26, 2026 18:04
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

This would consequently cause an argument.type error on consumers as it's now mixed instead of Node\Expr.

Copy link
Contributor Author

@staabm staabm Jan 27, 2026

Choose a reason for hiding this comment

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

I think this problem is inevitable (producing more precise types will slow down the analysis).
I raised the limit to 32 now, so it is less likely to happen

@staabm staabm marked this pull request as draft January 27, 2026 07:07
@staabm
Copy link
Contributor Author

staabm commented Jan 27, 2026

sorry for flooding this PR with commits/pushes

@staabm staabm marked this pull request as ready for review January 27, 2026 10:08
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@paulbalandan
Copy link
Contributor

I don't see how this is possible, because the worst case scenario is built up over one assign per line see https://github.com/GrzegorzDrozd/phpstan-performance-issue-reproduction/blob/master/src/index_bad.php

How about this one:

diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php
index 4df978002..979977508 100644
--- a/src/PhpDoc/TypeNodeResolver.php
+++ b/src/PhpDoc/TypeNodeResolver.php
@@ -1050,6 +1050,13 @@ final class TypeNodeResolver
 			$builder->degradeToGeneralArray(true);
 		}
 
+		foreach ($typeNode->items as $itemNode) {
+			if ($itemNode->valueType instanceof CallableTypeNode) {
+				$builder->degradeClosures(false); // explicit opt-out of closure degradation
+				break;
+			}
+		}
+
 		foreach ($typeNode->items as $itemNode) {
 			$offsetType = $this->resolveArrayShapeOffsetType($itemNode, $nameScope);
 			$builder->setOffsetValueType($offsetType, $this->resolve($itemNode->valueType, $nameScope), $itemNode->optional);
diff --git a/src/Type/Constant/ConstantArrayTypeBuilder.php b/src/Type/Constant/ConstantArrayTypeBuilder.php
index 9396fec39..3d7005e93 100644
--- a/src/Type/Constant/ConstantArrayTypeBuilder.php
+++ b/src/Type/Constant/ConstantArrayTypeBuilder.php
@@ -35,7 +35,7 @@ final class ConstantArrayTypeBuilder
 
 	private bool $degradeToGeneralArray = false;
 
-	private bool $degradeClosures = false;
+	private ?bool $degradeClosures = false;
 
 	private bool $oversized = false;
 
@@ -94,7 +94,7 @@ final class ConstantArrayTypeBuilder
 					$numClosures++;
 				}
 
-				if ($numClosures >= self::CLOSURES_COUNT_LIMIT) {
+				if ($numClosures >= self::CLOSURES_COUNT_LIMIT && $this->degradeClosures !== false) {
 					$this->degradeClosures = true;
 					$this->degradeToGeneralArray = true;
 					$this->oversized = true;
@@ -300,6 +300,15 @@ final class ConstantArrayTypeBuilder
 		$this->oversized = $this->oversized || $oversized;
 	}
 
+	/**
+	 * @param bool|null $degrade Use `null` to auto-detect based on closures count;
+	 *                           Use boolean to explicitly enable/disable closure degradation.
+	 */
+	public function degradeClosures(?bool $degrade = false): void
+	{
+		$this->degradeClosures = $degrade;
+	}
+
 	public function getArray(): Type
 	{
 		$keyTypesCount = count($this->keyTypes);

On my laptop, it is a steady ~3.02 seconds before/after this patch.

@staabm
Copy link
Contributor Author

staabm commented Jan 27, 2026

On my laptop, it is a steady ~3.02 seconds before/after this patch.

time vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php --filter testBug13933 takes 6 seconds after this change and 1.8 seconds before.

edit: found the typo in your suggestion :)

staabm and others added 2 commits January 27, 2026 15:54
Co-Authored-By: John Paul E. Balandan, CPA <51850998+paulbalandan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Degraded closures in constant arrays are now reported by checkMissingCallableSignature: true

4 participants