Skip to content

Commit 8fb1e13

Browse files
author
Abhishek Jakhotiya
committed
This rule escapes output using ->escapeHtml output function. It should be run only in phtml files because right now it will do this for whichever php file you provide. That is untested. You can take look at associated tests for which scenarios are covered. escapeJs and escapeUrl are yet to be implemented
1 parent 6cbe6a3 commit 8fb1e13

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright 2021 Adobe
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Rector\Src;
9+
10+
use PhpParser\Node;
11+
use PhpParser\Node\Stmt\Class_;
12+
use PhpParser\Node\Stmt\Echo_;
13+
use Rector\Core\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
17+
class AddHtmlEscaperToOutput extends AbstractRector
18+
{
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function getNodeTypes(): array
23+
{
24+
return [Echo_::class];
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function refactor(Node $node): ?Node
31+
{
32+
// if the echo already has escapeHtml called on $block or escaper, don't do anthing
33+
34+
$echoContent = $node->exprs[0];
35+
36+
if($echoContent instanceof Node\Expr\Variable || $echoContent instanceof Node\Expr\FuncCall){
37+
$node->exprs[0] = new Node\Expr\MethodCall(new Node\Expr\Variable('escaper'),'escapeHtml',[new Node\Arg($echoContent)]);
38+
return $node;
39+
}
40+
41+
if($echoContent instanceof Node\Expr\MethodCall && $echoContent->name != 'escapeHtml'){
42+
43+
$node->exprs[0] = new Node\Expr\MethodCall(new Node\Expr\Variable('escaper'),'escapeHtml',[new Node\Arg($echoContent)]);
44+
return $node;
45+
}
46+
47+
return null;
48+
}
49+
50+
/**
51+
* @inheritDoc
52+
*/
53+
public function getRuleDefinition(): RuleDefinition
54+
{
55+
return new RuleDefinition(
56+
'Add escaper methods like escapeHtml to html output',
57+
[
58+
new CodeSample(
59+
'echo $productName',
60+
'echo $escaper->escapeHtml($productName)'
61+
),
62+
]
63+
);
64+
}
65+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright 2021 Adobe
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Rector\Tests\AddHtmlEscaperToOutput;
9+
10+
use Iterator;
11+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
12+
use Symplify\SmartFileSystem\SmartFileInfo;
13+
14+
class AddHtmlEscaperToOutputTest extends AbstractRectorTestCase
15+
{
16+
/**
17+
* @dataProvider provideData()
18+
*/
19+
public function test(string $fileInfo): void
20+
{
21+
$this->doTestFile($fileInfo);
22+
}
23+
24+
/**
25+
* @return Iterator<SmartFileInfo>
26+
*/
27+
public function provideData(): Iterator
28+
{
29+
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
30+
}
31+
32+
public function provideConfigFilePath(): string
33+
{
34+
return __DIR__ . '/config/configured_rule.php';
35+
}
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
echo $escaper->escapeHtml($productName);
4+
5+
?>
6+
-----
7+
<?php
8+
9+
echo $escaper->escapeHtml($productName);
10+
11+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
echo $customer->getName();
4+
echo __('Something');
5+
?>
6+
-----
7+
<?php
8+
9+
echo $escaper->escapeHtml($customer->getName());
10+
echo $escaper->escapeHtml(__('Something'));
11+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
echo $productName;
4+
5+
?>
6+
-----
7+
<?php
8+
9+
echo $escaper->escapeHtml($productName);
10+
11+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Adobe
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento2\Rector\Src\AddArrayAccessInterfaceReturnTypes;
9+
use Rector\Config\RectorConfig;
10+
11+
return static function (RectorConfig $rectorConfig): void {
12+
$rectorConfig->rule(\Magento2\Rector\Src\AddHtmlEscaperToOutput::class);
13+
};

0 commit comments

Comments
 (0)