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

ParsedFunction::getLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Gettext\Utils;
4
5
/**
6
 * Function parsed by PhpFunctionsScanner.
7
 */
8
class ParsedFunction
9
{
10
    /**
11
     * The function name.
12
     *
13
     * @var string
14
     */
15
    protected $name;
16
17
    /**
18
     * The line where the function starts.
19
     *
20
     * @var int
21
     */
22
    protected $line;
23
24
    /**
25
     * The strings extracted from the function arguments.
26
     *
27
     * @var string[]
28
     */
29
    protected $arguments;
30
31
    /**
32
     * The current index of the function (-1 if no arguments).
33
     *
34
     * @var int|null
35
     */
36
    protected $argumentIndex;
37
38
    /**
39
     * Shall we stop adding string chunks to the current argument?
40
     *
41
     * @var bool
42
     */
43
    protected $argumentStopped;
44
45
    /**
46
     * Extracted comments.
47
     *
48
     * @var string[]|null
49
     */
50
    protected $comments;
51
52
    /**
53
     * Initializes the instance.
54
     *
55
     * @param string $name The function name.
56
     * @param int    $line The line where the function starts.
57
     */
58
    public function __construct($name, $line)
59
    {
60
        $this->name = $name;
61
        $this->line = $line;
62
        $this->arguments = [];
63
        $this->argumentIndex = -1;
64
        $this->argumentStopped = false;
65
        $this->comments = null;
66
    }
67
68
    /**
69
     * Stop extracting strings from the current argument (because we found something that's not a string).
70
     */
71
    public function stopArgument()
72
    {
73
        if ($this->argumentIndex === -1) {
74
            $this->argumentIndex = 0;
75
        }
76
        $this->argumentStopped = true;
77
    }
78
79
    /**
80
     * Go to the next argument because we a comma was found.
81
     */
82
    public function nextArgument()
83
    {
84
        if ($this->argumentIndex === -1) {
85
            // This should neve occur, but let's stay safe - During test/development an Exception should be thrown.
86
            $this->argumentIndex = 1;
87
        } else {
88
            ++$this->argumentIndex;
89
        }
90
        $this->argumentStopped = false;
91
    }
92
93
    /**
94
     * Add a string to the current argument.
95
     *
96
     * @param string|null $chunk
97
     */
98
    public function addArgumentChunk($chunk)
99
    {
100
        if ($this->argumentStopped === false) {
101
            if ($this->argumentIndex === -1) {
102
                $this->argumentIndex = 0;
103
            }
104
            if (isset($this->arguments[$this->argumentIndex])) {
105
                $this->arguments[$this->argumentIndex] .= $chunk;
106
            } else {
107
                $this->arguments[$this->argumentIndex] = $chunk;
108
            }
109
        }
110
    }
111
112
    /**
113
     * Add a comment associated to this function.
114
     *
115
     * @param string $comment
116
     */
117
    public function addComment($comment)
118
    {
119
        if ($this->comments === null) {
120
            $this->comments = [];
121
        }
122
        $this->comments[] = $comment;
123
    }
124
125
    /**
126
     * Return the line the function starts.
127
     *
128
     * @return int Line number.
129
     */
130
    public function getLine()
131
    {
132
        return $this->line;
133
    }
134
135
    /**
136
     * A closing parenthesis was found: return the final data.
137
     * The array returned has the following values:
138
     *  0 => string The function name.
139
     *  1 => int The line where the function starts.
140
     *  2 => string[] the strings extracted from the function arguments.
141
     *  3 => string[] the comments associated to the function.
142
     *
143
     * @return array
144
     */
145
    public function close()
146
    {
147
        $arguments = [];
148
        for ($i = 0; $i <= $this->argumentIndex; ++$i) {
149
            $arguments[$i] = isset($this->arguments[$i]) ? $this->arguments[$i] : null;
150
        }
151
152
        return [
153
            $this->name,
154
            $this->line,
155
            $arguments,
156
            $this->comments,
157
        ];
158
    }
159
}
160