magento2
magento2 copied to clipboard
Unclear/invalid return types in Magento\Framework\Escaper
Summary
When running phpstan against phtml files on max level, the proper use of the Escaper can cause all sorts of errors due to the poor return types in this class.
First: Almost every method takes a value that can be cast to a string, but all declare that the type is string. This methods should document that it is a type equivalent to \Stringable|scalar instead of string.
Second: escapeHtml outputs either an array or a string, which causes typesafety concerns when having code as simple as <?= $escaper->escapeHtml($stringVariable) ?> since phpstan knows escapeHtml could return an array. To fix this, we should instruct phpstan under what circumstances an array might be returned. For example: @phpstan-return ($data is array ? string[] : string)
To fix this in my own code, I've ended up having to stub out the Escaper class (see https://github.com/bitExpert/phpstan-magento/issues/343 )
Examples
(see summary)
Proposed solution
No response
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.
- [x] 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”.
Hi @navarr. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.
- For 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.
Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: 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.
Hi @engcom-November. 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: :point_down:
- [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
- [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
- [ ] 3. Add
Area: XXXXXlabel to the ticket, indicating the functional areas it may be related to. - [ ] 4. Verify that the issue is reproducible on
2.4-developbranchDetails
- If the issue is reproducible on2.4-developbranch, 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: Confirmedonce verification is complete. - [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
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: :point_down:
- [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
- [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
- [ ] 3. Add
Area: XXXXXlabel to the ticket, indicating the functional areas it may be related to. - [ ] 4. Verify that the issue is reproducible on
2.4-developbranchDetails
- If the issue is reproducible on2.4-developbranch, 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: Confirmedonce verification is complete. - [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
Hello @navarr,
Thanks for the report and collaboration!
Please let us know the exact phpstan command you ran to scan the .phtml file.
Thanks
Hello @navarr,
Gentle reminder to look into this https://github.com/magento/magento2/issues/40012#issuecomment-3026908592.
Thank You
Dear @navarr,
This issue is being closed due to inactivity over an extended period. If the issue still persists, please feel free to reopen it or create a new ticket with the relevant details.
Thank you for your understanding.
@engcom-Hotel, here you go:
$ find . -type f -iname '*.phtml' -exec ./vendor/bin/phpstan analyse --level=5 -c dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/phpstan.neon {} + | grep -i escape | grep string
1972/1972 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
142 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
253 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
293 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int<0, max> given.
52 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
68 Parameter #1 $string of method Magento\Framework\Escaper::escapeHtmlAttr() expects string, int|null given.
20 Parameter #1 $string of method Magento\Framework\Escaper::escapeJs() expects string, int given.
22 Parameter #1 $string of method Magento\Framework\Escaper::escapeJs() expects string, int given.
25 Parameter #1 $string of method Magento\Framework\Escaper::escapeJs() expects string, float given.
74 Parameter #1 $data of method Magento\Framework\View\Element\AbstractBlock::escapeHtml() expects array|string, int given.
34 Parameter #1 $string of method Magento\Framework\Escaper::escapeHtmlAttr() expects string, (float|int) given.
36 Parameter #1 $data of method Magento\Framework\Escaper::escapeHtml() expects array|string, (float|int) given.
40 Parameter #1 $string of method Magento\Framework\Escaper::escapeHtmlAttr() expects string, (float|int) given.
38 Parameter #1 $data of method Magento\Framework\Escaper::escapeHtml() expects array|string, int given.
31 Parameter #1 $data of method Magento\Framework\Escaper::escapeHtml() expects array|string, int given.
23 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, (float|int) given.
39 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeJs() expects string, (float|int) given.
40 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, (float|int) given.
64 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, (float|int) given.
65 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, (float|int) given.
80 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, (float|int) given.
85 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, (float|int) given.
89 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, (float|int) given.
104 Parameter #1 $data of method Magento\Framework\View\Element\AbstractBlock::escapeHtml() expects array|string, float|null given.
110 Parameter #1 $data of method Magento\Framework\View\Element\AbstractBlock::escapeHtml() expects array|string, float|null given.
19 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
20 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
24 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeJs() expects string, int given.
42 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeJs() expects string, int given.
31 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int<min, -1>|int<1, max> given.
32 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int<min, -1>|int<1, max> given.
1 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
1 Parameter #1 $string of method Magento\Framework\Escaper::escapeHtmlAttr() expects string, int|null given.
3 Parameter #1 $string of method Magento\Framework\Escaper::escapeJs() expects string, int given.
5 Parameter #1 $string of method Magento\Framework\Escaper::escapeJs() expects string, int given.
8 Parameter #1 $string of method Magento\Framework\Escaper::escapeJs() expects string, float given.
1 Parameter #1 $data of method Magento\Framework\View\Element\AbstractBlock::escapeHtml() expects array|string, int given.
1 Parameter #1 $data of method Magento\Framework\Escaper::escapeHtml() expects array|string, int given.
1 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
1 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
3 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeJs() expects string, int given.
18 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeJs() expects string, int given.
If you want the filenames, remove the 2 grep's at the end and read through the entire output to find the filenames for lines matching expects string, .
Here's one:
Line app/code/Magento/Backend/view/adminhtml/templates/widget/grid/extended.phtml
142 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
253 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int given.
293 Parameter #1 $string of method Magento\Framework\View\Element\AbstractBlock::escapeHtmlAttr() expects string, int<0, max> given.
The correct fix is indeed to improve the types in phpdocs in the Escaper class for some of these escape... methods, like:
diff --git a/lib/internal/Magento/Framework/Escaper.php b/lib/internal/Magento/Framework/Escaper.php
index 9c249923197..f7f621aa5d4 100644
--- a/lib/internal/Magento/Framework/Escaper.php
+++ b/lib/internal/Magento/Framework/Escaper.php
@@ -70,7 +70,7 @@ class Escaper
* AllowedTags will not be escaped, except the following: script, img, embed,
* iframe, video, source, object, audio
*
- * @param string|array $data
+ * @param string|int|float|\Stringable|array<string|int|float|\Stringable> $data
* @param array|null $allowedTags
* @return string|array
*/
@@ -267,7 +267,7 @@ class Escaper
/**
* Escape a string for the HTML attribute context
*
- * @param string $string
+ * @param string|int|float|\Stringable $string
* @param boolean $escapeSingleQuote
* @return string
* @since 101.0.0
@@ -313,7 +313,7 @@ class Escaper
/**
* Escape string for the JavaScript context
*
- * @param string $string
+ * @param string|int|float|\Stringable $string
* @return string
* @since 101.0.0
*/
I deliberately left out bool as scalar value, as I don't know what is expected of a bool type being passed to an escape method of the output.
Thanks @hostep for the command.
The issue is reproducible for us with the given command, hence confirming the issue.
Thanks again
:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-15272 is successfully created for this GitHub issue.
:white_check_mark: 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.