fix phpstan warnings

Created on 20 March 2025, 18 days 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

Production build 0.71.5 2024