Completed
Pull Request — master (#166)
by Pascal
06:52
created

ParsedComment::isRelatedWith()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Gettext\Utils;
4
5
/**
6
 * Comment parsed by PhpFunctionsScanner.
7
 */
8
class ParsedComment
9
{
10
    /**
11
     * The comment itself.
12
     *
13
     * @var string
14
     */
15
    protected $comment;
16
17
    /**
18
     * The line where the comment starts.
19
     *
20
     * @var int
21
     */
22
    protected $firstLine;
23
24
    /**
25
     * The line where the comment ends.
26
     *
27
     * @var int
28
     */
29
    protected $lastLine;
30
31
    /**
32
     * Initializes the instance.
33
     *
34
     * @param string $comment The comment itself.
35
     * @param int    $firstLine The line where the comment starts.
36
     * @param int    $lastLine The line where the comment ends.
37
     */
38
    public function __construct($comment, $firstLine, $lastLine)
39
    {
40
        $this->comment = $comment;
41
        $this->firstLine = $firstLine;
42
        $this->lastLine = $lastLine;
43
    }
44
45
    /**
46
     * Create new object from raw comment data.
47
     *
48
     * @param string $value The PHP comment string.
49
     * @param int $line The line where the comment starts.
50
     *
51
     * @return self The parsed comment.
52
     */
53
    public static function create($value, $line)
54
    {
55
        $lastLine = $line + substr_count($value, "\n");
56
57
        $lines = array_map(function ($line) {
58
            if ('' === trim($line)) {
59
                return null;
60
            }
61
62
            $line = ltrim($line, '#*/ ');
63
            $line = rtrim($line, '#*/ ');
64
65
            return trim($line);
66
        }, explode("\n", $value));
67
68
        // Remove empty lines.
69
        $lines = array_filter($lines);
70
        $value = implode(' ', $lines);
71
72
        return new self($value, $line, $lastLine);
73
    }
74
75
    /**
76
     * Return the line where the comment starts.
77
     *
78
     * @return int Line number.
79
     */
80
    public function getFirstLine()
81
    {
82
        return $this->firstLine;
83
    }
84
85
    /**
86
     * Return the line where the comment ends.
87
     *
88
     * @return int Line number.
89
     */
90
    public function getLastLine()
91
    {
92
        return $this->lastLine;
93
    }
94
95
    /**
96
     * Return the actual comment string.
97
     *
98
     * @return string The comment.
99
     */
100
    public function getComment()
101
    {
102
        return $this->comment;
103
    }
104
105
    /**
106
     * Whether this comment is related with a given function.
107
     *
108
     * @param ParsedFunction $function The function to check.
109
     * @return bool Whether the comment is related or not.
110
     */
111
    public function isRelatedWith(ParsedFunction $function)
112
    {
113
        return $this->getLastLine() === $function->getLine() || $this->getLastLine() === $function->getLine() - 1;
114
    }
115
116
    /**
117
     * Whether the comment matches the required prefixes.
118
     *
119
     * @param array $prefixes An array of prefixes to check.
120
     * @return bool Whether the comment matches the prefixes or not.
121
     */
122
    public function checkPrefixes($prefixes)
123
    {
124
        if ('' === $this->comment) {
125
            return false;
126
        }
127
128
        if (empty($prefixes)) {
129
            return true;
130
        }
131
132
        foreach ($prefixes as $prefix) {
133
            if (strpos($this->comment, $prefix) === 0) {
134
                return true;
135
            }
136
        }
137
138
        return false;
139
    }
140
}
141