Skip to main content

A playground to test Drupal code with phpstan-drupal is coming soon!

Published on

I'm excited to announce a new feature coming to phpstan-drupal that already exists for PHPStan. PHPStan has an online playground to run and test analysis results. Soon, we will have one for phpstan-drupal! The online playground is an extremely useful tool when reporting bugs to PHPStan and will make it easier to report bugs for phpstan-drupal. 

I had thought about building this previously but was concerned about possible costs. After all, phpstan-drupal is a personal open-source project. It was brought up again in the #phpstan channel in Drupal Slack by Dezső Biczó (mxr576.) In great timing, OPTASY recently signed up as an organization sponsor through GitHub Sponsors. I will use these funds to pay for the playground's operating costs. 

When up an running, hopefully by the end of November, the playground will support analyzing code against the latest version of Drupal with PHPStan and phpstan-drupal. Later iterations will allow customizing the version of Drupal used (it's a bit more complicated.)

I emailed Ondřej asking if it was okay to copy the code; even though it's open source, it's always good to ask. Ondřej was also nice enough to disclose that the playground is affordable. The phpstan-drupal playground will probably receive less traffic, but I expect it to have longer execution times. With that, I'm assuming it should fall within a reasonable range.

The playground uses the Serverless framework to deploy to AWS Lambda. The code is broken into three components:

  • playground-runner: Executes sample PHP code with PHPStan configuration and returns the results, not a publicly exposed function.
  • playground-api: Public API, which executes the playground-runner and allows storing results in an S3 bucket for sharing.
  • website: The interface to interact with the playground-api.

I got everything up and running in a few hours with a rough interface. The biggest challenge was getting phpstan-drupal to properly analyze with the drupal/core package in the vendor directory. With a few hacks, it works.

However, there are some quicks. When I passed it the following code, I got the correct deprecation errors:


module_load_include('inc', 'node', 'node.admin');

But, when I sent the following:


module_load_include('inc', 'node', 'node.admin');
\Drupal::moduleHandler()->loadInclude('node', 'inc', 'node.admin');

It told me that the \Drupal class could not be found! I have some debugging to do.

I also am going to rewrite the playground-api code copied from PHPStan. PHPStan supports testing code from PHP 7.2 to PHP 8.2 and beyond. The phpstan-drupal playground will only run PHP versions supported by Drupal core. I will also need to see if we can support multiple versions of Drupal core. It might result in a few different playground-runner functions. For example, one for 11.x, 10.3.x, and ^10.2.0. We'll see.