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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
<?php
namespace Gettext\Utils;
/** * Function parsed by PhpFunctionsScanner. */ class ParsedFunction { /** * The function name. * * @var string */ protected $name;
/** * The line where the function starts. * * @var int */ protected $line;
/** * The strings extracted from the function arguments. * * @var string[] */ protected $arguments;
/** * The current index of the function (-1 if no arguments). * * @var int|null */ protected $argumentIndex;
/** * Shall we stop adding string chunks to the current argument? * * @var bool */ protected $argumentStopped;
/** * Extracted comments. * * @var string[]|null */ protected $comments;
/** * Initializes the instance. * * @param string $name The function name. * @param int $line The line where the function starts. */ public function __construct($name, $line) { $this->name = $name; $this->line = $line; $this->arguments = []; $this->argumentIndex = -1; $this->argumentStopped = false; $this->comments = null; }
/** * Stop extracting strings from the current argument (because we found something that's not a string). */ public function stopArgument() { if ($this->argumentIndex === -1) { $this->argumentIndex = 0; } $this->argumentStopped = true; }
/** * Go to the next argument because we a comma was found. */ public function nextArgument() { if ($this->argumentIndex === -1) { // This should neve occur, but let's stay safe - During test/development an Exception should be thrown. $this->argumentIndex = 1; } else { ++$this->argumentIndex; } $this->argumentStopped = false; }
/** * Add a string to the current argument. * * @param string|null $chunk */ public function addArgumentChunk($chunk) { if ($this->argumentStopped === false) { if ($this->argumentIndex === -1) { $this->argumentIndex = 0; } if (isset($this->arguments[$this->argumentIndex])) { $this->arguments[$this->argumentIndex] .= $chunk; } else { $this->arguments[$this->argumentIndex] = $chunk; } } }
/** * Add a comment associated to this function. * * @param string $comment */ public function addComment($comment) { if ($this->comments === null) { $this->comments = []; } $this->comments[] = $comment; } /** * A closing parenthesis was found: return the final data. * The array returned has the following values: * 0 => string The function name. * 1 => int The line where the function starts. * 2 => string[] the strings extracted from the function arguments. * 3 => string[] the comments associated to the function. * * @return array */ public function close() { $arguments = []; for ($i = 0; $i <= $this->argumentIndex; ++$i) { $arguments[$i] = isset($this->arguments[$i]) ? $this->arguments[$i] : null; }
return [ $this->name, $this->line, $arguments, $this->comments, ]; } }
|