2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php73\Rector\String_;
|
2018-10-17 01:47:21 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-10-17 07:54:48 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php73\Rector\String_\SensitiveHereNowDocRector\SensitiveHereNowDocRectorTest
|
2018-10-17 07:54:48 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SensitiveHereNowDocRector extends AbstractRector implements MinPhpVersionInterface
|
2018-10-17 01:47:21 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const WRAP_SUFFIX = '_WRAP';
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::SENSITIVE_HERE_NOW_DOC;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-17 01:47:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes heredoc/nowdoc that contains closing word to safe wrapper name', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-11-11 12:22:35 +00:00
|
|
|
$value = <<<A
|
2018-10-17 01:47:21 +00:00
|
|
|
A
|
2018-11-11 12:22:35 +00:00
|
|
|
A
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-10-17 01:47:21 +00:00
|
|
|
$value = <<<A_WRAP
|
|
|
|
A
|
|
|
|
A_WRAP
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-10-17 01:47:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-17 01:47:21 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-17 01:47:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [String_::class];
|
2018-10-17 01:47:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param String_ $node
|
2018-10-17 01:47:21 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-17 01:47:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$kind = $node->getAttribute(AttributeKey::KIND);
|
|
|
|
if (!\in_array($kind, [String_::KIND_HEREDOC, String_::KIND_NOWDOC], \true)) {
|
2018-10-17 01:47:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// the doc label is not in the string → ok
|
2018-10-17 01:56:52 +00:00
|
|
|
/** @var string $docLabel */
|
2023-01-10 11:07:38 +00:00
|
|
|
$docLabel = $node->getAttribute(AttributeKey::DOC_LABEL);
|
2021-05-29 22:10:59 +00:00
|
|
|
if (\strpos($node->value, $docLabel) === \false) {
|
2018-10-17 01:47:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-01-10 11:07:38 +00:00
|
|
|
$node->setAttribute(AttributeKey::DOC_LABEL, $this->uniquateDocLabel($node->value, $docLabel));
|
2018-10-17 01:47:21 +00:00
|
|
|
// invoke redraw
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
2018-10-17 01:47:21 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function uniquateDocLabel(string $value, string $docLabel) : string
|
2018-10-17 01:47:21 +00:00
|
|
|
{
|
|
|
|
$docLabel .= self::WRAP_SUFFIX;
|
|
|
|
$docLabelCounterTemplate = $docLabel . '_%d';
|
|
|
|
$i = 0;
|
2021-05-29 22:10:59 +00:00
|
|
|
while (\strpos($value, $docLabel) !== \false) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$docLabel = \sprintf($docLabelCounterTemplate, ++$i);
|
2018-10-17 01:47:21 +00:00
|
|
|
}
|
|
|
|
return $docLabel;
|
|
|
|
}
|
|
|
|
}
|