Menu

← Back to method.*

Error Identifier: method.alreadyNarrowedType

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

class Collection
{
	/** @var array<int, string> */
	private array $items = [];

	public function has(string $key): bool
	{
		return isset($this->items[$key]);
	}
}

function doFoo(Collection $collection): void
{
	if ($collection->has('foo')) {
		// ...
	} elseif ($collection->has('foo')) { // ERROR: will always evaluate to true
		// ...
	}
}

Why is it reported? #

A method call that acts as a type-checking or type-narrowing operation will always evaluate to true. PHPStan has already inferred enough type information to determine that the result of this method call is always true in this context.

This typically occurs when the same type check is performed redundantly, or when previous conditions already guarantee the result.

How to fix it #

Remove the redundant check, or fix the logic to test for a different condition:

 <?php declare(strict_types = 1);
 
 function doFoo(Collection $collection): void
 {
 	if ($collection->has('foo')) {
 		// ...
-	} elseif ($collection->has('foo')) {
+	} elseif ($collection->has('bar')) {
 		// ...
 	}
 }

If the check is no longer needed in an elseif/else chain, remove remaining cases below and the error will disappear.

How to ignore this error #

You can use the identifier method.alreadyNarrowedType to ignore this error using a comment:

// @phpstan-ignore method.alreadyNarrowedType
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: method.alreadyNarrowedType

Rules that report this error #

  • PHPStan\Rules\Comparison\ImpossibleCheckTypeMethodCallRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.