diff --git a/source/_daily_emails/2024-09-22.md b/source/_daily_emails/2024-09-22.md new file mode 100644 index 00000000..6e0f3e9f --- /dev/null +++ b/source/_daily_emails/2024-09-22.md @@ -0,0 +1,29 @@ +--- +title: Writing custom PHPStan rules for Drupal projects +date: 2024-09-22 +permalink: daily/2024/09/22/writing-custom-phpstan-rules-for-drupal-projects +tags: + - software-development + - drupal + - php + - phpstan +cta: ~ +snippet: | + I wrote a custom PHPStan rule! +drupal_planet: true +--- + +Today, I watched a video recording of a session by Ondřej Mirtes - the creator of PHPStan - on writing custom rules for PHPStan. + +After explaining how to write them, he showed some examples of custom rules - one of which was ensuring a `Person` class couldn't be created outside of a `PersonRepository` class. + +This seemed straightforward so I thought about how I could do something similar within my [Drupal automated testing email course][0] to check my Drupal code. + +Similar to Ondřej's example, I wondered if I could enforce that no post nodes should be created outside of the `PostBuilder` class by searching for any code like `Node::create(['type' => 'post'])`. + +I was able to get a version working quickly and have [posted it as a gist][1]. + +I'm sure some improvements can be made, but it was a successful experiment and something I can see me using more in the future. + +[0]: {{site.url}}/atdc +[1]: https://gist.github.com/opdavies/a2f9d92cf3b67db6a64b9fca4e4e6697