Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NikSpyratos's avatar

How can I determine which PHP extensions are actually required in my codebase?

I've searched just about everywhere, and asked in a few communities, but it seems nobody has tackled this problem yet.

As far as I can tell this seems to be a missing tool in the PHP landscape: analysing a codebase to determine which extensions are truly required, instead of just relying on what is in the composer.json of the project and dependencies.

composer check-platform-reqs validates that your PHP installation has the required extensions as defined by your composer file(s), not that your code actually uses each of those extensions.

Adding a bunch of extension names to my composer.json requirements based on vibes and reasonable guesses is fine, but I'd like to be accurate if I'm being that explicit in my project.

Anyone used something to solve this before?

0 likes
2 replies
LaryAI's avatar
Level 58

To determine which PHP extensions are actually required by your codebase, you can follow a manual approach or use a tool designed to analyze your code for extension dependencies. Unfortunately, there isn't a widely known tool that does this automatically and comprehensively, but I can suggest a couple of methods to help you identify the necessary extensions.

Manual Approach

  1. Review Your Code: Look through your codebase for any functions, classes, or features that are specific to certain PHP extensions. For example, if you see functions like curl_init(), you know the curl extension is required.

  2. Disable Extensions: Temporarily disable extensions in your PHP configuration, one at a time, and run your test suite or access different parts of your application to see if anything breaks. This can help identify which extensions are truly necessary.

  3. Check Documentation: For each library or framework you use, check their documentation to see if they specify required PHP extensions.

Semi-Automatic Tool

You can create a simple script that uses reflection and other built-in PHP functions to check for the usage of extension-specific functions and classes. Here’s a basic example of how you might write such a script:

<?php

$extensions = get_loaded_extensions();
$extensionFunctions = [];
foreach ($extensions as $extension) {
    $ext = new ReflectionExtension($extension);
    $extensionFunctions[$extension] = array_map(function ($func) {
        return $func->getName();
    }, $ext->getFunctions());
}

$directory = new RecursiveDirectoryIterator('./src');
$iterator = new RecursiveIteratorIterator($directory);
$phpFiles = new RegexIterator($iterator, '/\.php$/i', RecursiveRegexIterator::GET_MATCH);

$usedExtensions = [];

foreach ($phpFiles as $file) {
    $content = file_get_contents($file[0]);
    foreach ($extensionFunctions as $extension => $functions) {
        foreach ($functions as $function) {
            if (preg_match('/\b' . preg_quote($function, '/') . '\b/', $content)) {
                $usedExtensions[$extension] = true;
            }
        }
    }
}

echo "Extensions used in this codebase:\n";
foreach (array_keys($usedExtensions) as $ext) {
    echo "- $ext\n";
}

Explanation

  • Load Extensions: The script loads all currently enabled PHP extensions and retrieves the functions they provide.
  • Scan PHP Files: It scans all PHP files in the specified directory (./src in this case).
  • Check for Function Usage: For each file, it checks if any of the functions provided by the extensions are used.

Limitations

  • False Positives: The script might report false positives if a function name from an extension is used as a method name or in a different context.
  • Complex Features: Some extensions provide features that are not limited to specific function calls (like performance improvements or syntax changes), which this script won't detect.

Conclusion

While this script provides a basic level of analysis, for a comprehensive solution, you might need to enhance it or consider commercial static analysis tools that can provide a more detailed report on extension dependencies.

NikSpyratos's avatar

Lary's solution needs some tweaking to actually work (the regex step is incorrect), but it's still a very crude approximation. It basically only gave me a list of core extensions already included, and wouldn't account for dynamic code. E.g. if you're using MySQL for your DB, and the above code scanned your vendor dir, it would still tell you that you need Postgres, sqlite extensions and so on.

Please or to participate in this conversation.