0

I recently updated PHP from 8.1 to 8.2.

This resulted in a bunch of deprecation notices about use of dynamic properties in PHPUnit tests.

So, based on the docs, I added the AllowDynamicProperties class.

/**
 * Unit tests for the myModule class (timing).
 *
 * @group myModuleSuite
 *
 * @coversDefaultClass \Drupal\myModule\Class
 *
 * @property \DateTimeZone $timezone_utc
 * @property \DateTimeZone $timezone_nyc
 */
#[\AllowDynamicProperties]
class MyModuleTimingTest extends UnitTestCase {

This fixes the deprecation notice for PHP 8.2, but now the Drupal coding standards check complains about a "missing doc comment" for my MyModuleTimingTest class.

How am I supposed to write this so that I can use the attribute and not violate the coding standards?

How I'm checking the coding standards

Using Coder module 8.3.16.

/vendor/bin/phpcs -p --colors --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,info,txt,md --ignore=node_modules,vendor ./web/modules/custom ./web/themes/custom

6
  • Is declaring the properties not an option?
    – mona lisa
    Commented Feb 3, 2023 at 13:45
  • Is the coding standards file up to date for attributes, php 8.2 etc?
    – Kevin
    Commented Feb 3, 2023 at 14:55
  • @Kevin I'm doing this with the latest version of Coder as of writing, 8.3.16. That was released in August 2022 prior to PHP 8.2, but attributes arrived in PHP 8.0, so I assume there would be some way to handle them by now. Commented Feb 3, 2023 at 15:22
  • @cilefen I could declare the properties to fix this as well, but here I want to know how I can use an attribute with a class in a way that does not violate the coding standards. Commented Feb 3, 2023 at 15:24
  • Is there a particular reason for making your properties dynamic and not declare them in the class? Using dynamic properties is new to me. Usually properties are static and declared in the class, and each property has a codeblock for it. I'm not clear on the use case for dynamic properties.
    – Jaypan
    Commented Feb 3, 2023 at 16:12

1 Answer 1

0

Update: This is now fixed as of Coder 3.17.

Original:

Sure enough, as pointed out by @Clive, this is a bug with Coder and a fix is in progress.

So right now, there is no good way to declare PHP attributes on classes without the coding standards check complaining.

2
  • Why not statically define the class properties with their own docblocks, like is done in the rest of Drupal?
    – Jaypan
    Commented Feb 9, 2023 at 1:49
  • @Jaypan I do that for all my regular code, but in this case, I have some unit tests for a custom module that loop through an array to generate a few hundred immutable datetimes as properties in the setUp() method to make it easy to test a bunch of time-related edge cases. Putting all of these in a docblock seems excessively verbose. Commented Feb 9, 2023 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.