In PHP project development, we often use define or const to define constants. As the project continues to expand, the number of constants will also increase dramatically. Over time, some constants may be forgotten and no longer referenced by any code. These unused constants increase maintenance costs and reduce code readability and maintainability. In order to improve the code quality, we can use the get_defined_constants function combined with static analysis methods to identify and clean unused constants.
get_defined_constants is one of the built-in functions of PHP, which is used to return all defined constants in the current script (including user-defined constants and PHP internal constants). This function can accept a Boolean parameter, and when the parameter is true , it returns a multidimensional array grouped by classification; otherwise it returns a flat array.
Example:
print_r(get_defined_constants(true));
get_defined_constants does not directly tell us whether a constant is used. But we can combine the following two steps to achieve this goal indirectly:
Gets all user-defined constant names.
Iterate through the project code and check whether these constants are referenced.
Mark unreferenced constants as "unused".
Here is a simple implementation idea, assuming you are maintaining a small and medium-sized project:
$allConstants = get_defined_constants(true);
$userConstants = isset($allConstants['user']) ? $allConstants['user'] : [];
$projectDir = __DIR__ . '/src'; // Project source code directory
$usedConstants = [];
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($projectDir));
foreach ($files as $file) {
if ($file->isFile() && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$content = file_get_contents($file->getPathname());
foreach ($userConstants as $name => $value) {
// Simple string search,You can also use regular matching accuracy
if (strpos($content, $name) !== false) {
$usedConstants[$name] = true;
}
}
}
}
$unusedConstants = array_diff_key($userConstants, $usedConstants);
echo "List of unused constants:\n";
print_r(array_keys($unusedConstants));
For large projects, it is recommended to use regular expressions to avoid misjudgment, such as interference when variable names are similar to constant names.
It is recommended to cooperate with code static analysis tools (such as PHPStan, Psalm) for multiple verifications.
Before removing constants, it is recommended to test repeatedly in the development environment to avoid accidentally deleting constants that are being referenced dynamically.
Suppose your project defines the following constants:
define('API_URL', 'https://m66.net/api/');
define('DEBUG_MODE', true);
define('UNUSED_CONST', 12345);
After script analysis, it is found that UNUSED_CONST is not referenced in the project, so you can consider removing it from the code to improve the neatness of the project code.
Although PHP does not have built-in functionality to directly find unused constants, we can still achieve this through the get_defined_constants function and simple code scanning logic. Rational management of constants helps to improve code readability and maintainability, and also reflects the maturity and standardization of a project.