From 570cca8e7b8b35aa88d60f8d1400b9387c6b8bfb Mon Sep 17 00:00:00 2001 From: Navarr Date: Fri, 11 Jul 2025 17:05:27 -0400 Subject: [PATCH] Add Stubs for Magento's loosey-string behavior In many cases, Magento typedefs a `string` but truly accepts anything that can be cast to a string. This can be annoying for things like Escaper and Session Messages, which many are inclined to pass Phrase objects into. Additionally, Escaper has return types of `string|array`, passing an array if the input is an array. However, Magento's documentation blocks don't specify that's when it passes an array - so phpstan thinks that the result of Escaper can be either at any time. In that way, you cannot echo out the result of an `escapeHtml` in a PHTML file on max level without getting some errors. --- src/Magento/Framework/Escaper.php | 69 ++++++ .../Framework/Message/ManagerInterface.php | 212 ++++++++++++++++++ .../Framework/Message/MessageInterface.php | 118 ++++++++++ 3 files changed, 399 insertions(+) create mode 100644 src/Magento/Framework/Escaper.php create mode 100644 src/Magento/Framework/Message/ManagerInterface.php create mode 100644 src/Magento/Framework/Message/MessageInterface.php diff --git a/src/Magento/Framework/Escaper.php b/src/Magento/Framework/Escaper.php new file mode 100644 index 0000000..bdd7673 --- /dev/null +++ b/src/Magento/Framework/Escaper.php @@ -0,0 +1,69 @@ + $data + * @param string[]|null $allowedTags + * @return ($data is array ? string[] : string) + */ + public function escapeHtml($data, array|null $allowedTags = null) {} + + /** + * @param CastableToString $string + * @param bool $escapeSingleQuote + * @return string + */ + public function escapeHtmlAttr($string, bool $escapeSingleQuote = true) {} + + /** + * @param CastableToString $string + * @return string + */ + public function escapeUrl($string) {} + + /** + * @param CastableToString $string + * @return string + */ + public function encodeUrlParam($string) {} + + /** + * @param CastableToString $string + * @return string + */ + public function escapeJs($string) {} + + /** + * @param CastableToString $string + * @return string + */ + public function escapeCss($string) {} + + /** + * @param CastableToString[]|CastableToString $data + * @param string $quote + * @return ($data is array ? string[] : string) + */ + public function escapeJsQuote($data, string $quote = '\'') {} + + /** + * @param CastableToString $data + * @return string + */ + public function escapeXssInUrl($data) {} + + /** + * @param string $data + * @param bool $addSlashes + * @return string + */ + public function escapeQuote(string $data, bool $addSlashes = false) {} +} \ No newline at end of file diff --git a/src/Magento/Framework/Message/ManagerInterface.php b/src/Magento/Framework/Message/ManagerInterface.php new file mode 100644 index 0000000..7621b14 --- /dev/null +++ b/src/Magento/Framework/Message/ManagerInterface.php @@ -0,0 +1,212 @@ +