Skip to content

Add rule to detect Symfony\Component\Yaml\Yaml::parse() usage#978

Merged
mglaman merged 4 commits intomainfrom
feature/symfony-yaml-parse-rule
Apr 15, 2026
Merged

Add rule to detect Symfony\Component\Yaml\Yaml::parse() usage#978
mglaman merged 4 commits intomainfrom
feature/symfony-yaml-parse-rule

Conversation

@mglaman
Copy link
Copy Markdown
Owner

@mglaman mglaman commented Apr 15, 2026

Summary

Adds a new opt-in rule (drupal.symfonyYamlParseRule) that flags direct calls to Symfony\Component\Yaml\Yaml::parse() and suggests using \Drupal\Component\Serialization\Yaml::decode() instead.

Drupal's wrapper should be preferred because it:

  • Wraps Symfony YAML exceptions as Drupal\Component\Serialization\Exception\InvalidDataTypeException, keeping error handling consistent across the codebase
  • Always applies the correct parse flags (PARSE_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\Yaml became a class_alias() for Drupal\Component\Serialization\Yaml. The rule straightforwardly suggests the Component class.

Test plan

  • php vendor/bin/phpunit --filter=SymfonyYamlParseRuleTest — new test passes
  • php vendor/bin/phpunit — full suite green (772 tests)
  • php vendor/bin/phpcs src/Rules/Drupal/SymfonyYamlParseRule.php — no lint errors
  • php vendor/bin/phpstan analyze src/Rules/Drupal/SymfonyYamlParseRule.php — no errors

🤖 Generated with Claude Code

mglaman and others added 3 commits April 15, 2026 15:17
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>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 SymfonyYamlParseRule to detect Symfony\Component\Yaml\Yaml::parse() static calls and emit a rule error with identifier drupal.symfonyYamlParse.
  • Added PHPUnit coverage + a fixture demonstrating flagged and allowed usage.
  • Wired the rule into configuration (extension.neon, rules.neon, bleedingEdge.neon) behind a new drupal.rules.symfonyYamlParseRule toggle.

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.

Comment thread src/Rules/Drupal/SymfonyYamlParseRule.php Outdated
Comment thread src/Rules/Drupal/SymfonyYamlParseRule.php
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>
@mglaman mglaman merged commit 25e0bea into main Apr 15, 2026
14 of 15 checks passed
@mglaman mglaman deleted the feature/symfony-yaml-parse-rule branch April 15, 2026 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect usage of Symfony\Component\Yaml\Yaml::parse and suggest Drupal\Core\Serialization\Yaml::decode

2 participants