-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Open
1 / 21 of 2 issues completedOpen
1 / 21 of 2 issues completed
Copy link
Labels
Area: FrameworkComponent: ObjectManagerIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P3May be fixed according to the position in the backlog.May be fixed according to the position in the backlog.Progress: ready for devReported on 2.4.5Indicates original Magento version for the Issue report.Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject itIssue related to Developer Experience and needs help with Triage to Confirm or Reject it
Description
Preconditions and environment
- Magento version: 2.4.5
- Using PHPStan with the following conditions:
- Level >= 5
- Adding
declare(strict_types=1);
at the top of php files
Steps to reproduce
- At the root of your Magento project, create a file named Test.php with the following contents:
<?php
declare(strict_types=1);
namespace Foo\Bar\Controller;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
class Test extends Action implements HttpGetActionInterface
{
public function execute()
{
$this->messageManager->addErrorMessage(__('foobar'));
return $this->resultFactory->create(Page::class);
}
}
- Run PHPStan:
vendor/bin/phpstan analyse --level 6 -c dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/phpstan.neon Test.php
Expected result
PHPStan shouldn't report any error.
Actual result
PHPStan reports the following error:
Parameter #1 $message of method Magento\Framework\Message\ManagerInterface::addErrorMessage() expects string, Magento\Framework\Phrase given.
Additional information
To fix this issue, the typehints of parameters and methods that accept Phrase objects should be changed from string
to string|Phrase
.
A few examples:
In Magento\Backend\Block\Widget\Container:
/**
* @var string
*/
protected $_headerText = 'Container Widget Header';
Must be changed to:
/**
* @var string|Phrase
*/
protected $_headerText = 'Container Widget Header';
In Magento\Backend\Block\Widget\Button\ButtonList:
/**
* Update specified button property
*
* @param string $buttonId
* @param string|null $key
* @param string $data
* @return void
*/
public function update($buttonId, $key, $data)
{
Must be changed to:
/**
* Update specified button property
*
* @param string $buttonId
* @param string|null $key
* @param string|Phrase $data
* @return void
*/
public function update($buttonId, $key, $data)
{
In Magento\Framework\Message\ManagerInterface:
/**
* Adds new error message
*
* @param string $message
* @param string|null $group
* @return ManagerInterface
*/
public function addErrorMessage($message, $group = null);
Must be changed to:
/**
* Adds new error message
*
* @param string|Phrase $message
* @param string|null $group
* @return ManagerInterface
*/
public function addErrorMessage($message, $group = null);
(same for the other addXxX methods of the same class).
In Magento\Framework\View\Page\Title:
/**
* Set page title
*
* @param string $title
* @return $this
*/
public function set($title)
{
Must be changed to:
/**
* Set page title
*
* @param string|Phrase $title
* @return $this
*/
public function set($title)
{
Other option: add Stringable interface to the phrase class and use string|Stringable
.
Release note
No response
Triage and priority
- Severity: S0 - Affects critical data or functionality and leaves users without workaround.Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
engcom-Hotel and Morgy93
Sub-issues
Collapse Sub-issuesSub-issues
- Manage this item control⌃ shift⇧ uU
- Manage this item control⌃ shift⇧ uU
To pick up a draggable item, press the space bar.
While dragging, use the arrow keys to move the item.
Press space again to drop the item in its new position, or press escape to cancel.
Metadata
Metadata
Assignees
Labels
Area: FrameworkComponent: ObjectManagerIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P3May be fixed according to the position in the backlog.May be fixed according to the position in the backlog.Progress: ready for devReported on 2.4.5Indicates original Magento version for the Issue report.Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject itIssue related to Developer Experience and needs help with Triage to Confirm or Reject it
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
m2-assistant commentedon Aug 31, 2022
Hi @guvra. Thank you for your report.
To speed up processing of this issue, make sure that you provided the following information:
Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:
@magento give me 2.4-develop instance
- upcoming 2.4.x releaseFor more details, review the Magento Contributor Assistant documentation.
Add a comment to assign the issue:
@magento I am working on this
To learn more about issue processing workflow, refer to the Code Contributions.
🕙 You can find the schedule on the Magento Community Calendar page.
📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.
✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel
guvra commentedon Sep 15, 2022
Until it is fixed, I implemented a workaround with a phpstan extension:
This workaround is not ideal, because this extension will prevent phpstan from displaying an error when a Phrase object is passed to a function that only accepts strings (e.g.
public function foo(string $text)
.m2-assistant commentedon Sep 28, 2022
Hi @engcom-Hotel. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇
1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
Details
If the issue has a valid description, the labelIssue: Format is valid
will be added to the issue automatically. Please, edit issue description if needed, until labelIssue: Format is valid
appears.2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add
Issue: Clear Description
label to the issue by yourself.3. Add
Component: XXXXX
label(s) to the ticket, indicating the components it may be related to.4. Verify that the issue is reproducible on
2.4-develop
branchDetails
- Add the comment@magento give me 2.4-develop instance
to deploy test instance on Magento infrastructure.- If the issue is reproducible on
2.4-develop
branch, please, add the labelReproduced on 2.4.x
.- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
5. Add label
Issue: Confirmed
once verification is complete.6. Make sure that automatic system confirms that report has been added to the backlog.
engcom-Hotel commentedon Sep 28, 2022
Hello @guvra,
Thanks for the report and collaboration!
We have tried to reproduce the issue in Magento 2.4-develop instance and the issue is reproducible for us with the same steps as mentioned in the main description:
Please find below the screenshot of the error for reference:
Hence confirming the issue.
Thanks
github-jira-sync-bot commentedon Sep 28, 2022
✅ Jira issue https://jira.corp.adobe.com/browse/AC-6760 is successfully created for this GitHub issue.
m2-assistant commentedon Sep 28, 2022
✅ Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.
antoniocarboni commentedon Apr 30, 2024
I temp. fix with this row on ignoreErrors on phpstan config file
'#Parameter \#1 \$data of method Magento\\Framework\\Escaper::escapeHtml\(\) expects array\|string, Magento\\Framework\\Phrase given.#'
ihor-sviziev commentedon Jan 31, 2025
TBH I would not say that phpstan is reporting "false positives", because when used strict types - it will actually fail, so that's all the real issues.
I also found that related issue + pr that fixing a small sub-set of this issue, so I added it as a sub-task to this one
PS: I wrote my thoughts on how to properly fix the issue, maybe you @guvra or @antoniocarboni can write your thoughts on it?
#39572 (review)