Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit f0ea360

Browse files
committed
change matching pattern for method content
When listing jobs, the previous pattern would fail if there was any braces within the handle() method.
1 parent 478eeda commit f0ea360

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/Parser.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,33 @@ public function parseFeatureJobs(Feature $feature)
5252

5353
public function parseFunctionBody($contents, $function)
5454
{
55-
$pattern = "/function\s$function\([a-zA-Z0-9_\$\s]+\)?". // match "function handle(...)"
56-
'[\n\s]?[\t\s]*'. // regardless of the indentation preceding the {
57-
'{([^{}]*)}/'; // find everything within braces.
55+
// $pattern = "/function\s$function\([a-zA-Z0-9_\$\s,]+\)?". // match "function handle(...)"
56+
// '[\n\s]?[\t\s]*'. // regardless of the indentation preceding the {
57+
// '{([^{}]*)}/'; // find everything within braces.
58+
59+
$pattern = '~^\s*[\w\s]+\(.*\)\s*\K({((?>"[^"]*+"|\'[^\']*+\'|//.*$|/\*[\s\S]*?\*/|#.*$|<<<\s*["\']?(\w+)["\']?[^;]+\3;$|[^{}<\'"/#]++|[^{}]++|(?1))*)})~m';
60+
61+
// '~^ \s* [\w\s]+ \( .* \) \s* \K'. # how it matches a function definition
62+
// '('. # (1 start)
63+
// '{'. # opening brace
64+
// '('. # (2 start)
65+
/* '(?>'.*/ # atomic grouping (for its non-capturing purpose only)
66+
// '" [^"]*+ "'. # double quoted strings
67+
// '| \' [^\']*+ \''. # single quoted strings
68+
// '| // .* $'. # a comment block starting with //
69+
// '| /\* [\s\S]*? \*/'. # a multi line comment block /*...*/
70+
// '| \# .* $'. # a single line comment block starting with #...
71+
// '| <<< \s* ["\']?'. # heredocs and nowdocs
72+
// '( \w+ )'. # (3) ^
73+
// '["\']? [^;]+ \3 ; $'. # ^
74+
// '| [^{}<\'"/#]++'. # force engine to backtack if it encounters special characters [<'"/#] (possessive)
75+
// '| [^{}]++'. # default matching bahaviour (possessive)
76+
// '| (?1)'. # recurse 1st capturing group
77+
// ')*'. # zero to many times of atomic group
78+
// ')'. # (2 end)
79+
// '}'. # closing brace
80+
// ')~'; # (1 end)
81+
5882

5983
preg_match($pattern, $contents, $match);
6084

@@ -184,7 +208,7 @@ private function parseKeywordJobSyntax($match, $contents)
184208
} else {
185209
// nope it's just Space::class, we will figure
186210
// out the namespace from a "use" statement.
187-
$name = str_replace('::class', '', $match);
211+
$name = str_replace(['::class', ');'], '', $match);
188212
preg_match("/use\s(.*$name)/", $contents, $namespace);
189213
// it is necessary to have a \ at the beginning.
190214
$namespace = '\\'.preg_replace('/^\\\/', '', $namespace[1]);
@@ -207,7 +231,7 @@ private function parseInitJobSyntax($match, $contents)
207231
$match = str_replace('new ', '', $match);
208232

209233
// match the job's class name
210-
preg_match('/(.*Job).*\);/', $match, $name);
234+
preg_match('/(.*Job).*[\);]?/', $match, $name);
211235
$name = $name[1];
212236

213237
// Determine Namespace

0 commit comments

Comments
 (0)