Debian Codegraph @

vendor/phpstan/phpdoc-parser/src/Ast/ConstExpr/DoctrineConstExprStringNode.php

raw
1 <?php declare(strict_types = 1);
2
3 namespace PHPStan\PhpDocParser\Ast\ConstExpr;
4
5 use PHPStan\PhpDocParser\Ast\NodeAttributes;
6 use function sprintf;
7 use function str_replace;
8 use function strlen;
9 use function substr;
10
11 class DoctrineConstExprStringNode extends ConstExprStringNode
12 {
13
14 use NodeAttributes;
15
16 public string $value;
17
18 public function __construct(string $value)
19 {
20 parent::__construct($value, self::DOUBLE_QUOTED);
21 $this->value = $value;
22 }
23
24 public function __toString(): string
25 {
26 return self::escape($this->value);
27 }
28
29 public static function unescape(string $value): string
30 {
31 // from https://github.com/doctrine/annotations/blob/a9ec7af212302a75d1f92fa65d3abfbd16245a2a/lib/Doctrine/Common/Annotations/DocLexer.php#L103-L107
32 return str_replace('""', '"', substr($value, 1, strlen($value) - 2));
33 }
34
35 private static function escape(string $value): string
36 {
37 // from https://github.com/phpstan/phpdoc-parser/issues/205#issuecomment-1662323656
38 return sprintf('"%s"', str_replace('"', '""', $value));
39 }
40
41 /**
42 * @param array<string, mixed> $properties
43 */
44 public static function __set_state(array $properties): self
45 {
46 $instance = new self($properties['value']);
47 if (isset($properties['attributes'])) {
48 foreach ($properties['attributes'] as $key => $value) {
49 $instance->setAttribute($key, $value);
50 }
51 }
52 return $instance;
53 }
54
55 }