PoInitSmarty   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 0
loc 197
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getMsgctxtArgNames() 0 4 1
A setMsgctxtArgNames() 0 4 1
A addMsgctxtArgNames() 0 4 1
A getMsgidArgNames() 0 4 1
A setMsgidArgNames() 0 4 1
A addMsgidArgNames() 0 4 1
A getMsgidPluralArgNames() 0 4 1
A setMsgidPluralArgNames() 0 4 1
A addMsgidPluralArgNames() 0 4 1
C msginitString() 0 37 12
1
<?php
2
3
namespace Geekwright\Po;
4
5
/**
6
 * PoInitPHP provides 'msginit' like logic which can take a Smarty template file,
7
 * recognize gettext like function tokens, and capture the translatable strings
8
 * in a PoFile object.
9
 *
10
 * The Smarty functions are expected to be in the format:
11
 *   funcname(msgid="message" msgid_plural="plural message" msgctxt="context")
12
 * The specifics of the function ('funcname') and the argument names ('msgid',
13
 * 'msgid_plural', and 'msgctxt') can be specified.
14
 *
15
 * A properly initialized Smarty v3 object  is required.
16
 *
17
 * @category  Extractors
18
 * @package   Po
19
 * @author    Richard Griffith <[email protected]>
20
 * @copyright 2015-2018 Richard Griffith
21
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      https://github.com/geekwright/Po
23
 */
24
class PoInitSmarty extends PoInitAbstract
25
{
26
    /**
27
     * @var \Smarty $smarty Smarty 3 object
28
     */
29
    protected $smarty = null;
30
31
    /**
32
     * @var string[] $gettextTags tags for gettext constructs, i.e. tag($msgid)
33
     */
34
    protected $gettextTags = array('gettext', '_');
35
36
    /**
37
     * @var string[] $pgettextTags tags for pgettext constructs, i.e. tag($msgctxt, $msgid)
38
     */
39
    protected $pgettextTags = array();
40
41
    /**
42
     * @var string[] $ngettextTags tags for ngettext constructs, i.e. tag($msgid, $msgid_plural)
43
     */
44
    protected $ngettextTags = array();
45
46
    /**
47
     * @var string[] $msgidArgNames names of Smarty function arguments for msgid
48
     */
49
    protected $msgidArgNames = array('msgid');
50
51
    /**
52
     * @var string[] $msgidPluralArgNames names of Smarty function arguments for msgid_plural
53
     */
54
    protected $msgidPluralArgNames = array('msgid_plural');
55
56
    /**
57
     * @var string[] $msgctxtArgNames names of Smarty function argments for msgctxt
58
     */
59
    protected $msgctxtArgNames = array('msgctxt');
60
61
    /**
62
     * Set the Smarty and PoObjects to use in the Init process
63
     *
64
     * @param \Smarty     $smarty a fully initialize Smarty 3 instance
65
     * @param PoFile|null $poFile a PoFile object to be used in msginit
66
     */
67 14
    public function __construct(\Smarty $smarty, ?PoFile $poFile = null)
68
    {
69 14
        $this->smarty = $smarty;
70 14
        $this->poFile = $poFile;
71 14
    }
72
73
    /**
74
     * getMsgctxtArgNames - get argument name(s) used for the msgctxt
75
     *
76
     * @return string[]
77
     */
78 1
    public function getMsgctxtArgNames(): array
79
    {
80 1
        return $this->msgctxtArgNames;
81
    }
82
83
    /**
84
     * Set argument name(s) used for the msgctxt
85
     *
86
     * @param string[] $argNames array of argument names to set
87
     *
88
     * @return void
89
     */
90 1
    public function setMsgctxtArgNames(array $argNames): void
91
    {
92 1
        $this->msgctxtArgNames = $argNames;
93 1
    }
94
95
    /**
96
     * Add argument name(s) used for the msgctxt
97
     *
98
     * @param string|string[] $argNames argument name(s) to add
99
     *
100
     * @return void
101
     */
102 1
    public function addMsgctxtArgNames($argNames): void
103
    {
104 1
        $this->msgctxtArgNames = array_merge($this->msgctxtArgNames, (array) $argNames);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->msgct...mes, (array) $argNames) of type array is incompatible with the declared type array<integer,string> of property $msgctxtArgNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105 1
    }
106
107
    /**
108
     * Get argument name(s) used for the msgid
109
     *
110
     * @return string[]
111
     */
112 1
    public function getMsgidArgNames(): array
113
    {
114 1
        return $this->msgidArgNames;
115
    }
116
117
    /**
118
     * Set argument name(s) used for the msgid
119
     *
120
     * @param string[] $argNames array of argument names to set
121
     *
122
     * @return void
123
     */
124 1
    public function setMsgidArgNames(array $argNames): void
125
    {
126 1
        $this->msgidArgNames = $argNames;
127 1
    }
128
129
    /**
130
     * Add argument name(s) used for the msgid
131
     *
132
     * @param string|string[] $argNames argument name(s) to add
133
     *
134
     * @return void
135
     */
136 1
    public function addMsgidArgNames($argNames): void
137
    {
138 1
        $this->msgidArgNames = array_merge($this->msgidArgNames, (array) $argNames);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->msgid...mes, (array) $argNames) of type array is incompatible with the declared type array<integer,string> of property $msgidArgNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
139 1
    }
140
141
    /**
142
     * Get argument name(s) used for the msgid_plural
143
     *
144
     * @return string[]
145
     */
146 1
    public function getMsgidPluralArgNames(): array
147
    {
148 1
        return $this->msgidPluralArgNames;
149
    }
150
151
    /**
152
     * Set argument name(s) used for the msgid_plural
153
     *
154
     * @param string[] $argNames array of argument names to set
155
     *
156
     * @return void
157
     */
158 1
    public function setMsgidPluralArgNames(array $argNames): void
159
    {
160 1
        $this->msgidPluralArgNames = $argNames;
161 1
    }
162
163
    /**
164
     * Add argument name(s) used for the msgid_plural
165
     *
166
     * @param string|string[] $argNames argument name(s) to add
167
     *
168
     * @return void
169
     */
170 1
    public function addMsgidPluralArgNames($argNames): void
171
    {
172 1
        $this->msgidPluralArgNames = array_merge($this->msgidPluralArgNames, (array) $argNames);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->msgid...mes, (array) $argNames) of type array is incompatible with the declared type array<integer,string> of property $msgidPluralArgNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
173 1
    }
174
175
    /**
176
     * Inspect the supplied source, capture gettext references as a PoFile object.
177
     *
178
     * @param string $source  php source code
179
     * @param string $refname source identification used for PO reference comments
180
     *
181
     * @return PoFile
182
     */
183 6
    public function msginitString(string $source, string $refname): PoFile
184
    {
185 6
        if (!($this->poFile instanceof PoFile)) {
186 1
            $this->poFile = new PoFile;
187
        }
188
189 6
        $tpl = $this->smarty->createTemplate('eval:'.$source);
190 6
        $tags = $this->smarty->getTags($tpl);
191
192 5
        $translateTags = array_merge($this->gettextTags, $this->pgettextTags, $this->ngettextTags);
193 5
        foreach ($tags as $tag) {
194 5
            if (in_array($tag[0], $translateTags)) {
195 5
                $entry = new PoEntry;
196 5
                $haveEntry = false;
197 5
                $entry->add(PoTokens::REFERENCE, $refname);
198 5
                foreach ($tag[1] as $temp) {
199 5
                    foreach ($temp as $key => $value) {
200 5
                        if ($value[0]=="'" || $value[0]=='"') {
201 5
                            if (in_array($key, $this->msgidArgNames)) {
202 5
                                $entry->set(PoTokens::MESSAGE, $this->escapeForPo($value));
203 5
                                $haveEntry = true;
204 3
                            } elseif (in_array($key, $this->msgidPluralArgNames)) {
205 2
                                $entry->set(PoTokens::PLURAL, $this->escapeForPo($value));
206 1
                            } elseif (in_array($key, $this->msgctxtArgNames)) {
207 5
                                $entry->set(PoTokens::CONTEXT, $this->escapeForPo($value));
208
                            }
209
                        }
210
                    }
211
                }
212 5
                if ($haveEntry) {
213 5
                    $this->checkPhpFormatFlag($entry);
214 5
                    $this->poFile->mergeEntry($entry);
215
                }
216
            }
217
        }
218 5
        return $this->poFile;
219
    }
220
}
221