| 1 |
<?php declare(strict_types = 1); |
| 2 |
|
| 3 |
namespace PHPStan\PhpDocParser\Ast\ConstExpr; |
| 4 |
|
| 5 |
use PHPStan\PhpDocParser\Ast\NodeAttributes; |
| 6 |
use function addcslashes; |
| 7 |
use function assert; |
| 8 |
use function dechex; |
| 9 |
use function ord; |
| 10 |
use function preg_replace_callback; |
| 11 |
use function sprintf; |
| 12 |
use function str_pad; |
| 13 |
use function strlen; |
| 14 |
use const STR_PAD_LEFT; |
| 15 |
|
| 16 |
class ConstExprStringNode implements ConstExprNode |
| 17 |
{ |
| 18 |
|
| 19 |
public const SINGLE_QUOTED = 1; |
| 20 |
public const DOUBLE_QUOTED = 2; |
| 21 |
|
| 22 |
use NodeAttributes; |
| 23 |
|
| 24 |
public string $value; |
| 25 |
|
| 26 |
/** @var self::SINGLE_QUOTED|self::DOUBLE_QUOTED */ |
| 27 |
public $quoteType; |
| 28 |
|
| 29 |
/** |
| 30 |
* @param self::SINGLE_QUOTED|self::DOUBLE_QUOTED $quoteType |
| 31 |
*/ |
| 32 |
public function __construct(string $value, int $quoteType) |
| 33 |
{ |
| 34 |
$this->value = $value; |
| 35 |
$this->quoteType = $quoteType; |
| 36 |
} |
| 37 |
|
| 38 |
public function __toString(): string |
| 39 |
{ |
| 40 |
if ($this->quoteType === self::SINGLE_QUOTED) { |
| 41 |
// from https://github.com/nikic/PHP-Parser/blob/0ffddce52d816f72d0efc4d9b02e276d3309ef01/lib/PhpParser/PrettyPrinter/Standard.php#L1007 |
| 42 |
return sprintf("'%s'", addcslashes($this->value, '\'\\')); |
| 43 |
} |
| 44 |
|
| 45 |
// from https://github.com/nikic/PHP-Parser/blob/0ffddce52d816f72d0efc4d9b02e276d3309ef01/lib/PhpParser/PrettyPrinter/Standard.php#L1010-L1040 |
| 46 |
return sprintf('"%s"', $this->escapeDoubleQuotedString()); |
| 47 |
} |
| 48 |
|
| 49 |
private function escapeDoubleQuotedString(): string |
| 50 |
{ |
| 51 |
$quote = '"'; |
| 52 |
$escaped = addcslashes($this->value, "\n\r\t\f\v$" . $quote . '\\'); |
| 53 |
|
| 54 |
// Escape control characters and non-UTF-8 characters. |
| 55 |
// Regex based on https://stackoverflow.com/a/11709412/385378. |
| 56 |
$regex = '/( |
| 57 |
[\x00-\x08\x0E-\x1F] # Control characters |
| 58 |
| [\xC0-\xC1] # Invalid UTF-8 Bytes |
| 59 |
| [\xF5-\xFF] # Invalid UTF-8 Bytes |
| 60 |
| \xE0(?=[\x80-\x9F]) # Overlong encoding of prior code point |
| 61 |
| \xF0(?=[\x80-\x8F]) # Overlong encoding of prior code point |
| 62 |
| [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start |
| 63 |
| [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start |
| 64 |
| [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start |
| 65 |
| (?<=[\x00-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle |
| 66 |
| (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]|[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence |
| 67 |
| (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence |
| 68 |
| (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence |
| 69 |
| (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2) |
| 70 |
)/x'; |
| 71 |
return preg_replace_callback($regex, static function ($matches) { |
| 72 |
assert(strlen($matches[0]) === 1); |
| 73 |
$hex = dechex(ord($matches[0])); |
| 74 |
|
| 75 |
return '\\x' . str_pad($hex, 2, '0', STR_PAD_LEFT); |
| 76 |
}, $escaped); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param array<string, mixed> $properties |
| 81 |
*/ |
| 82 |
public static function __set_state(array $properties): self |
| 83 |
{ |
| 84 |
$instance = new self($properties['value'], $properties['quoteType']); |
| 85 |
if (isset($properties['attributes'])) { |
| 86 |
foreach ($properties['attributes'] as $key => $value) { |
| 87 |
$instance->setAttribute($key, $value); |
| 88 |
} |
| 89 |
} |
| 90 |
return $instance; |
| 91 |
} |
| 92 |
|
| 93 |
} |