-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathStringAnnotationToAttributeMapper.php
More file actions
40 lines (33 loc) · 1023 Bytes
/
StringAnnotationToAttributeMapper.php
File metadata and controls
40 lines (33 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
declare(strict_types=1);
namespace Rector\PhpAttribute\AnnotationToAttributeMapper;
use PhpParser\Node\Scalar\String_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
/**
* @implements AnnotationToAttributeMapperInterface<string>
*/
final class StringAnnotationToAttributeMapper implements AnnotationToAttributeMapperInterface
{
public function isCandidate(mixed $value): bool
{
return is_string($value);
}
/**
* @param string $value
*/
public function map($value): String_
{
if (str_contains($value, "'") && ! str_contains($value, "\n")) {
$kind = String_::KIND_DOUBLE_QUOTED;
} else {
$kind = String_::KIND_SINGLE_QUOTED;
}
if (str_starts_with($value, '"') && str_ends_with($value, '"')) {
$value = trim($value, '"');
}
return new String_($value, [
AttributeKey::KIND => $kind,
]);
}
}