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 3 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

hostep
Copy link
Contributor

@hostep hostep commented Aug 5, 2025

…rove static analysis

Description (*)

When doing static analysis, the types defined as return type in certain methods in core Magento that have to do with creating or fetching blocks are not specific enough.
We can use generic type definitions to improve this significantly by having it interpreted as the exact class a method call returns.

More detailed information can be found in #40017

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes Add generic types in certain php classes, for improved static analysis support #40017

Manual testing scenarios (*)

  1. Temporarily change the file app/design/frontend/Magento/luma/Magento_LayeredNavigation/templates/layer/state.phtml and fully remove its contents and replace it with the following:
<?php

/** @var \Magento\Framework\View\TemplateEngine\Php $this */
/** @var \Magento\LayeredNavigation\Block\Navigation\State $block */
/** @var \Magento\Framework\View\Element\BlockFactory $blockFactory */
/** @var \Magento\Framework\View\Layout\Generator\Block $blockGenerator */

// demo of changes in \Magento\Framework\View\TemplateEngine\Php::helper
$productAlertHelper = $this->helper(\Magento\ProductAlert\Helper\Data::class);

// demo of changes in \Magento\ProductAlert\Helper\Data::createBlock
$productView = $productAlertHelper->createBlock(\Magento\ProductAlert\Block\Product\View::class);
$productView->getPostAction();

// demo of changes in \Magento\Framework\View\Element\BlockFactory::createBlock
$defaultCaptchaBlock = $blockFactory->createBlock(Magento\Captcha\Block\Captcha\DefaultCaptcha::class);
$defaultCaptchaBlock->getCaptchaModel();

// demo of changes in \Magento\Framework\View\Layout::createBlock
$rssCategoryBlock = $block->getLayout()->createBlock(\Magento\Catalog\Block\Rss\Category::class);
$rssCategoryBlock->getRssData();

// demo of changes in \Magento\Framework\View\Layout::addBlock
$rssNewProductsBlock = $block->getLayout()->addBlock(\Magento\Catalog\Block\Rss\Product\NewProducts::class);
$rssNewProductsBlock->getRssData();

// demo of changes in \Magento\Framework\View\Layout::getBlockSingleton
$rssProductSpecialBlock = $block->getLayout()->getBlockSingleton(\Magento\Catalog\Block\Rss\Product\Special::class);
$rssProductSpecialBlock->getRssData();

// demo of changes in \Magento\Framework\View\Layout\Generator\Block::createBlock
$cmsPageBlock = $blockGenerator->createBlock(\Magento\Customer\Block\Address\Renderer\DefaultRenderer::class, 'test');
$cmsPageBlock->renderArray([]);
  1. Now run phpstan with level 2 on this file: ./vendor/bin/phpstan analyse --level=2 -c dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/phpstan.neon app/design/frontend/Magento/luma/Magento_LayeredNavigation/templates/layer/state.phtml

Before:

 ------ -----------------------------------------------------------------------------------------------
  Line
 ------ -----------------------------------------------------------------------------------------------
  12     Call to an undefined method Magento\Framework\App\Helper\AbstractHelper::createBlock().
  17     Call to an undefined method Magento\Framework\View\Element\BlockInterface::getCaptchaModel().
  21     Call to an undefined method Magento\Framework\View\Element\BlockInterface::getRssData().
  25     Call to an undefined method Magento\Framework\View\Element\BlockInterface::getRssData().
  29     Call to an undefined method Magento\Framework\View\Element\BlockInterface::getRssData().
  33     Call to an undefined method Magento\Framework\View\Element\AbstractBlock::renderArray().
 ------ -----------------------------------------------------------------------------------------------

 [ERROR] Found 6 errors

After:


 [OK] No errors

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Copy link

m2-assistant bot commented Aug 5, 2025

Hi @hostep. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@ct-prd-pr-scan
Copy link

ct-prd-pr-scan bot commented Aug 5, 2025

The security team has been informed about this pull request due to the presence of risky security keywords. For security vulnerability reports, please visit Adobe's vulnerability disclosure program on HackerOne or email [email protected].

2 similar comments
@ct-prd-pr-scan
Copy link

ct-prd-pr-scan bot commented Aug 5, 2025

The security team has been informed about this pull request due to the presence of risky security keywords. For security vulnerability reports, please visit Adobe's vulnerability disclosure program on HackerOne or email [email protected].

@ct-prd-pr-scan
Copy link

ct-prd-pr-scan bot commented Aug 5, 2025

The security team has been informed about this pull request due to the presence of risky security keywords. For security vulnerability reports, please visit Adobe's vulnerability disclosure program on HackerOne or email [email protected].

@hostep
Copy link
Contributor Author

hostep commented Aug 5, 2025

@magento run all tests

@hostep
Copy link
Contributor Author

hostep commented Aug 5, 2025

@magento run Static Tests

@ct-prd-pr-scan
Copy link

ct-prd-pr-scan bot commented Aug 5, 2025

The security team has been informed about this pull request due to the presence of risky security keywords. For security vulnerability reports, please visit Adobe's vulnerability disclosure program on HackerOne or email [email protected].

@engcom-Hotel engcom-Hotel added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Aug 5, 2025
@github-project-automation github-project-automation bot moved this to Pending Review in Pull Requests Dashboard Aug 5, 2025
@engcom-Hotel
Copy link
Contributor

@magento run all tests

Copy link
Contributor

@engcom-Hotel engcom-Hotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @hostep,

Thanks for the contribution!

The changes looks good to me, but please fix the failed static tests. Functional test failures seems flaky to me, and for SVC failure, we will create a JIRA.

Thanks

@engcom-Hotel engcom-Hotel moved this from Pending Review to Changes Requested in Community Dashboard Aug 12, 2025
@hostep hostep force-pushed the introducing-generic-types-in-docblocks-for-improved-static-analysis branch from d6d3a52 to 5e7d8a6 Compare August 12, 2025 19:43
@hostep
Copy link
Contributor Author

hostep commented Aug 12, 2025

@magento run all tests

The static test failures are false positives, but I just rebase and force pushed, so let's see if they keep failing with false positive result or not ...

@hostep
Copy link
Contributor Author

hostep commented Aug 12, 2025

@engcom-Hotel, the 3 static test failures (one, two, three) all complain about missing copyright headers, but the headers are there and in the correct format, so I'm confused. Why do these test failures trigger?

Also the Database Compare test fails, even though they are all green, why is that happening?

@engcom-Hotel
Copy link
Contributor

Hello @hostep,

I think it is due to case sensitive check. Let me check once.

Thanks

@engcom-Hotel
Copy link
Contributor

@magento run Static Tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: needs update Progress: pending review Project: Community Picked PRs upvoted by the community
Projects
Status: Changes Requested
Development

Successfully merging this pull request may close these issues.

Add generic types in certain php classes, for improved static analysis support
3 participants