Menu

← Back to classConstant.*

Error Identifier: classConstant.unused

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
{
	private const MAX_RETRIES = 3;

	public function doSomething(): void
	{
	}
}

Why is it reported? #

A private class constant is declared but never read anywhere in the class. Since private constants cannot be accessed from outside the class, this constant is dead code. It may be leftover from a refactoring or a constant that was defined but never actually used.

In the example above, Foo::MAX_RETRIES is declared as private but is never used within the Foo class.

Learn more: Always-Used Class Constants

How to fix it #

Remove the unused constant:

 <?php declare(strict_types = 1);
 
 class Foo
 {
-	private const MAX_RETRIES = 3;
-
 	public function doSomething(): void
 	{
 	}
 }

Or use the constant in the class:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	private const MAX_RETRIES = 3;

 	public function doSomething(): void
 	{
+		for ($i = 0; $i < self::MAX_RETRIES; $i++) {
+			// retry logic
+		}
 	}
 }

If the constant is used through reflection or other dynamic means not visible to static analysis, implement the Always-Used Class Constants extension.

How to ignore this error #

You can use the identifier classConstant.unused to ignore this error using a comment:

// @phpstan-ignore classConstant.unused
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: classConstant.unused

Rules that report this error #

  • PHPStan\Rules\DeadCode\UnusedPrivateConstantRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.