1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Gettext\Scanner; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class to handle the info of a parsed comment |
8
|
|
|
*/ |
9
|
|
|
final class ParsedComment |
10
|
|
|
{ |
11
|
|
|
private $comment; |
12
|
|
|
private $filename; |
13
|
|
|
private $line; |
14
|
|
|
private $lastLine; |
15
|
|
|
|
16
|
|
|
public function __construct(string $comment, string $filename, int $line) |
17
|
|
|
{ |
18
|
|
|
$this->filename = $filename; |
19
|
|
|
$this->line = $this->lastLine = $line; |
20
|
|
|
$this->setComment($comment); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getComment(): string |
24
|
|
|
{ |
25
|
|
|
return $this->comment; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function setComment(string $comment) |
29
|
|
|
{ |
30
|
|
|
$lines = array_map(function ($line) { |
31
|
|
|
$line = ltrim($line, "#*/ \t"); |
32
|
|
|
$line = rtrim($line, "#*/ \t"); |
33
|
|
|
|
34
|
|
|
return trim($line); |
35
|
|
|
}, explode("\n", $comment)); |
36
|
|
|
|
37
|
|
|
$this->lastLine = $this->line + count($lines) - 1; |
38
|
|
|
$this->comment = trim(implode("\n", $lines)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getLine(): int |
42
|
|
|
{ |
43
|
|
|
return $this->line; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getLastLine(): int |
47
|
|
|
{ |
48
|
|
|
return $this->lastLine; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getFilename(): string |
52
|
|
|
{ |
53
|
|
|
return $this->filename; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Whether this comment is related with a given function. |
58
|
|
|
*/ |
59
|
|
|
public function isRelatedWith(ParsedFunction $function): bool |
60
|
|
|
{ |
61
|
|
|
if ($this->getFilename() !== $function->getFilename()) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->getLastLine() < $function->getLine() - 1) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($this->getLine() > $function->getLastLine()) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Whether the comment matches the required prefixes. |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function isPrefixed(array $prefixes): bool |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if ($this->getComment() === '' || empty($prefixes)) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
foreach ($prefixes as $prefix) { |
86
|
|
|
if (strpos($this->comment, $prefix) === 0) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
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.