From 5e7d8a696aa5bdc4fceb4d2b3558b7530e1064d5 Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Tue, 5 Aug 2025 08:30:23 +0200 Subject: [PATCH 1/3] Introducing dynamic generic type definitions in php docblocks, to improve static analysis --- app/code/Magento/ProductAlert/Helper/Data.php | 8 +++- .../Framework/View/Element/BlockFactory.php | 12 ++++-- .../Magento/Framework/View/Layout.php | 39 +++++++++++++------ .../Framework/View/Layout/Generator/Block.php | 23 +++++++---- .../Framework/View/LayoutInterface.php | 31 +++++++++------ .../Framework/View/TemplateEngine/Php.php | 12 ++++-- 6 files changed, 84 insertions(+), 41 deletions(-) diff --git a/app/code/Magento/ProductAlert/Helper/Data.php b/app/code/Magento/ProductAlert/Helper/Data.php index 6806e15de288d..03f17bdda9df2 100644 --- a/app/code/Magento/ProductAlert/Helper/Data.php +++ b/app/code/Magento/ProductAlert/Helper/Data.php @@ -110,8 +110,12 @@ public function getSaveUrl($type) /** * Create block instance * - * @param string|\Magento\Framework\View\Element\AbstractBlock $block - * @return \Magento\Framework\View\Element\AbstractBlock + * @template T of \Magento\Framework\View\Element\AbstractBlock + * + * @param class-string|T $block + * + * @return T + * * @throws \Magento\Framework\Exception\LocalizedException */ public function createBlock($block) diff --git a/lib/internal/Magento/Framework/View/Element/BlockFactory.php b/lib/internal/Magento/Framework/View/Element/BlockFactory.php index a690d6aed0dfb..7d442fe306a84 100644 --- a/lib/internal/Magento/Framework/View/Element/BlockFactory.php +++ b/lib/internal/Magento/Framework/View/Element/BlockFactory.php @@ -1,7 +1,7 @@ $blockName * @param array $arguments - * @return \Magento\Framework\View\Element\BlockInterface + * + * @return T + * * @throws \LogicException */ public function createBlock($blockName, array $arguments = []) diff --git a/lib/internal/Magento/Framework/View/Layout.php b/lib/internal/Magento/Framework/View/Layout.php index 3bc42f846a819..40010b55513b7 100644 --- a/lib/internal/Magento/Framework/View/Layout.php +++ b/lib/internal/Magento/Framework/View/Layout.php @@ -175,7 +175,7 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra protected $appState; /** - * @var \Psr\Log\LoggerInterface + * @var Logger */ protected $logger; @@ -204,8 +204,8 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra * @param FrontendInterface $cache * @param ContextFactory $readerContextFactory * @param Layout\Generator\ContextFactory $generatorContextFactory - * @param State $appState - * @param LoggerInterface $logger + * @param AppState $appState + * @param Logger $logger * @param bool $cacheable * @param SerializerInterface|null $serializer * @param int|null $cacheLifetime @@ -785,10 +785,13 @@ public function unsetElement($name) /** * Block Factory * - * @param string $type + * @template T of \Magento\Framework\View\Element\AbstractBlock + * + * @param class-string $type * @param string $name * @param array $arguments - * @return \Magento\Framework\View\Element\AbstractBlock + * + * @return T */ public function createBlock($type, $name = '', array $arguments = []) { @@ -796,16 +799,20 @@ public function createBlock($type, $name = '', array $arguments = []) $name = $this->structure->createStructuralElement($name, Element::TYPE_BLOCK, $type); $block = $this->_createBlock($type, $name, $arguments); $block->setLayout($this); + return $block; } /** * Create block and add to layout * - * @param string $type + * @template T of \Magento\Framework\View\Element\AbstractBlock + * + * @param class-string $type * @param string $name * @param array $arguments - * @return \Magento\Framework\View\Element\AbstractBlock + * + * @return T */ protected function _createBlock($type, $name, array $arguments = []) { @@ -813,17 +820,21 @@ protected function _createBlock($type, $name, array $arguments = []) $blockGenerator = $this->generatorPool->getGenerator(Layout\Generator\Block::TYPE); $block = $blockGenerator->createBlock($type, $name, $arguments); $this->setBlock($name, $block); + return $block; } /** * Add a block to registry, create new object if needed * - * @param string|\Magento\Framework\View\Element\AbstractBlock $block + * @template T of \Magento\Framework\View\Element\AbstractBlock + * + * @param class-string|T $block * @param string $name * @param string $parent * @param string $alias - * @return \Magento\Framework\View\Element\AbstractBlock + * + * @return T */ public function addBlock($block, $name = '', $parent = '', $alias = '') { @@ -1012,8 +1023,12 @@ public function getMessagesBlock() /** * Get block singleton * - * @param string $type - * @return \Magento\Framework\App\Helper\AbstractHelper + * @template T of \Magento\Framework\View\Element\AbstractBlock + * + * @param class-string $type + * + * @return T + * * @throws \Magento\Framework\Exception\LocalizedException */ public function getBlockSingleton($type) @@ -1086,7 +1101,7 @@ public function executeRenderer($namespace, $staticType, $dynamicType, $data = [ $this->build(); if ($options = $this->getRendererOptions($namespace, $staticType, $dynamicType)) { $dictionary = []; - /** @var $block \Magento\Framework\View\Element\Template */ + /** @var \Magento\Framework\View\Element\Template $block */ $block = $this->createBlock($options['type'], '') ->setData($data) ->assign($dictionary) diff --git a/lib/internal/Magento/Framework/View/Layout/Generator/Block.php b/lib/internal/Magento/Framework/View/Layout/Generator/Block.php index 98cf78784070e..8ffa18268cc0f 100644 --- a/lib/internal/Magento/Framework/View/Layout/Generator/Block.php +++ b/lib/internal/Magento/Framework/View/Layout/Generator/Block.php @@ -1,7 +1,7 @@ getScheduledStructure(); $layout = $generatorContext->getLayout(); $structure = $generatorContext->getStructure(); - /** @var $blocks \Magento\Framework\View\Element\AbstractBlock[] */ + /** @var \Magento\Framework\View\Element\AbstractBlock[] $blocks */ $blocks = []; $blockActions = []; // Instantiate blocks and collect all actions data @@ -242,10 +242,13 @@ protected function generateBlock( /** * Create block instance * - * @param string|\Magento\Framework\View\Element\AbstractBlock $block + * @template T of \Magento\Framework\View\Element\AbstractBlock + * + * @param class-string|T $block * @param string $name * @param array $arguments - * @return \Magento\Framework\View\Element\AbstractBlock + * + * @return T */ public function createBlock($block, $name, array $arguments = []) { @@ -259,10 +262,14 @@ public function createBlock($block, $name, array $arguments = []) /** * Create block object instance based on block type * - * @param string|\Magento\Framework\View\Element\AbstractBlock $block + * @template T of \Magento\Framework\View\Element\AbstractBlock + * + * @param class-string|T $block * @param array $arguments + * * @throws \Magento\Framework\Exception\LocalizedException - * @return \Magento\Framework\View\Element\AbstractBlock + * + * @return T */ protected function getBlockInstance($block, array $arguments = []) { diff --git a/lib/internal/Magento/Framework/View/LayoutInterface.php b/lib/internal/Magento/Framework/View/LayoutInterface.php index 3a63b5ccc9ea3..c614177524072 100644 --- a/lib/internal/Magento/Framework/View/LayoutInterface.php +++ b/lib/internal/Magento/Framework/View/LayoutInterface.php @@ -1,7 +1,7 @@ $type + * @param string $name + * @param array $arguments + * + * @return T */ public function createBlock($type, $name = '', array $arguments = []); /** * Add a block to registry, create new object if needed * - * @param string|\Magento\Framework\View\Element\AbstractBlock $block + * @template T of Element\BlockInterface + * + * @param class-string|T $block * @param string $name * @param string $parent * @param string $alias - * @return Element\BlockInterface + * + * @return T */ public function addBlock($block, $name = '', $parent = '', $alias = ''); @@ -252,8 +258,11 @@ public function getMessagesBlock(); /** * Get block singleton * - * @param string $type - * @return Element\BlockInterface + * @template T of Element\BlockInterface + * + * @param class-string $type + * + * @return T */ public function getBlockSingleton($type); @@ -294,7 +303,7 @@ public function isManipulationAllowed($name); * Save block in blocks registry * * @param string $name - * @param Element\BlockInterface $block + * @param Element\BlockInterface $block * @return LayoutInterface */ public function setBlock($name, $block); diff --git a/lib/internal/Magento/Framework/View/TemplateEngine/Php.php b/lib/internal/Magento/Framework/View/TemplateEngine/Php.php index ec312ba9a9171..25a0f2d749e91 100644 --- a/lib/internal/Magento/Framework/View/TemplateEngine/Php.php +++ b/lib/internal/Magento/Framework/View/TemplateEngine/Php.php @@ -1,7 +1,7 @@ $className + * + * @return T + * * @throws \LogicException */ public function helper($className) From a66a5303969d9e8fc41ea238394844de4e30578c Mon Sep 17 00:00:00 2001 From: Abhinav Pathak <51681618+engcom-Hotel@users.noreply.github.com> Date: Wed, 13 Aug 2025 19:23:34 +0530 Subject: [PATCH 2/3] Fix Static test failures Copyright failure fixes --- lib/internal/Magento/Framework/View/Element/BlockFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/View/Element/BlockFactory.php b/lib/internal/Magento/Framework/View/Element/BlockFactory.php index 7d442fe306a84..fa9fdbf736cf2 100644 --- a/lib/internal/Magento/Framework/View/Element/BlockFactory.php +++ b/lib/internal/Magento/Framework/View/Element/BlockFactory.php @@ -1,7 +1,7 @@ Date: Wed, 13 Aug 2025 21:11:28 +0200 Subject: [PATCH 3/3] Correct casing of copyright headers, to fix static test failures. --- lib/internal/Magento/Framework/View/Layout/Generator/Block.php | 2 +- lib/internal/Magento/Framework/View/LayoutInterface.php | 2 +- lib/internal/Magento/Framework/View/TemplateEngine/Php.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/View/Layout/Generator/Block.php b/lib/internal/Magento/Framework/View/Layout/Generator/Block.php index 8ffa18268cc0f..1c020ffab1429 100644 --- a/lib/internal/Magento/Framework/View/Layout/Generator/Block.php +++ b/lib/internal/Magento/Framework/View/Layout/Generator/Block.php @@ -1,7 +1,7 @@