Skip to content

Commit f815154

Browse files
committed
Introducing dynamic generic type definitions in php docblocks, to improve static analysis
1 parent f39080e commit f815154

File tree

6 files changed

+84
-41
lines changed

6 files changed

+84
-41
lines changed

app/code/Magento/ProductAlert/Helper/Data.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ public function getSaveUrl($type)
110110
/**
111111
* Create block instance
112112
*
113-
* @param string|\Magento\Framework\View\Element\AbstractBlock $block
114-
* @return \Magento\Framework\View\Element\AbstractBlock
113+
* @template T of \Magento\Framework\View\Element\AbstractBlock
114+
*
115+
* @param class-string<T>|T $block
116+
*
117+
* @return T
118+
*
115119
* @throws \Magento\Framework\Exception\LocalizedException
116120
*/
117121
public function createBlock($block)

lib/internal/Magento/Framework/View/Element/BlockFactory.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All rights reserved.
55
*/
66
namespace Magento\Framework\View\Element;
77

@@ -33,9 +33,13 @@ public function __construct(ObjectManagerInterface $objectManager)
3333
/**
3434
* Create block
3535
*
36-
* @param string $blockName
36+
* @template T of BlockInterface
37+
*
38+
* @param class-string<T> $blockName
3739
* @param array $arguments
38-
* @return \Magento\Framework\View\Element\BlockInterface
40+
*
41+
* @return T
42+
*
3943
* @throws \LogicException
4044
*/
4145
public function createBlock($blockName, array $arguments = [])

lib/internal/Magento/Framework/View/Layout.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra
175175
protected $appState;
176176

177177
/**
178-
* @var \Psr\Log\LoggerInterface
178+
* @var Logger
179179
*/
180180
protected $logger;
181181

@@ -204,8 +204,8 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra
204204
* @param FrontendInterface $cache
205205
* @param ContextFactory $readerContextFactory
206206
* @param Layout\Generator\ContextFactory $generatorContextFactory
207-
* @param State $appState
208-
* @param LoggerInterface $logger
207+
* @param AppState $appState
208+
* @param Logger $logger
209209
* @param bool $cacheable
210210
* @param SerializerInterface|null $serializer
211211
* @param int|null $cacheLifetime
@@ -785,45 +785,56 @@ public function unsetElement($name)
785785
/**
786786
* Block Factory
787787
*
788-
* @param string $type
788+
* @template T of \Magento\Framework\View\Element\AbstractBlock
789+
*
790+
* @param class-string<T> $type
789791
* @param string $name
790792
* @param array $arguments
791-
* @return \Magento\Framework\View\Element\AbstractBlock
793+
*
794+
* @return T
792795
*/
793796
public function createBlock($type, $name = '', array $arguments = [])
794797
{
795798
$this->build();
796799
$name = $this->structure->createStructuralElement($name, Element::TYPE_BLOCK, $type);
797800
$block = $this->_createBlock($type, $name, $arguments);
798801
$block->setLayout($this);
802+
799803
return $block;
800804
}
801805

802806
/**
803807
* Create block and add to layout
804808
*
805-
* @param string $type
809+
* @template T of \Magento\Framework\View\Element\AbstractBlock
810+
*
811+
* @param class-string<T> $type
806812
* @param string $name
807813
* @param array $arguments
808-
* @return \Magento\Framework\View\Element\AbstractBlock
814+
*
815+
* @return T
809816
*/
810817
protected function _createBlock($type, $name, array $arguments = [])
811818
{
812819
/** @var \Magento\Framework\View\Layout\Generator\Block $blockGenerator */
813820
$blockGenerator = $this->generatorPool->getGenerator(Layout\Generator\Block::TYPE);
814821
$block = $blockGenerator->createBlock($type, $name, $arguments);
815822
$this->setBlock($name, $block);
823+
816824
return $block;
817825
}
818826

819827
/**
820828
* Add a block to registry, create new object if needed
821829
*
822-
* @param string|\Magento\Framework\View\Element\AbstractBlock $block
830+
* @template T of \Magento\Framework\View\Element\AbstractBlock
831+
*
832+
* @param class-string<T>|T $block
823833
* @param string $name
824834
* @param string $parent
825835
* @param string $alias
826-
* @return \Magento\Framework\View\Element\AbstractBlock
836+
*
837+
* @return T
827838
*/
828839
public function addBlock($block, $name = '', $parent = '', $alias = '')
829840
{
@@ -1012,8 +1023,12 @@ public function getMessagesBlock()
10121023
/**
10131024
* Get block singleton
10141025
*
1015-
* @param string $type
1016-
* @return \Magento\Framework\App\Helper\AbstractHelper
1026+
* @template T of \Magento\Framework\View\Element\AbstractBlock
1027+
*
1028+
* @param class-string<T> $type
1029+
*
1030+
* @return T
1031+
*
10171032
* @throws \Magento\Framework\Exception\LocalizedException
10181033
*/
10191034
public function getBlockSingleton($type)
@@ -1086,7 +1101,7 @@ public function executeRenderer($namespace, $staticType, $dynamicType, $data = [
10861101
$this->build();
10871102
if ($options = $this->getRendererOptions($namespace, $staticType, $dynamicType)) {
10881103
$dictionary = [];
1089-
/** @var $block \Magento\Framework\View\Element\Template */
1104+
/** @var \Magento\Framework\View\Element\Template $block */
10901105
$block = $this->createBlock($options['type'], '')
10911106
->setData($data)
10921107
->assign($dictionary)

lib/internal/Magento/Framework/View/Layout/Generator/Block.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All rights reserved.
55
*/
66
namespace Magento\Framework\View\Layout\Generator;
77

@@ -20,7 +20,7 @@ class Block implements Layout\GeneratorInterface
2020
/**
2121
* Type of generator
2222
*/
23-
const TYPE = 'block';
23+
public const TYPE = 'block';
2424

2525
/**
2626
* @var \Magento\Framework\View\Element\BlockFactory
@@ -123,7 +123,7 @@ public function process(Layout\Reader\Context $readerContext, Layout\Generator\C
123123
$scheduledStructure = $readerContext->getScheduledStructure();
124124
$layout = $generatorContext->getLayout();
125125
$structure = $generatorContext->getStructure();
126-
/** @var $blocks \Magento\Framework\View\Element\AbstractBlock[] */
126+
/** @var \Magento\Framework\View\Element\AbstractBlock[] $blocks */
127127
$blocks = [];
128128
$blockActions = [];
129129
// Instantiate blocks and collect all actions data
@@ -242,10 +242,13 @@ protected function generateBlock(
242242
/**
243243
* Create block instance
244244
*
245-
* @param string|\Magento\Framework\View\Element\AbstractBlock $block
245+
* @template T of \Magento\Framework\View\Element\AbstractBlock
246+
*
247+
* @param class-string<T>|T $block
246248
* @param string $name
247249
* @param array $arguments
248-
* @return \Magento\Framework\View\Element\AbstractBlock
250+
*
251+
* @return T
249252
*/
250253
public function createBlock($block, $name, array $arguments = [])
251254
{
@@ -259,10 +262,14 @@ public function createBlock($block, $name, array $arguments = [])
259262
/**
260263
* Create block object instance based on block type
261264
*
262-
* @param string|\Magento\Framework\View\Element\AbstractBlock $block
265+
* @template T of \Magento\Framework\View\Element\AbstractBlock
266+
*
267+
* @param class-string<T>|T $block
263268
* @param array $arguments
269+
*
264270
* @throws \Magento\Framework\Exception\LocalizedException
265-
* @return \Magento\Framework\View\Element\AbstractBlock
271+
*
272+
* @return T
266273
*/
267274
protected function getBlockInstance($block, array $arguments = [])
268275
{

lib/internal/Magento/Framework/View/LayoutInterface.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All rights reserved.
55
*/
66
namespace Magento\Framework\View;
77

@@ -187,21 +187,27 @@ public function getParentName($childName);
187187
/**
188188
* Block Factory
189189
*
190-
* @param string $type
191-
* @param string $name
192-
* @param array $arguments
193-
* @return Element\BlockInterface
190+
* @template T of Element\BlockInterface
191+
*
192+
* @param class-string<T> $type
193+
* @param string $name
194+
* @param array $arguments
195+
*
196+
* @return T
194197
*/
195198
public function createBlock($type, $name = '', array $arguments = []);
196199

197200
/**
198201
* Add a block to registry, create new object if needed
199202
*
200-
* @param string|\Magento\Framework\View\Element\AbstractBlock $block
203+
* @template T of Element\BlockInterface
204+
*
205+
* @param class-string<T>|T $block
201206
* @param string $name
202207
* @param string $parent
203208
* @param string $alias
204-
* @return Element\BlockInterface
209+
*
210+
* @return T
205211
*/
206212
public function addBlock($block, $name = '', $parent = '', $alias = '');
207213

@@ -252,8 +258,11 @@ public function getMessagesBlock();
252258
/**
253259
* Get block singleton
254260
*
255-
* @param string $type
256-
* @return Element\BlockInterface
261+
* @template T of Element\BlockInterface
262+
*
263+
* @param class-string<T> $type
264+
*
265+
* @return T
257266
*/
258267
public function getBlockSingleton($type);
259268

@@ -294,7 +303,7 @@ public function isManipulationAllowed($name);
294303
* Save block in blocks registry
295304
*
296305
* @param string $name
297-
* @param Element\BlockInterface $block
306+
* @param Element\BlockInterface $block
298307
* @return LayoutInterface
299308
*/
300309
public function setBlock($name, $block);

lib/internal/Magento/Framework/View/TemplateEngine/Php.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All rights reserved.
55
*/
66
declare(strict_types=1);
77

@@ -121,8 +121,12 @@ public function __get($name)
121121
/**
122122
* Get helper singleton
123123
*
124-
* @param string $className
125-
* @return \Magento\Framework\App\Helper\AbstractHelper
124+
* @template T of \Magento\Framework\App\Helper\AbstractHelper
125+
*
126+
* @param class-string<T> $className
127+
*
128+
* @return T
129+
*
126130
* @throws \LogicException
127131
*/
128132
public function helper($className)

0 commit comments

Comments
 (0)