Menu

← Back to method.*

Error Identifier: method.resultDiscarded

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 list<int> */
	private array $items;

	#[\NoDiscard]
	public function filter(callable $callback): self
	{
		$new = clone $this;
		$new->items = array_filter($this->items, $callback);
		return $new;
	}
}

$collection = new Collection();
$collection->filter(fn (int $v): bool => $v > 0);

Why is it reported? #

The method is called on a separate line and its return value is discarded. The method is marked with #[\NoDiscard], indicating that its return value must be used. This typically applies to immutable or pure methods where calling the method without capturing the return value means the call has no meaningful effect. The original object remains unchanged.

How to fix it #

Capture the return value of the method call:

 <?php declare(strict_types = 1);
 
 $collection = new Collection();
-$collection->filter(fn (int $v): bool => $v > 0);
+$filtered = $collection->filter(fn (int $v): bool => $v > 0);

If the return value is intentionally not needed, use a (void) cast to signal the intent:

 <?php declare(strict_types = 1);
 
 $collection = new Collection();
-$collection->filter(fn (int $v): bool => $v > 0);
+(void) $collection->filter(fn (int $v): bool => $v > 0);

Non-ignorable error #

This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.

Rules that report this error #

  • PHPStan\Rules\Methods\CallToMethodStatementWithNoDiscardRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.