Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruleset::populateTokenListeners(): add tests #757

Merged
merged 2 commits into from
Dec 19, 2024

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Dec 7, 2024

Description

Suggested changelog entry

N/A

Related issues/external references

This PR is part of a series of PRs expanding the tests for the Ruleset class.

$expected = ['PHP' => 'PHP'];

foreach (self::$ruleset->tokenListeners as $token => $listeners) {
$this->assertTrue(is_array($listeners), 'No listeners registered for token'.Tokens::tokenName($token));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a reason not to use sprintf here? Seems to be used in the other cases.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be some other copies where this same construct is used, throughout this class.

Copy link
Member Author

@jrfnl jrfnl Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NielsdeBlaauw Thanks for reviewing.

As for interpolation vs concatenation vs sprintf(): there is no hard rule being followed. The important thing here is that if a test contains more than one assertion, the $message parameter is being set for each assertion to make it more straight forward to know which assertion failed (if the test would fail). Especially if there are, for instance, three assertTrue() assertions, it becomes hard to distinguish which failed - "false is not true" - without those $message parameters.

For all practical purposes, whether interpolation vs concatenation vs sprintf() is used for the $message parameter is largely a balance of code readability, trying to keep the line length below 120 and trying not to violate the "concatenation operator is not allowed to have whitespace around it" CS rule which is being enforced for this codebase.

... and add one extra assertion to increase the stability of the tests.
Copy link
Contributor

@rodrigoprimo rodrigoprimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR, @jrfnl. Overall, it looks good to me.

I left a few inline comments with some questions.

I also wonder why the test class for the Ruleset::populateTokenListeners() method tests the changes this method makes to the Ruleset::tokenListeners property, but not to other Ruleset public properties like Ruleset::sniffs and Rules::sniffCodes.

*
* @return void
*/
public function testSetsSupportedTokenizersWhenProvidedBySniff($token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related to this PR, and I imagine you already have this on your radar, but I wonder if we should add a check to Ruleset::populateTokenListeners() to ensure that $vars['supportedTokenizers'] is an array before calling foreach:

foreach ($vars['supportedTokenizers'] as $tokenizer) {

Copy link
Member Author

@jrfnl jrfnl Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not really on my radar and as you correctly say: outside the scope of this PR. This PR is about adding tests for the existing code, not about improving the existing code.

Having said that, I'm not all that inclined to add an is_array() check for the following reasons:

  1. As of PHPCS 4.0, the property is no longer supported as there will only be one supported tokenizer.
  2. Guard code IMO should be mostly about protecting end users from accidental mistakes. Sniff developers should have at least a basic understanding of what they're doing.
  3. The property has always been an array property, there is no new or changed behaviour (other than the upcoming change in 4.0).
  4. If a sniff would do this wrong, I believe it is a good thing that this would lead to a PHP error during development of the sniff.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to elaborate on why you are not inclined to add an is_array() check. Makes sense to me.

@jrfnl jrfnl merged commit 46b9a59 into master Dec 19, 2024
57 checks passed
@jrfnl jrfnl deleted the feature/ruleset-add-tests-populatetokenlisteners branch December 19, 2024 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants