fix phpstan warnings

Created on 20 March 2025, 5 months ago

Problem/Motivation

phpstan reports:

------ ------------------------------------------------------------------------------
Line modules/context_ui/src/ContextListBuilder.php
------ ------------------------------------------------------------------------------
78 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line modules/context_ui/src/Controller/ContextUIController.php
------ ------------------------------------------------------------------------------
72 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line modules/context_ui/src/Form/ConditionDeleteForm.php
------ ------------------------------------------------------------------------------
54 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ -----------------------------------------------------------------------------------
Line modules/context_ui/src/Form/ContextAddForm.php
------ -----------------------------------------------------------------------------------
27 Method Drupal\context_ui\Form\ContextAddForm::save() should return int but return
statement is missing.
------ -----------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line modules/context_ui/src/Form/ContextDeleteForm.php
------ ------------------------------------------------------------------------------
37 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line modules/context_ui/src/Form/ContextDisableForm.php
------ ------------------------------------------------------------------------------
37 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line modules/context_ui/src/Form/ContextDuplicateForm.php
------ ------------------------------------------------------------------------------
37 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ -----------------------------------------------------------------------------
Line modules/context_ui/src/Form/ContextEditForm.php
------ -----------------------------------------------------------------------------
64 Method Drupal\context_ui\Form\ContextEditForm::save() should return int but
return statement is missing.
------ -----------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line modules/context_ui/src/Form/ContextFormBase.php
------ ------------------------------------------------------------------------------
57 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line modules/context_ui/src/Form/ReactionDeleteForm.php
------ ------------------------------------------------------------------------------
55 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line modules/context_ui/src/Plugin/Block/ContextInspector.php
------ ------------------------------------------------------------------------------
91 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------
Line src/ContextManager.php
------ ------------------------------------------------------------------------
156 Method Drupal\context\ContextManager::getContext() should return
array|Drupal\context\ContextInterface but return statement is missing.
------ ------------------------------------------------------------------------

------ -----------------------------------------------------------------------
Line src/Entity/Context.php
------ -----------------------------------------------------------------------
370 Method Drupal\context\Entity\Context::disable() should return
$this(Drupal\context\Entity\Context) but return statement is missing.
------ -----------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Entity/ContextAccess.php
------ ------------------------------------------------------------------------------
31 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/Condition/ContextAll.php
------ ------------------------------------------------------------------------------
50 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/Condition/ContextAny.php
------ ------------------------------------------------------------------------------
50 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/Condition/HttpStatusCode.php
------ ------------------------------------------------------------------------------
50 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/Condition/RequestDomain.php
------ ------------------------------------------------------------------------------
56 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/Condition/UserProfilePage.php
------ ------------------------------------------------------------------------------
62 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/Condition/ViewInclusion.php
------ ------------------------------------------------------------------------------
63 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/ContextReaction/Blocks.php
------ ------------------------------------------------------------------------------
158 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
300 \Drupal calls should be avoided in classes, use dependency injection instead
303 \Drupal calls should be avoided in classes, use dependency injection instead
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/ContextReaction/Menu.php
------ ------------------------------------------------------------------------------
40 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/ContextReaction/Regions.php
------ ------------------------------------------------------------------------------
63 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/ContextReaction/Theme.php
------ ------------------------------------------------------------------------------
55 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
85 \Drupal calls should be avoided in classes, use dependency injection instead
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Plugin/DisplayVariant/ContextBlockPageVariant.php
------ ------------------------------------------------------------------------------
90 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Reaction/Blocks/Controller/ContextReactionBlocksController.php
------ ------------------------------------------------------------------------------
79 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Reaction/Blocks/Form/BlockDeleteForm.php
------ ------------------------------------------------------------------------------
61 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Reaction/Blocks/Form/BlockFormBase.php
------ ------------------------------------------------------------------------------
159 Unsafe usage of new static().
💡 See:
https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static
------ ------------------------------------------------------------------------------

------ ------------------------------------------------------------------------------
Line src/Theme/ThemeSwitcherNegotiator.php
------ ------------------------------------------------------------------------------
76 \Drupal calls should be avoided in classes, use dependency injection instead
80 \Drupal calls should be avoided in classes, use dependency injection instead
------ ------------------------------------------------------------------------------

Steps to reproduce

phpstan analyse context

Proposed resolution

  • In ContextListBuilder, use parent::createInstance() rather than re-implementing __construct().
  • In ContextUIController, ConditionDeleteForm, ContextDeleteForm, ContextDisableForm, ReactionDeleteForm, ContextReactionBlocksController, BlockDeleteForm, and BlockFormBase, use parent::create() rather than re-implementing __construct().
  • In ContextAddForm and ContextEditForm, return $status.
  • In ContextDuplicateForm, remove both create() and __construct() because the parent class has it covered.
  • In ContextInspector, ContextAccess, ContextAll, ContextAny, HttpStatusCode, RequestDomain, UserProfilePage, ViewInclusion, and ContextBlockPageVariant, mark __construct() as final.
  • In ContextManager, return NULL by default.
  • In Context, return $this.
  • Implement ContainerFactoryPluginInterface in ContextReactionPluginBase so that the child classes Blocks, Menu, Regions, and Theme can use parent::create() instead of re-implementing __construct().
  • In Blocks, inject the module_handler dependency. Also, in the execute() function, call $block->setMainContent() with a render array, not a string.
  • In Theme and ThemeSwitcherNegotiator, inject the config.factory dependency.

Remaining tasks

Make an MR.

🐛 Bug report
Status

Active

Version

5.0

Component

Code

Created by

🇺🇸United States BenStallings

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @BenStallings
  • Merge request !63Resolve #3514532 "Fix phpstan warnings" → (Open) created by BenStallings
  • Pipeline finished with Failed
    5 months ago
    Total: 168s
    #453582
  • 🇮🇳India sdhruvi5142

    I will work on this.

  • Issue was unassigned.
  • Status changed to Needs review 9 days ago
  • First commit to issue fork.
  • Pipeline finished with Canceled
    9 days ago
    Total: 139s
    #577375
  • Pipeline finished with Success
    9 days ago
    Total: 269s
    #577377
  • Pipeline finished with Success
    9 days ago
    Total: 145s
    #577384
  • 🇸🇮Slovenia deaom

    There is still one phpstan warning in reghards to Drupal\devel\DevelDumperManager. The devel module is not listed as dependency so the tests are not installing it, meaning it can't be found so a warning is triggered. If devel is needed or that part of code where devel_dump is used, then dependency needs to be added to devel module.
    Tests are now passing, as gitlab-ci is testing previous, current and next releases there will be some inconsistencies as there are differences in phpunit verions.
    I'm leaving this to needs review.

  • Pipeline finished with Success
    9 days ago
    Total: 352s
    #577391
Production build 0.71.5 2024