Menu

← Back to possiblyImpure.*

Error Identifier: possiblyImpure.new

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 Connection
{
	public function __construct(private string $dsn)
	{
	}
}

class Factory
{
	/**
	 * @phpstan-pure
	 */
	public function create(): Connection
	{
		return new Connection('sqlite::memory:'); // ERROR: Possibly impure instantiation of class Connection in pure method Factory::create().
	}
}

Why is it reported? #

A function or method marked as @phpstan-pure instantiates a class whose constructor’s purity is unknown. The constructor might have side effects, which would make the calling function impure. PHPStan cannot guarantee that the instantiation is pure, so it reports it as possibly impure.

A pure function must not have any side effects and must always return the same result for the same inputs.

How to fix it #

Mark the constructor as @phpstan-pure so PHPStan knows the instantiation is safe:

 <?php declare(strict_types = 1);
 
 class Connection
 {
+	/** @phpstan-pure */
 	public function __construct(private string $dsn)
 	{
 	}
 }

Or remove the @phpstan-pure annotation from the calling method if it does not need to be pure:

 <?php declare(strict_types = 1);
 
 class Factory
 {
-	/**
-	 * @phpstan-pure
-	 */
 	public function create(): Connection
 	{
 		return new Connection('sqlite::memory:');
 	}
 }

How to ignore this error #

You can use the identifier possiblyImpure.new to ignore this error using a comment:

// @phpstan-ignore possiblyImpure.new
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: possiblyImpure.new

Rules that report this error #

  • PHPStan\Rules\Pure\PureFunctionRule [1]
  • PHPStan\Rules\Pure\PureMethodRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.