Completed
Pull Request — master (#98)
by Michele
01:57
created

ParsedFunction   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 120
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A stopArgument() 0 7 2
A nextArgument() 0 10 2
A addArgumentChunk() 0 13 4
A close() 0 13 3
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
     * Initializes the instance.
47
     *
48
     * @param string $name The function name.
49
     * @param int    $line The line where the function starts.
50
     */
51
    public function __construct($name, $line)
52
    {
53
        $this->name = $name;
54
        $this->line = $line;
55
        $this->arguments = array();
56
        $this->argumentIndex = -1;
57
        $this->argumentStopped = false;
58
    }
59
60
    /**
61
     * Stop extracting strings from the current argument (because we found something that's not a string).
62
     */
63
    public function stopArgument()
64
    {
65
        if ($this->argumentIndex === -1) {
66
            $this->argumentIndex = 0;
67
        }
68
        $this->argumentStopped = true;
69
    }
70
71
    /**
72
     * Go to the next argument because we a comma was found.
73
     */
74
    public function nextArgument()
75
    {
76
        if ($this->argumentIndex === -1) {
77
            // This should neve occur, but let's stay safe - During test/development an Exception should be thrown.
78
            $this->argumentIndex = 1;
79
        } else {
80
            ++$this->argumentIndex;
81
        }
82
        $this->argumentStopped = false;
83
    }
84
85
    /**
86
     * Add a string to the current argument.
87
     *
88
     * @param string $chunk
89
     */
90
    public function addArgumentChunk($chunk)
91
    {
92
        if ($this->argumentStopped === false) {
93
            if ($this->argumentIndex === -1) {
94
                $this->argumentIndex = 0;
95
            }
96
            if (isset($this->arguments[$this->argumentIndex])) {
97
                $this->arguments[$this->argumentIndex] .= $chunk;
98
            } else {
99
                $this->arguments[$this->argumentIndex] = $chunk;
100
            }
101
        }
102
    }
103
104
    /**
105
     * A closing parenthesis was found: return the final data.
106
     *
107
     * @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...
108
     *
109
     *   @var string The function name.
110
     *   @var int The line where the function starts.
111
     *   @var string[] the strings extracted from the function arguments.
112
     * }
113
     */
114
    public function close()
115
    {
116
        $arguments = array();
117
        for ($i = 0; $i <= $this->argumentIndex; ++$i) {
118
            $arguments[$i] = isset($this->arguments[$i]) ? $this->arguments[$i] : '';
119
        }
120
121
        return array(
122
            $this->name,
123
            $this->line,
124
            $arguments,
125
        );
126
    }
127
}
128