Problem/Motivation

Amongst the "new" errors found when running PHPStan on level 2 is: Variable $foo in PHPDoc tag @var does not exist.

This child-issue exists to fix the "low hanging fruit" ones.

Steps to reproduce

- Run PHPStan on level 2 and see the above error amongst all others.

Proposed resolution

- Solve all of the the above mentioned reported errors.
- Run PHPStan on level 2 and see less of the above mentioned error.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3323702

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

Spokje created an issue. See original summary.

spokje’s picture

StatusFileSize
new14.8 KB
spokje’s picture

StatusFileSize
new14.09 KB
spokje’s picture

StatusFileSize
new2.22 MB
new2.22 MB

A pre and post lvl2-baseline for 10.1.x-dev is attached.

spokje’s picture

Status: Active » Needs review
mondrake’s picture

+++ b/core/modules/taxonomy/src/TermStorage.php
@@ -164,7 +164,6 @@ protected function getParents(TermInterface $term) {
    */
   public function loadAllParents($tid) {
-    /** @var \Drupal\taxonomy\TermInterface $term */
     return (!empty($tid) && $term = $this->load($tid)) ? $this->getAncestors($term) : [];
   }
 
@@ -198,7 +197,6 @@ protected function getAncestors(TermInterface $term) {

@@ -198,7 +197,6 @@ protected function getAncestors(TermInterface $term) {
    * {@inheritdoc}
    */
   public function loadChildren($tid, $vid = NULL) {
-    /** @var \Drupal\taxonomy\TermInterface $term */
     return (!empty($tid) && $term = $this->load($tid)) ? $this->getChildren($term) : [];
   }

I think in this case we should first check $tid is not empty, and return if it is. Then, load $term, and in this case we can keep the type hint, adding null to the signature. Then, return the final result. i.e.

   public function loadChildren($tid, $vid = NULL) {
     if (empty($tid)) {
       return [];
     }
     /** @var \Drupal\taxonomy\TermInterface|null $term */
     $term = $this->load($tid));
     return $term ? $this->getChildren($term) : [];
   }
mallezie’s picture

Status: Needs review » Needs work

Putting needs work for #6.
I like this proposed change, makes things more readible.
Other changes look good.

_pratik_’s picture

Assigned: Unassigned » _pratik_
_pratik_’s picture

Assigned: _pratik_ » Unassigned
Status: Needs work » Needs review
StatusFileSize
new14.34 KB
new2.3 KB

I Hope this will work here.
Thanks

mallezie’s picture

Status: Needs review » Needs work

Thanks @_pratik_

However instead of

if (!empty($tid) && is_numeric($tid)) {
+      /**
+       * @var \Drupal\taxonomy\TermInterface|null $term
+       *    Term object.
+       */
+      $term = $this->load($tid);
+      return $this->getChildren($term);
+    }
+    return [];

I would adjust this to

if (!empty($tid)) {
+      /** @var \Drupal\taxonomy\TermInterface|null $term */
+      $term = $this->load($tid);
+      return $this->getChildren($term);
+    }
+    return [];

This can be a single line comment for the var declaration.
Also checking for is_numeric is not needed here. This would be better typehinted on the method, but that's out of scope here.

_utsavsharma’s picture

Status: Needs work » Needs review
StatusFileSize
new660 bytes
new14.29 KB

Made changes as per the #10.
Please review.

spokje’s picture

StatusFileSize
new14.28 KB
new498 bytes

Made all the changes as per the #10.
Please review.

spokje’s picture

StatusFileSize
new2.18 MB
new2.17 MB
andypost’s picture

Status: Needs review » Needs work

This type-hint is useless here, please revert the change and remove hint

+++ b/core/modules/taxonomy/src/TermStorage.php
@@ -198,8 +197,12 @@ protected function getAncestors(TermInterface $term) {
-    /** @var \Drupal\taxonomy\TermInterface $term */
-    return (!empty($tid) && $term = $this->load($tid)) ? $this->getChildren($term) : [];
+    if (!empty($tid)) {
+      /** @var \Drupal\taxonomy\TermInterface|null $term */
+      $term = $this->load($tid);
+      return $this->getChildren($term);

this breaks logic, load() may return NULL but getChildren() expect entity

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone made their first commit to this issue’s fork.

quietone’s picture

Status: Needs work » Needs review

Converted to an MR and added a few more easy ones. The rest all require a bit more thought, so setting this for a review.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems like a good cleanup.

the 3 additions/moves of @var make sense to be added above the variables.

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Is there something we can enable in our phpstan config or ignore entries we can remove here to prevent this coming back.

I'm wary of doing these in piecemeal chunks without a corresponding rule change to prevent it coming back - much like the approach we take for PHPCS

I've reviewed the parent issue and see that this was split to achieve some momentum, so in this case I'm ok with committing it, but ideally we'd be adding a rule or updating baselines etc to keep them from coming back.

Committed to 11.x

Thanks folks

  • larowlan committed 2832459c on 11.x
    Issue #3323702 by spokje, _pratik_, quietone: Fix PHPStan L2 error "...
mondrake’s picture

@larowlan

Is there something we can enable in our phpstan config or ignore entries we can remove here to prevent this coming back.

that would be #3425412: [Sep 14, 2026] Bump PHPStan rule level to 3

larowlan’s picture

Cheers 👍

Status: Fixed » Closed (fixed)

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