Add rule to detect Symfony\Component\Yaml\Yaml::parse() usage#978
Merged
Add rule to detect Symfony\Component\Yaml\Yaml::parse() usage#978
Conversation
Flags direct calls to Symfony's YAML parser and suggests \Drupal\Component\Serialization\Yaml::decode() instead, which respects the yaml_parser_class setting in Drupal's settings.php. Closes #642 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The actual benefit of Drupal\Component\Serialization\Yaml::decode() over Symfony\Component\Yaml\Yaml::parse() is consistent exception handling (InvalidDataTypeException) and correct parse flags. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Follows the same pattern as all other rules in this package: disabled by default, enabled in bleedingEdge.neon. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds an opt-in PHPStan rule to discourage direct usage of Symfony’s YAML parser in Drupal code, steering developers to Drupal’s YAML serialization wrapper.
Changes:
- Added
SymfonyYamlParseRuleto detectSymfony\Component\Yaml\Yaml::parse()static calls and emit a rule error with identifierdrupal.symfonyYamlParse. - Added PHPUnit coverage + a fixture demonstrating flagged and allowed usage.
- Wired the rule into configuration (
extension.neon,rules.neon,bleedingEdge.neon) behind a newdrupal.rules.symfonyYamlParseRuletoggle.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/Rules/Drupal/SymfonyYamlParseRule.php |
Implements the new static-call detection rule and its error message/identifier. |
tests/src/Rules/SymfonyYamlParseRuleTest.php |
Adds a focused test asserting errors are raised on the intended lines. |
tests/src/Rules/data/symfony-yaml-parse.php |
Fixture file containing flagged and non-flagged YAML parsing examples. |
rules.neon |
Registers the rule service and adds conditional enabling via the new toggle. |
extension.neon |
Adds the new rules toggle default + schema entry. |
bleedingEdge.neon |
Enables the new rule by default in bleeding edge mode. |
Handles both FullyQualified and plain Name nodes (from use imports), making the class name resolution explicit rather than relying on the NameResolver having already run. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new opt-in rule (
drupal.symfonyYamlParseRule) that flags direct calls toSymfony\Component\Yaml\Yaml::parse()and suggests using\Drupal\Component\Serialization\Yaml::decode()instead.Drupal's wrapper should be preferred because it:
Drupal\Component\Serialization\Exception\InvalidDataTypeException, keeping error handling consistent across the codebasePARSE_EXCEPTION_ON_INVALID_TYPE | PARSE_CUSTOM_TAGS)Closes #642.
Background: The Drupal core issue linked from #642 (#3205480) was fixed in Drupal 10.3/11.x — PECL YAML support was removed and
Drupal\Core\Serialization\Yamlbecame aclass_alias()forDrupal\Component\Serialization\Yaml. The rule straightforwardly suggests the Component class.Test plan
php vendor/bin/phpunit --filter=SymfonyYamlParseRuleTest— new test passesphp vendor/bin/phpunit— full suite green (772 tests)php vendor/bin/phpcs src/Rules/Drupal/SymfonyYamlParseRule.php— no lint errorsphp vendor/bin/phpstan analyze src/Rules/Drupal/SymfonyYamlParseRule.php— no errors🤖 Generated with Claude Code