Error Identifier: method.childAssertType

← Back to method.*

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 Foo
{

	/** @phpstan-assert int $x */
	public function assertInt(mixed $x): void
	{
	}

}

class Bar extends Foo
{

	/** @phpstan-assert string $x */
	public function assertInt(mixed $x): void
	{
	}

}

Why is it reported? #

Code that calls Foo::assertInt() relies on its @phpstan-assert tags. After the call, $x is treated as int. When the object is actually a Bar, the overriding method must keep that promise. Otherwise the narrowed type is wrong and code that depends on it may crash.

An overriding method has to narrow each expression at least as much as the parent method does in every situation:

  • after the call, for @phpstan-assert
  • when it returns true, for @phpstan-assert-if-true
  • when it returns false, for @phpstan-assert-if-false

Conditional tags also narrow the opposite branch to the negated type. A child method therefore cannot change the type of a @phpstan-assert-if-true tag in either direction, unless it never returns false.

Assert tags are inherited only when the overriding method has none of its own. Once a child method adds any assert tag, it must repeat the parent’s tags it still wants to keep. Otherwise it does not narrow the expression at all, and PHPStan reports this:

Method Bar::both() does not narrow $y when it returns true, but overridden method Foo::both() narrows it to string.

Asserting a narrower type than the parent (for example positive-int with the unconditional @phpstan-assert instead of int), or adding assertions about other parameters, is allowed.

This rule is currently available in Bleeding Edge.

How to fix it #

Make the child’s assertion compatible with the parent’s:

 class Bar extends Foo
 {

-	/** @phpstan-assert string $x */
+	/** @phpstan-assert positive-int $x */
 	public function assertInt(mixed $x): void
 	{
 	}

 }

When the child method adds its own tags, repeat the parent’s tags as well:

 class Bar extends Foo
 {

 	/**
 	 * @phpstan-assert-if-true int $x
+	 * @phpstan-assert-if-true string $y
 	 */
 	public function both(mixed $x, mixed $y): bool
 	{
 		return is_int($x) && is_string($y);
 	}

 }

If the child method should keep the parent’s behaviour, remove its tags so the parent’s tags are inherited.

How to ignore this error #

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

// @phpstan-ignore method.childAssertType
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.childAssertType

Rules that report this error #

  • PHPStan\Rules\Methods\MethodSignatureRule [1] [2]
  • PHPStan\Rules\Methods\OverridingMethodRule [1] [2]
  • PHPStan\Rules\Properties\OverridingPropertyRule [1] [2]
Theme
© 2026 PHPStan s.r.o.