Skip to content

Introducing dynamic generic type definitions in php docblocks, to imp… #40119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/code/Magento/ProductAlert/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>|T $block
*
* @return T
*
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function createBlock($block)
Expand Down
12 changes: 8 additions & 4 deletions lib/internal/Magento/Framework/View/Element/BlockFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
namespace Magento\Framework\View\Element;

Expand Down Expand Up @@ -33,9 +33,13 @@ public function __construct(ObjectManagerInterface $objectManager)
/**
* Create block
*
* @param string $blockName
* @template T of BlockInterface
*
* @param class-string<T> $blockName
* @param array $arguments
* @return \Magento\Framework\View\Element\BlockInterface
*
* @return T
*
* @throws \LogicException
*/
public function createBlock($blockName, array $arguments = [])
Expand Down
39 changes: 27 additions & 12 deletions lib/internal/Magento/Framework/View/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra
protected $appState;

/**
* @var \Psr\Log\LoggerInterface
* @var Logger
*/
protected $logger;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -785,45 +785,56 @@ public function unsetElement($name)
/**
* Block Factory
*
* @param string $type
* @template T of \Magento\Framework\View\Element\AbstractBlock
*
* @param class-string<T> $type
* @param string $name
* @param array $arguments
* @return \Magento\Framework\View\Element\AbstractBlock
*
* @return T
*/
public function createBlock($type, $name = '', array $arguments = [])
{
$this->build();
$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<T> $type
* @param string $name
* @param array $arguments
* @return \Magento\Framework\View\Element\AbstractBlock
*
* @return T
*/
protected function _createBlock($type, $name, array $arguments = [])
{
/** @var \Magento\Framework\View\Layout\Generator\Block $blockGenerator */
$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>|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 = '')
{
Expand Down Expand Up @@ -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<T> $type
*
* @return T
*
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getBlockSingleton($type)
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 15 additions & 8 deletions lib/internal/Magento/Framework/View/Layout/Generator/Block.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
namespace Magento\Framework\View\Layout\Generator;

Expand All @@ -20,7 +20,7 @@ class Block implements Layout\GeneratorInterface
/**
* Type of generator
*/
const TYPE = 'block';
public const TYPE = 'block';

/**
* @var \Magento\Framework\View\Element\BlockFactory
Expand Down Expand Up @@ -123,7 +123,7 @@ public function process(Layout\Reader\Context $readerContext, Layout\Generator\C
$scheduledStructure = $readerContext->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
Expand Down Expand Up @@ -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>|T $block
* @param string $name
* @param array $arguments
* @return \Magento\Framework\View\Element\AbstractBlock
*
* @return T
*/
public function createBlock($block, $name, array $arguments = [])
{
Expand All @@ -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>|T $block
* @param array $arguments
*
* @throws \Magento\Framework\Exception\LocalizedException
* @return \Magento\Framework\View\Element\AbstractBlock
*
* @return T
*/
protected function getBlockInstance($block, array $arguments = [])
{
Expand Down
31 changes: 20 additions & 11 deletions lib/internal/Magento/Framework/View/LayoutInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
namespace Magento\Framework\View;

Expand Down Expand Up @@ -187,21 +187,27 @@ public function getParentName($childName);
/**
* Block Factory
*
* @param string $type
* @param string $name
* @param array $arguments
* @return Element\BlockInterface
* @template T of Element\BlockInterface
*
* @param class-string<T> $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>|T $block
* @param string $name
* @param string $parent
* @param string $alias
* @return Element\BlockInterface
*
* @return T
*/
public function addBlock($block, $name = '', $parent = '', $alias = '');

Expand Down Expand Up @@ -252,8 +258,11 @@ public function getMessagesBlock();
/**
* Get block singleton
*
* @param string $type
* @return Element\BlockInterface
* @template T of Element\BlockInterface
*
* @param class-string<T> $type
*
* @return T
*/
public function getBlockSingleton($type);

Expand Down Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions lib/internal/Magento/Framework/View/TemplateEngine/Php.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand Down Expand Up @@ -121,8 +121,12 @@ public function __get($name)
/**
* Get helper singleton
*
* @param string $className
* @return \Magento\Framework\App\Helper\AbstractHelper
* @template T of \Magento\Framework\App\Helper\AbstractHelper
*
* @param class-string<T> $className
*
* @return T
*
* @throws \LogicException
*/
public function helper($className)
Expand Down