Conditions | 3 |
Paths | 3 |
Total Lines | 173 |
Code Lines | 106 |
Lines | 0 |
Ratio | 0 % |
Changes | 25 | ||
Bugs | 9 | Features | 11 |
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, array('name' => 1)), |
||
|
|||
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 | ); |
||
188 | |||
189 | /** |
||
190 | * WordPress can't handle: |
||
191 | * - incorrect shortcode opening tag (blindly matches everything |
||
192 | * between opening token and closing token) |
||
193 | * - spaces between shortcode open tag and its name ([ name]), |
||
194 | * - spaces around BBCode part ([name = "bbcode"]), |
||
195 | * - escaped tokens anywhere in the arguments ([x arg=" \" "]), |
||
196 | * - configurable syntax (that's intended), |
||
197 | * - numbers in shortcode name. |
||
198 | * |
||
199 | * Tests cases from array above with identifiers in the array below must be skipped. |
||
200 | */ |
||
201 | $wordpressSkip = array(3, 6, 16, 21, 22, 23, 25, 32, 33, 34, 40); |
||
202 | $result = array(); |
||
203 | foreach($tests as $key => $test) { |
||
204 | $syntax = array_shift($test); |
||
205 | |||
206 | $result[] = array_merge(array(new RegexParser($syntax)), $test); |
||
207 | $result[] = array_merge(array(new RegularParser($syntax)), $test); |
||
208 | if(!in_array($key, $wordpressSkip)) { |
||
209 | $result[] = array_merge(array(new WordpressParser()), $test); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | return $result; |
||
214 | } |
||
215 | |||
255 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.