Problem/Motivation

SyncToMenuForm.php does not pass PHPStan and PHPCS checks cleanly. Found while reviewing #3434782: Automated Drupal 11 compatibility fixes for stm. This is broken down into two parts:

1. PHPStan static-analysis finding

Running Drupal Check 1.5.0 locally, which runs PHPStan with Drupal-specific extensions, reports access to an undefined parent_tid property.

During recursive taxonomy-to-menu synchronization, the parent term ID is temporarily attached to the child term entity:

$children_term->parent_tid = $tid;

The syncTermToMenuLink() method then reads that value:

$parent_tid = isset($term->parent_tid) ? $term->parent_tid : NULL;

$term->parent_tid is not an actual taxonomy term field or a declared property. Technically, content entities in Drupal do permit non-field values via magic property methods. Therefore, this is purely a static analysis issue and doesn't constitute a runtime failure or Drupal 11 compatibility blocker.

However, using an undeclared property makes the parent-child data flow implicit; it temporarily mutates the term entity and prevents the class from passing static analysis cleanly.

2. PHPCS coding-standards findings

Running PHP_CodeSniffer with the Drupal and DrupalPractice standards reports extensive pre-existing coding-standards violations in SyncToMenuForm.php.

These include formatting, documentation, type usage, and Drupal best-practice findings. The file should be brought into compliance without introducing unrelated functional changes or expanding the cleanup to other files.

Steps to reproduce

  1. Install Drupal Check and PHP_CodeSniffer in a Drupal project containing STM.
  2. Check out STM's 8.x-1.x branch.
  3. Run Drupal Check against the form class:
    vendor/bin/drupal-check --no-progress web/modules/contrib/stm/src/Form/SyncToMenuForm.php
    
  4. Observe the static-analysis complaint concerning the undefined parent_tid property.
  5. Run PHPCS with the Drupal and DrupalPractice standards:
    vendor/bin/phpcs --standard=Drupal,DrupalPractice -s web/modules/contrib/stm/src/Form/SyncToMenuForm.php
    
  6. Observe the coding-standards violations reported for the file.

Proposed resolution

Address the two groups of findings as separate, reviewable changes.

1. Pass the parent term ID explicitly

Add an optional $parent_tid argument to syncTermToMenuLink().

When recursively synchronizing a child term, pass the current term ID directly as the child's parent term ID:

$this->syncTermToMenuLink(
  $children_term,
  $menu_name,
  $path_pattern,
  $vid,
  $tid
);

Use that argument to locate the parent term's menu link instead of attaching a temporary property to the term entity.

Root terms continue to omit the argument, which defaults to NULL. The generated menu hierarchy and synchronization behavior should remain unchanged.

2. Apply Drupal coding standards

Update SyncToMenuForm.php to pass the Drupal and DrupalPractice PHPCS standards.

Limit the changes to this class and address its existing formatting, documentation, type usage, service access, and related coding-practice findings. Avoid unrelated module-wide cleanup or behavioral changes.

Remaining tasks

  • ✅ Replace the temporary entity property with an explicit method argument.
  • ✅ Confirm Drupal Check reports no errors for the updated class.
  • ✅ Apply the Drupal and DrupalPractice coding standards.
  • ✅ Confirm PHPCS reports no violations for the updated class.
  • ✅ Verified that root and nested taxonomy terms retain the correct menu hierarchy.
  • ✅ Review and merge the changes.

User interface changes

None.

API changes

The public syncTermToMenuLink() method gains an optional $parent_tid argument. Existing callers remain compatible because the argument defaults to NULL.

No other intentional public API changes are introduced.

Data model changes

None. No taxonomy fields, entity properties, configuration, or database schema are added.

Issue fork stm-3635878

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jwilson3 created an issue. See original summary.

jwilson3’s picture

Title: Pass PhpStan and PhpCS » Coding standards: Pass PHPStan and PHPCS

jwilson3’s picture

Issue summary: View changes

Automated test coverage has been added for this requirement.

The kernel test creates two root terms and a three-level taxonomy hierarchy, synchronizes them to a menu, and verifies that:

  • Root terms have no menu parent.
  • The child menu link references the root menu link as its parent.
  • The grandchild menu link references the child menu link as its parent.
  • Each term produces exactly one menu link.

A functional browser test also submits the synchronization form through the actual Drupal route and verifies the persisted root → child → grandchild menu hierarchy.

Additional coverage verifies form defaults, repeat synchronization without duplicates, updates to existing links, and menu/vocabulary-scoped link lookup.

All tests pass in GitLab CI against Drupal 11.4.9 and Drupal 10.6.18.

  • jwilson3 committed 95158b09 on 8.x-1.x
    Issue #3635878 by jwilson3: Fix coding standards, pass PHPStan and PHPCS
    
jwilson3’s picture

Issue summary: View changes
Status: Active » Fixed

Fixed in release 1.5.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.