Always-used class constants

PHPStan is able to detect unused private class constants. There might be some cases where PHPStan thinks a class constant is unused, but the code might actually be correct. For example custom enum implementations like Consistence enums might take advantage of reflection to write and read private constants which static analysis cannot understand, but fortunately you can write a custom extension to make PHPStan understand what’s going on and avoid false-positives.

The implementation is all about applying the core concepts like reflection so check out that guide first and then continue here.

This is the interface your extension needs to implement:

namespace PHPStan\Rules\Constants;

use PHPStan\Reflection\ConstantReflection;

interface AlwaysUsedClassConstantsExtension

	public function isAlwaysUsed(ConstantReflection $constant): bool;


The implementation needs to be registered in your configuration file:

		class: MyApp\PHPStan\ConstantsExtension
			- phpstan.constants.alwaysUsedClassConstantsExtension

Edit this page on GitHub

© 2016–2024 Ondřej Mirtes