 [ ← blog / phpstan ](/) Nov 14, 2023  ·  \#phpstan  ·  3 min read 

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



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](https://phpstan.org/try). Soon, we will have one for [phpstan-drupal](https://github.com/mglaman/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](https://www.drupal.org/u/mxr576).) In great timing, [OPTASY](https://www.optasy.com/) recently signed up as an organization sponsor through [GitHub Sponsors](https://github.com/sponsors/mglaman). 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](https://www.serverless.com/) framework to deploy to [AWS Lambda](https://aws.amazon.com/lambda/). The code is broken into three components:

- [`playground-runner`](https://github.com/mglaman/phpstan-drupal/tree/fa79d1f1caa8f9e39eaac3d2d8664dff15483c3e/playground-runner): Executes sample PHP code with PHPStan configuration and returns the results, not a publicly exposed function.
- [`playground-api`](https://github.com/mglaman/phpstan-drupal/tree/fa79d1f1caa8f9e39eaac3d2d8664dff15483c3e/playground-api): Public API, which executes the `playground-runner` and allows storing results in an S3 bucket for sharing.
- [`website`](https://github.com/mglaman/phpstan-drupal/tree/d8f4b4b6ce3149483cb61ac76c8c9a831dd56481/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](https://github.com/mglaman/phpstan-drupal/blob/main/playground-runner/scaffold.php).

     ![phpstan-drupal playground sample form overview](/sites/default/files/styles/large/public/2023-11/Screenshot%202023-11-10%20at%204.48.27%E2%80%AFPM.png?itok=dgEkv19y) 

 

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

```php
<?php

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

But, when I sent the following:

```php
<?php

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.

 

  ![](/themes/custom/mglaman/images/headshot.webp)Thanks for reading

I write about Drupal, PHP, and the quiet infrastructure behind large sites — caching, config, and testing. New posts land every week or two.

 [Subscribe via RSS →](/rss.xml) [About me →](/about-me) 

 



 

  [← Older

PHPStan is now running for all Drupal modules on GitLab CI!

 ](/blog/phpstan-now-running-all-drupal-modules-gitlab-ci) [Newer →

Looking back on 2023

 ](/blog/looking-back-2023)