1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\FluidSchema; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use function explode; |
8
|
|
|
use function implode; |
9
|
|
|
use function strpos; |
10
|
|
|
|
11
|
|
|
class Comment |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $comment; |
17
|
|
|
|
18
|
|
|
public function __construct(string $comment) |
19
|
|
|
{ |
20
|
|
|
$this->comment = $comment; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function removeAnnotation(string $annotation): self |
24
|
|
|
{ |
25
|
|
|
$annotation = ltrim($annotation, '@'); |
26
|
|
|
$commentLines = explode("\n", $this->comment); |
27
|
|
|
$newLines = []; |
28
|
|
|
|
29
|
|
|
foreach ($commentLines as $commentLine) { |
30
|
|
View Code Duplication |
if (strpos($commentLine, '@') === 0) { |
|
|
|
|
31
|
|
|
$newCommentLine = substr($commentLine, 1); |
32
|
|
|
$annotationName = strtok($newCommentLine, " \t(\n"); |
33
|
|
|
if ($annotationName === $annotation) { |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
$newLines[] = $commentLine; |
38
|
|
|
} |
39
|
|
|
$this->comment = implode("\n", $newLines); |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function hasAnnotation(string $annotation): bool |
44
|
|
|
{ |
45
|
|
|
$annotation = ltrim($annotation, '@'); |
46
|
|
|
$commentLines = explode("\n", $this->comment); |
47
|
|
|
|
48
|
|
|
foreach ($commentLines as $commentLine) { |
49
|
|
View Code Duplication |
if (strpos($commentLine, '@') === 0) { |
|
|
|
|
50
|
|
|
$newCommentLine = substr($commentLine, 1); |
51
|
|
|
$annotationName = strtok($newCommentLine, " \t(\n"); |
52
|
|
|
if ($annotationName === $annotation) { |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $annotation |
62
|
|
|
* @param mixed $content |
63
|
|
|
* @param bool $replaceExisting |
64
|
|
|
* @return Comment |
65
|
|
|
*/ |
66
|
|
|
public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self |
67
|
|
|
{ |
68
|
|
|
if ($replaceExisting === true) { |
69
|
|
|
$this->removeAnnotation($annotation); |
70
|
|
|
} |
71
|
|
|
$annotation = ltrim($annotation, '@'); |
72
|
|
|
if ($explicitNull === true && $content === null) { |
73
|
|
|
$content = '(null)'; |
74
|
|
|
} else { |
75
|
|
|
$content = DoctrineAnnotationDumper::exportValues($content); |
76
|
|
|
} |
77
|
|
|
$this->comment .= "\n@".$annotation.$content; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getComment(): string |
82
|
|
|
{ |
83
|
|
|
return $this->comment; |
84
|
|
|
} |
85
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.