Completed
Pull Request — master (#102)
by Michele
02:00
created

ParsedFunction::addComment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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 = array();
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 $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 = array();
121
        }
122
        $this->comments[] = $comment;
123
    }
124
    /**
125
     * A closing parenthesis was found: return the final data.
126
     *
127
     * @return array{
0 ignored issues
show
Documentation introduced by
The doc-type array{ could not be parsed: Unknown type name "array{" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
128
     *
129
     *   @var string The function name.
130
     *   @var int The line where the function starts.
131
     *   @var string[] the strings extracted from the function arguments.
132
     *   @var string[] the comments associated to the function.
133
     * }
134
     */
135
    public function close()
136
    {
137
        $arguments = array();
138
        for ($i = 0; $i <= $this->argumentIndex; ++$i) {
139
            $arguments[$i] = isset($this->arguments[$i]) ? $this->arguments[$i] : '';
140
        }
141
142
        return array(
143
            $this->name,
144
            $this->line,
145
            $arguments,
146
            $this->comments,
147
        );
148
    }
149
}
150