Skip to content

Commit ae4acf1

Browse files
authored
Merge pull request #124 from iMattPro/update-events
Process simple dispatcher events too
2 parents 1795f47 + dde3c3e commit ae4acf1

File tree

6 files changed

+58
-30
lines changed

6 files changed

+58
-30
lines changed

src/Events/php_exporter.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,13 @@ public function crawl_php_file($file)
119119
$event_line = $i;
120120
$this->set_current_event($this->get_event_name($event_line, false), $event_line);
121121

122-
// Find variables of the event
123-
$arguments = $this->get_vars_from_array();
124-
$doc_vars = $this->get_vars_from_docblock();
125-
$this->validate_vars_docblock_array($arguments, $doc_vars);
122+
// Find variables of the event if it has them
123+
if (strpos($this->file_lines[$event_line], 'compact('))
124+
{
125+
$arguments = $this->get_vars_from_array();
126+
$doc_vars = $this->get_vars_from_docblock();
127+
$this->validate_vars_docblock_array($arguments, $doc_vars);
128+
}
126129
}
127130
else
128131
{
@@ -185,20 +188,8 @@ public function get_event_name($event_line, $is_dispatch)
185188
$event_text_line = $this->file_lines[$event_line];
186189
$event_text_line = ltrim($event_text_line, " \t");
187190

188-
if ($is_dispatch)
189-
{
190-
$regex = '#\$([a-z](?:[a-z0-9_]|->)*)';
191-
$regex .= '->dispatch\(';
192-
$regex .= '\'%s\'';
193-
$regex .= '\);#';
194-
}
195-
else
196-
{
197-
$regex = '#extract\(\$([a-z](?:[a-z0-9_]|->)*)';
198-
$regex .= '->trigger_event\(';
199-
$regex .= '\'%s\'';
200-
$regex .= ', compact\(\$vars\)\)\);#';
201-
}
191+
$event = $is_dispatch ? 'dispatch' : 'trigger_event';
192+
$regex = '/->(?:' . $event . ')\(([\'"])%s\1/';
202193

203194
$match = array();
204195
preg_match(sprintf($regex, $this->preg_match_event_name()), $event_text_line, $match);

tests/events/invalid_name_simple.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Modify the data for post submitting
4+
*
5+
* @event rxu.postsmerging.posts_merging_end
6+
* @since 2.0.0
7+
*/
8+
$phpbb_dispatcher->trigger_event('rxu.PostsMerging.posts_merging_end');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Modify the data for post submitting
4+
*
5+
* @event rxu.postsmerging.posts_merging_end
6+
* @since 2.0.0
7+
*/
8+
$phpbb_dispatcher->dispatch('rxu.PostsMerging.posts_merging_end');

tests/events/valid_name_simple.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Modify the data for post submitting
4+
*
5+
* @event rxu.postsmerging.posts_merging_end
6+
* @since 2.0.0
7+
*/
8+
$phpbb_dispatcher->trigger_event('rxu.postsmerging.posts_merging_end');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Modify the data for post submitting
4+
*
5+
* @event rxu.postsmerging.posts_merging_end
6+
* @since 2.0.0
7+
*/
8+
$phpbb_dispatcher->dispatch('rxu.postsmerging.posts_merging_end');

tests/php_exporter_test.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,34 @@ public function extension_data()
3434
];
3535

3636
return [
37-
[27, './tests/events/invalid_name_long_multi.php', 'rxu.PostsMerging.posts_merging_end', $expected_vars, $expected_errors],
38-
[17, './tests/events/invalid_name_long_single.php', 'rxu.PostsMerging.posts_merging_end', $expected_vars, $expected_errors],
39-
[27, './tests/events/invalid_name_short_multi.php', 'rxu.PostsMerging.posts_merging_end', $expected_vars, $expected_errors],
40-
[17, './tests/events/invalid_name_short_single.php', 'rxu.PostsMerging.posts_merging_end', $expected_vars, $expected_errors],
41-
[27, './tests/events/valid_name_long_multi.php', 'rxu.postsmerging.posts_merging_end', $expected_vars],
42-
[17, './tests/events/valid_name_long_single.php', 'rxu.postsmerging.posts_merging_end', $expected_vars],
43-
[27, './tests/events/valid_name_short_multi.php', 'rxu.postsmerging.posts_merging_end', $expected_vars],
44-
[17, './tests/events/valid_name_short_single.php', 'rxu.postsmerging.posts_merging_end', $expected_vars],
37+
[27, './tests/events/invalid_name_long_multi.php', false, 'rxu.PostsMerging.posts_merging_end', $expected_vars, $expected_errors],
38+
[17, './tests/events/invalid_name_long_single.php', false, 'rxu.PostsMerging.posts_merging_end', $expected_vars, $expected_errors],
39+
[27, './tests/events/invalid_name_short_multi.php', false, 'rxu.PostsMerging.posts_merging_end', $expected_vars, $expected_errors],
40+
[17, './tests/events/invalid_name_short_single.php', false, 'rxu.PostsMerging.posts_merging_end', $expected_vars, $expected_errors],
41+
[7, './tests/events/invalid_name_simple.php', false, 'rxu.PostsMerging.posts_merging_end', [], $expected_errors],
42+
[7, './tests/events/invalid_name_simple_dispatch.php', true, 'rxu.PostsMerging.posts_merging_end', [], $expected_errors],
43+
[27, './tests/events/valid_name_long_multi.php', false, 'rxu.postsmerging.posts_merging_end', $expected_vars],
44+
[17, './tests/events/valid_name_long_single.php', false, 'rxu.postsmerging.posts_merging_end', $expected_vars],
45+
[27, './tests/events/valid_name_short_multi.php', false, 'rxu.postsmerging.posts_merging_end', $expected_vars],
46+
[17, './tests/events/valid_name_short_single.php', false, 'rxu.postsmerging.posts_merging_end', $expected_vars],
47+
[7, './tests/events/valid_name_simple.php', false, 'rxu.postsmerging.posts_merging_end', []],
48+
[7, './tests/events/valid_name_simple_dispatch.php', true, 'rxu.postsmerging.posts_merging_end', []],
4549
];
4650
}
4751

4852
/**
4953
* @dataProvider extension_data
5054
*/
51-
public function test_event_name($line, $content, $expected_name, $expected_vars, $expected_errors = [])
55+
public function test_event_name($line, $file, $is_dispatch, $expected_name, $expected_vars, $expected_errors = [])
5256
{
57+
$content = file_get_contents($file);
5358
$output = new Output();
5459
$exporter = new php_exporter($output, '');
55-
$exporter->set_content(explode("\n", file_get_contents($content)));
60+
$exporter->set_content(explode("\n", $content));
5661

57-
$name = $exporter->get_event_name($line, false);
62+
$name = $exporter->get_event_name($line, $is_dispatch);
5863
$exporter->set_current_event($name, $line);
59-
$vars = $exporter->get_vars_from_array(false);
64+
$vars = strpos($content, 'compact(') ? $exporter->get_vars_from_array(false) : [];
6065

6166
self::assertEquals($expected_name, $name);
6267
self::assertEquals($expected_vars, $vars);

0 commit comments

Comments
 (0)