Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any tool to check a composer package for presence of actually used PHP extensions in "required", "required-dev" and "suggest" sections?

I have made a composer package "foo" which uses ext-mysqli . On my dev box mysqli extension is present and so all my tests for this package succeed without any issue. But if this package would land on a box without mysqli extension installed then it would fail miserably. Composer addresses the issue by allowing "ext-*" in "require", "require-dev" and "suggest" sections. But a package developer should remember to actually put these dependencies in. And apparently there is nothing to stop a developer (or at least warn) before publishing a package without all used extensions listed as dependencies.

With multitude of extensions available today it is fairly easy to overlook these dependencies, especially when they are always satisfied on dev box used to develop a particular package and so no local test will reveal their absence.

Is there any tool which may check a composer package to see if it uses particular extensions and warn if extensions used not actually listed in composer.json?

like image 681
Vladimir Bashkirtsev Avatar asked Jul 10 '16 17:07

Vladimir Bashkirtsev


People also ask

How do I know if a package is installed in PHP?

If your server only has a single PHP version installed, you can run this PHP command anywhere, and it will give you the same list of modules. The general command we will be using is php -m. This command will give you the full list of installed PHP modules/extensions.

How do I know if a composer package is installed?

You can run composer show -i (short for --installed ). In the latest version just use composer show .

Where does composer Look for packages?

Composer will look in all your repositories to find the packages your project requires. By default, only the Packagist.org repository is registered in Composer. You can add more repositories to your project by declaring them in composer. json .

Can composer install PHP extensions?

Composer has platform packages, which are virtual packages for things that are installed on the system but are not actually installable by Composer. This includes PHP itself, PHP extensions and some system libraries. php represents the PHP version of the user, allowing you to apply constraints, e.g. ^7.1 .


1 Answers

Thanks to answer by Jens A. Koch I decided to make such tool. It may be found on packagist.org as logics/extcheck package.

As expected it builds a dictionary out of installed extensions and then parses the source code referred in autoload and autoload-dev sections.

It is not implemented as a sniff due to a simple reason: CodeSniffer is mainly concerned with PHP/JS/CSS files and not with composer.json.

Basically to use it all you need to to is to add "logics/extcheck" package to "require-dev" section of composer.json , run "composer update" and then "vendor/bin/extcheck" . It will give you all extensions actually used by the code but not mentioned in composer.json. Call it with -v option and will also give you information about extensions uses.

Notably the original issue is wide spread and even well known/top packages actually have missing extension dependencies. If you think that you have added all required extensions to composer.json then try extcheck - I dare you! :)

like image 168
Vladimir Bashkirtsev Avatar answered Oct 13 '22 01:10

Vladimir Bashkirtsev