Skip to content

Commit

Permalink
Fixed phpcs sniffs w.r.t to docblocks and spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek Jakhotiya committed Nov 22, 2023
1 parent 9780610 commit 5ddc480
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions Magento2/Rector/Src/AddHtmlEscaperToOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Magento2\Rector\Src;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Echo_;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -17,6 +16,9 @@
class AddHtmlEscaperToOutput extends AbstractRector
{

/**
* @var string[]
*/
private $_phpFunctionsToIgnore = ['\count','\strip_tags'];

/**
Expand Down Expand Up @@ -75,6 +77,12 @@ public function refactor(Node $node): ?Node
return null;
}

/**
* We check if content of the echo should be escaped
*
* @param Node $echoContent
* @return bool
*/
private function canEscapeOutput(Node $echoContent):bool
{
if ($echoContent instanceof Node\Expr\Variable) {
Expand Down Expand Up @@ -109,14 +117,21 @@ private function canEscapeOutput(Node $echoContent):bool
return false;
}

/**
* We do not want to escape __() output if the inner content contains html
*
* @param string $str
* @return bool
*/
private function stringContainsHtml(string $str)
{
return strlen($str) !== strlen(strip_tags($str));
}

/**
* If the developer has marked the output as noEscape by using the @noEscape
* we want to leave that code as it is
* If the developer has marked the output as noEscape by using the @noEscape we want to leave that code as it is
*
* @param Node $echoContent
*/
private function hasNoEscapeAttribute(Node $echoContent):bool
{
Expand All @@ -132,6 +147,8 @@ private function hasNoEscapeAttribute(Node $echoContent):bool

/**
* If method contains the keyword HTML we assume developer intends to output html
*
* @param Node\Expr\MethodCall $echoContent
*/
private function methodReturnsValidHtmlOrUrl(Node\Expr\MethodCall $echoContent):bool
{
Expand All @@ -140,8 +157,9 @@ private function methodReturnsValidHtmlOrUrl(Node\Expr\MethodCall $echoContent):
}

/**
* Some php function return safe output. They need not be escaped.
* count, strip_tags are example
* Some php function return safe output. count, strip_tags need not be escaped.
*
* @param Node\Expr\FuncCall $funcNode
*/
private function willFunctionReturnSafeOutput(Node\Expr\FuncCall $funcNode):bool
{
Expand Down

0 comments on commit 5ddc480

Please sign in to comment.