Score:0

"Missing class doc comment" when using attribute on class

cn flag

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

id flag
Is declaring the properties not an option?
Kevin avatar
in flag
Is the coding standards file up to date for attributes, php 8.2 etc?
cn flag
@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.
cn flag
@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.
Jaypan avatar
de flag
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.
cn flag
Might well just be a bug like this one: https://www.drupal.org/project/coder/issues/3250986
Score:0
cn flag

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.

Jaypan avatar
de flag
Why not statically define the class properties with their own docblocks, like is done in the rest of Drupal?
cn flag
@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.
Jaypan avatar
de flag
I see. Thanks, I was curious about the use case.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.