Conditions | 3 |
Paths | 3 |
Total Lines | 220 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
42 | public function provideShortcodes() |
||
43 | { |
||
44 | $s = new CommonSyntax(); |
||
45 | |||
46 | $tests = array( |
||
47 | // invalid |
||
48 | array($s, '', array()), |
||
49 | array($s, '[]', array()), |
||
50 | array($s, '', array()), |
||
51 | array($s, 'x html([a. title][, alt][, classes]) x', array()), |
||
52 | array($s, '[/y]', array()), |
||
53 | array($s, '[sc', array()), |
||
54 | array($s, '[sc / [/sc]', array()), |
||
55 | array($s, '[sc arg="val', array()), |
||
56 | |||
57 | // single shortcodes |
||
58 | array($s, '[sc]', array( |
||
59 | new ParsedShortcode(new Shortcode('sc', array(), null), '[sc]', 0), |
||
60 | )), |
||
61 | array($s, '[sc arg=val]', array( |
||
62 | new ParsedShortcode(new Shortcode('sc', array('arg' => 'val'), null), '[sc arg=val]', 0), |
||
63 | )), |
||
64 | array($s, '[sc novalue arg="complex value"]', array( |
||
65 | new ParsedShortcode(new Shortcode('sc', array('novalue' => null, 'arg' => 'complex value'), null), '[sc novalue arg="complex value"]', 0), |
||
66 | )), |
||
67 | array($s, '[sc x="ąćęłńóśżź ĄĆĘŁŃÓŚŻŹ"]', array( |
||
68 | new ParsedShortcode(new Shortcode('sc', array('x' => 'ąćęłńóśżź ĄĆĘŁŃÓŚŻŹ'), null), '[sc x="ąćęłńóśżź ĄĆĘŁŃÓŚŻŹ"]', 0), |
||
69 | )), |
||
70 | array($s, '[sc x="multi'."\n".'line"]', array( |
||
71 | new ParsedShortcode(new Shortcode('sc', array('x' => 'multi'."\n".'line'), null), '[sc x="multi'."\n".'line"]', 0), |
||
72 | )), |
||
73 | array($s, '[sc noval x="val" y]content[/sc]', array( |
||
74 | new ParsedShortcode(new Shortcode('sc', array('noval' => null, 'x' => 'val', 'y' => null), 'content'), '[sc noval x="val" y]content[/sc]', 0), |
||
75 | )), |
||
76 | array($s, '[sc x="{..}"]', array( |
||
77 | new ParsedShortcode(new Shortcode('sc', array('x' => '{..}'), null), '[sc x="{..}"]', 0), |
||
78 | )), |
||
79 | array($s, '[sc a="x y" b="x" c=""]', array( |
||
80 | new ParsedShortcode(new Shortcode('sc', array('a' => 'x y', 'b' => 'x', 'c' => ''), null), '[sc a="x y" b="x" c=""]', 0), |
||
81 | )), |
||
82 | array($s, '[sc a="a \"\" b"]', array( |
||
83 | new ParsedShortcode(new Shortcode('sc', array('a' => 'a \"\" b'), null), '[sc a="a \"\" b"]', 0), |
||
84 | )), |
||
85 | array($s, '[sc/]', array( |
||
86 | new ParsedShortcode(new Shortcode('sc', array(), null), '[sc/]', 0), |
||
87 | )), |
||
88 | array($s, '[sc /]', array( |
||
89 | new ParsedShortcode(new Shortcode('sc', array(), null), '[sc /]', 0), |
||
90 | )), |
||
91 | array($s, '[sc arg=val cmp="a b"/]', array( |
||
92 | new ParsedShortcode(new Shortcode('sc', array('arg' => 'val', 'cmp' => 'a b'), null), '[sc arg=val cmp="a b"/]', 0), |
||
93 | )), |
||
94 | array($s, '[sc x y /]', array( |
||
95 | new ParsedShortcode(new Shortcode('sc', array('x' => null, 'y' => null), null), '[sc x y /]', 0), |
||
96 | )), |
||
97 | array($s, '[sc x="\ " /]', array( |
||
98 | new ParsedShortcode(new Shortcode('sc', array('x' => '\ '), null), '[sc x="\ " /]', 0), |
||
99 | )), |
||
100 | array($s, '[ sc x = "\ " y = value z / ]', array( |
||
101 | new ParsedShortcode(new Shortcode('sc', array('x' => '\ ', 'y' => 'value', 'z' => null), null), '[ sc x = "\ " y = value z / ]', 0), |
||
102 | )), |
||
103 | array($s, '[ sc x= "\ " y =value ] vv [ / sc ]', array( |
||
104 | new ParsedShortcode(new Shortcode('sc', array('x' => '\ ', 'y' => 'value'), ' vv '), '[ sc x= "\ " y =value ] vv [ / sc ]', 0), |
||
105 | )), |
||
106 | array($s, '[sc url="http://giggle.com/search" /]', array( |
||
107 | new ParsedShortcode(new Shortcode('sc', array('url' => 'http://giggle.com/search'), null), '[sc url="http://giggle.com/search" /]', 0), |
||
108 | )), |
||
109 | |||
110 | // bbcode |
||
111 | array($s, '[sc = "http://giggle.com/search" /]', array( |
||
112 | new ParsedShortcode(new Shortcode('sc', array(), null, 'http://giggle.com/search'), '[sc = "http://giggle.com/search" /]', 0), |
||
113 | )), |
||
114 | |||
115 | // multiple shortcodes |
||
116 | array($s, 'Lorem [ipsum] random [code-code arg=val] which is here', array( |
||
117 | new ParsedShortcode(new Shortcode('ipsum', array(), null), '[ipsum]', 6), |
||
118 | new ParsedShortcode(new Shortcode('code-code', array('arg' => 'val'), null), '[code-code arg=val]', 21), |
||
119 | )), |
||
120 | array($s, 'x [aa] x [aa] x', array( |
||
121 | new ParsedShortcode(new Shortcode('aa', array(), null), '[aa]', 2), |
||
122 | new ParsedShortcode(new Shortcode('aa', array(), null), '[aa]', 9), |
||
123 | )), |
||
124 | array($s, 'x [x]a[/x] x [x]a[/x] x', array( |
||
125 | new ParsedShortcode(new Shortcode('x', array(), 'a'), '[x]a[/x]', 2), |
||
126 | new ParsedShortcode(new Shortcode('x', array(), 'a'), '[x]a[/x]', 13), |
||
127 | )), |
||
128 | array($s, 'x [x x y=z a="b c"]a[/x] x [x x y=z a="b c"]a[/x] x', array( |
||
129 | new ParsedShortcode(new Shortcode('x', array('x' => null, 'y' => 'z', 'a' => 'b c'), 'a'), '[x x y=z a="b c"]a[/x]', 2), |
||
130 | new ParsedShortcode(new Shortcode('x', array('x' => null, 'y' => 'z', 'a' => 'b c'), 'a'), '[x x y=z a="b c"]a[/x]', 27), |
||
131 | )), |
||
132 | array($s, 'x [code /] y [code]z[/code] x [code] y [code/] a', array( |
||
133 | new ParsedShortcode(new Shortcode('code', array(), null), '[code /]', 2), |
||
134 | new ParsedShortcode(new Shortcode('code', array(), 'z'), '[code]z[/code]', 13), |
||
135 | new ParsedShortcode(new Shortcode('code', array(), null), '[code]', 30), |
||
136 | new ParsedShortcode(new Shortcode('code', array(), null), '[code/]', 39), |
||
137 | )), |
||
138 | array($s, 'x [code arg=val /] y [code cmp="xx"/] x [code x=y/] a', array( |
||
139 | new ParsedShortcode(new Shortcode('code', array('arg' => 'val'), null), '[code arg=val /]', 2), |
||
140 | new ParsedShortcode(new Shortcode('code', array('cmp' => 'xx'), null), '[code cmp="xx"/]', 21), |
||
141 | new ParsedShortcode(new Shortcode('code', array('x' => 'y'), null), '[code x=y/]', 40), |
||
142 | )), |
||
143 | array($s, 'x [ code arg=val /]a[ code/]c[x / ] m [ y ] c [ / y]', array( |
||
144 | new ParsedShortcode(new Shortcode('code', array('arg' => 'val'), null), '[ code arg=val /]', 2), |
||
145 | new ParsedShortcode(new Shortcode('code', array(), null), '[ code/]', 23), |
||
146 | new ParsedShortcode(new Shortcode('x', array(), null), '[x / ]', 32), |
||
147 | new ParsedShortcode(new Shortcode('y', array(), ' c '), '[ y ] c [ / y]', 47), |
||
148 | )), |
||
149 | |||
150 | // other syntax |
||
151 | array(new Syntax('[[', ']]', '//', '==', '""'), '[[code arg==""val oth""]]cont[[//code]]', array( |
||
152 | new ParsedShortcode(new Shortcode('code', array('arg' => 'val oth'), 'cont'), '[[code arg==""val oth""]]cont[[//code]]', 0), |
||
153 | )), |
||
154 | array(new Syntax('^', '$', '&', '!!!', '@@'), '^code a!!!@@\"\"@@ b!!!@@x\"y@@ c$cnt^&code$', array( |
||
155 | new ParsedShortcode(new Shortcode('code', array('a' => '\"\"', 'b' => 'x\"y', 'c' => null), 'cnt'), '^code a!!!@@\"\"@@ b!!!@@x\"y@@ c$cnt^&code$', 0), |
||
156 | )), |
||
157 | |||
158 | // UTF-8 sequences |
||
159 | array($s, '’’’’[sc]’’[sc]', array( |
||
160 | new ParsedShortcode(new Shortcode('sc', array(), null), '[sc]', 4), |
||
161 | new ParsedShortcode(new Shortcode('sc', array(), null), '[sc]', 10), |
||
162 | )), |
||
163 | |||
164 | // performance |
||
165 | // array($s, 'x [[aa]] y', array()), |
||
166 | array($s, str_repeat('[a]', 20), array_map(function($offset) { // 20 |
||
167 | return new ParsedShortcode(new Shortcode('a', array(), null), '[a]', $offset); |
||
168 | }, range(0, 57, 3))), |
||
169 | array($s, '[b][a]x[a][/a][/a][/b] [b][a][a][/a]y[/a][/b]', array( |
||
170 | new ParsedShortcode(new Shortcode('b', array(), '[a]x[a][/a][/a]'), '[b][a]x[a][/a][/a][/b]', 0), |
||
171 | new ParsedShortcode(new Shortcode('b', array(), '[a][a][/a]y[/a]'), '[b][a][a][/a]y[/a][/b]', 23), |
||
172 | )), |
||
173 | array($s, '[b] [a][a][a] [/b] [b] [a][a][a] [/b]', array( |
||
174 | new ParsedShortcode(new Shortcode('b', array(), ' [a][a][a] '), '[b] [a][a][a] [/b]', 0), |
||
175 | new ParsedShortcode(new Shortcode('b', array(), ' [a][a][a] '), '[b] [a][a][a] [/b]', 19), |
||
176 | )), |
||
177 | array($s, '[name]random[/other]', array( |
||
178 | new ParsedShortcode(new Shortcode('name', array(), null), '[name]', 0), |
||
179 | )), |
||
180 | array($s, '[0][1][2][3]', array( |
||
181 | new ParsedShortcode(new Shortcode('0', array(), null), '[0]', 0), |
||
182 | new ParsedShortcode(new Shortcode('1', array(), null), '[1]', 3), |
||
183 | new ParsedShortcode(new Shortcode('2', array(), null), '[2]', 6), |
||
184 | new ParsedShortcode(new Shortcode('3', array(), null), '[3]', 9), |
||
185 | )), |
||
186 | array($s, '[_][na_me][_name][name_][n_am_e][_n_]', array( |
||
187 | new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0), |
||
188 | new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3), |
||
189 | new ParsedShortcode(new Shortcode('_name', array(), null), '[_name]', 10), |
||
190 | new ParsedShortcode(new Shortcode('name_', array(), null), '[name_]', 17), |
||
191 | new ParsedShortcode(new Shortcode('n_am_e', array(), null), '[n_am_e]', 24), |
||
192 | new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32), |
||
193 | )), |
||
194 | array($s, '[x]/[/x] [x]"[/x] [x]=[/x] [x]][/x] [x] [/x] [x]x[/x]', array( |
||
195 | new ParsedShortcode(new Shortcode('x', array(), '/'), '[x]/[/x]', 0), |
||
196 | new ParsedShortcode(new Shortcode('x', array(), '"'), '[x]"[/x]', 9), |
||
197 | new ParsedShortcode(new Shortcode('x', array(), '='), '[x]=[/x]', 18), |
||
198 | new ParsedShortcode(new Shortcode('x', array(), ']'), '[x]][/x]', 27), |
||
199 | new ParsedShortcode(new Shortcode('x', array(), ' '), '[x] [/x]', 36), |
||
200 | new ParsedShortcode(new Shortcode('x', array(), 'x'), '[x]x[/x]', 45), |
||
201 | )), |
||
202 | array($s, '[a]0[/a]', array( |
||
203 | new ParsedShortcode(new Shortcode('a', array(), '0'), '[a]0[/a]', 0), |
||
204 | )), |
||
205 | array($s, '[fa icon=fa-camera /] [fa icon=fa-camera extras=fa-4x /]', array( |
||
206 | new ParsedShortcode(new Shortcode('fa', array('icon' => 'fa-camera'), null), '[fa icon=fa-camera /]', 0), |
||
207 | new ParsedShortcode(new Shortcode('fa', array('icon' => 'fa-camera', 'extras' => 'fa-4x'), null), '[fa icon=fa-camera extras=fa-4x /]', 22), |
||
208 | )), |
||
209 | array($s, '[fa icon=fa-circle-o-notch extras=fa-spin,fa-3x /]', array( |
||
210 | new ParsedShortcode(new Shortcode('fa', array('icon' => 'fa-circle-o-notch', 'extras' => 'fa-spin,fa-3x'), null), '[fa icon=fa-circle-o-notch extras=fa-spin,fa-3x /]', 0), |
||
211 | )), |
||
212 | array($s, '[z =]', array()), |
||
213 | array($s, '[x=#F00 one=#F00 two="#F00"]', array( |
||
214 | new ParsedShortcode(new Shortcode('x', array('one' => '#F00', 'two' => '#F00'), null, '#F00'), '[x=#F00 one=#F00 two="#F00"]', 0), |
||
215 | )), |
||
216 | array($s, '[*] [* xyz arg=val]', array( |
||
217 | new ParsedShortcode(new Shortcode('*', array(), null, null), '[*]', 0), |
||
218 | new ParsedShortcode(new Shortcode('*', array('xyz' => null, 'arg' => 'val'), null, null), '[* xyz arg=val]', 4), |
||
219 | )), |
||
220 | array($s, '[*=bb x=y]cnt[/*]', array( |
||
221 | new ParsedShortcode(new Shortcode('*', array('x' => 'y'), 'cnt', 'bb'), '[*=bb x=y]cnt[/*]', 0), |
||
222 | )), |
||
223 | array($s, '[ [] ] [x] [ ] [/x] ] [] [ [y] ] [] [ [z] [/#] [/z] [ [] ] [/] [/y] ] [z] [ [/ [/] /] ] [/z]', array( |
||
224 | new ParsedShortcode(new Shortcode('x', array(), ' [ ] ', null), '[x] [ ] [/x]', 7), |
||
225 | new ParsedShortcode(new Shortcode('y', array(), ' ] [] [ [z] [/#] [/z] [ [] ] [/] ', null), '[y] ] [] [ [z] [/#] [/z] [ [] ] [/] [/y]', 27), |
||
226 | new ParsedShortcode(new Shortcode('z', array(), ' [ [/ [/] /] ] ', null), '[z] [ [/ [/] /] ] [/z]', 70), |
||
227 | )), |
||
228 | array($s, '[x=/[/] [y a=/"//] [z=http://url/] [a=http://url ]', array( |
||
229 | new ParsedShortcode(new Shortcode('x', array(), null, '/['), '[x=/[/]', 0), |
||
230 | new ParsedShortcode(new Shortcode('y', array('a' => '/"/'), null, null), '[y a=/"//]', 8), |
||
231 | new ParsedShortcode(new Shortcode('z', array(), null, 'http://url'), '[z=http://url/]', 19), |
||
232 | new ParsedShortcode(new Shortcode('a', array(), null, 'http://url'), '[a=http://url ]', 35), |
||
233 | )), |
||
234 | ); |
||
235 | |||
236 | /** |
||
237 | * WordPress can't handle: |
||
238 | * - incorrect shortcode opening tag (blindly matches everything |
||
239 | * between opening token and closing token) |
||
240 | * - spaces between shortcode open tag and its name ([ name]), |
||
241 | * - spaces around BBCode part ([name = "bbcode"]), |
||
242 | * - escaped tokens anywhere in the arguments ([x arg=" \" "]), |
||
243 | * - configurable syntax (that's intended), |
||
244 | * - numbers in shortcode name. |
||
245 | * |
||
246 | * Tests cases from array above with identifiers in the array below must be skipped. |
||
247 | */ |
||
248 | $wordpressSkip = array(3, 6, 16, 21, 22, 23, 25, 32, 33, 34, 46, 47, 49, 51); |
||
249 | $result = array(); |
||
250 | foreach($tests as $key => $test) { |
||
251 | $syntax = array_shift($test); |
||
252 | |||
253 | $result[] = array_merge(array(new RegexParser($syntax)), $test); |
||
254 | $result[] = array_merge(array(new RegularParser($syntax)), $test); |
||
255 | if(!in_array($key, $wordpressSkip, true)) { |
||
256 | $result[] = array_merge(array(new WordpressParser()), $test); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | return $result; |
||
261 | } |
||
262 | |||
330 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.