From f0ea360c8d0370be005b45704a10f3958db4b7ab Mon Sep 17 00:00:00 2001 From: Abed Halawi Date: Thu, 30 Jun 2016 14:22:30 +0300 Subject: [PATCH] change matching pattern for method content When listing jobs, the previous pattern would fail if there was any braces within the handle() method. --- src/Parser.php | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index cf1d2a7..dce3c95 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -52,9 +52,33 @@ public function parseFeatureJobs(Feature $feature) public function parseFunctionBody($contents, $function) { - $pattern = "/function\s$function\([a-zA-Z0-9_\$\s]+\)?". // match "function handle(...)" - '[\n\s]?[\t\s]*'. // regardless of the indentation preceding the { - '{([^{}]*)}/'; // find everything within braces. + // $pattern = "/function\s$function\([a-zA-Z0-9_\$\s,]+\)?". // match "function handle(...)" + // '[\n\s]?[\t\s]*'. // regardless of the indentation preceding the { + // '{([^{}]*)}/'; // find everything within braces. + + $pattern = '~^\s*[\w\s]+\(.*\)\s*\K({((?>"[^"]*+"|\'[^\']*+\'|//.*$|/\*[\s\S]*?\*/|#.*$|<<<\s*["\']?(\w+)["\']?[^;]+\3;$|[^{}<\'"/#]++|[^{}]++|(?1))*)})~m'; + + // '~^ \s* [\w\s]+ \( .* \) \s* \K'. # how it matches a function definition + // '('. # (1 start) + // '{'. # opening brace + // '('. # (2 start) + /* '(?>'.*/ # atomic grouping (for its non-capturing purpose only) + // '" [^"]*+ "'. # double quoted strings + // '| \' [^\']*+ \''. # single quoted strings + // '| // .* $'. # a comment block starting with // + // '| /\* [\s\S]*? \*/'. # a multi line comment block /*...*/ + // '| \# .* $'. # a single line comment block starting with #... + // '| <<< \s* ["\']?'. # heredocs and nowdocs + // '( \w+ )'. # (3) ^ + // '["\']? [^;]+ \3 ; $'. # ^ + // '| [^{}<\'"/#]++'. # force engine to backtack if it encounters special characters [<'"/#] (possessive) + // '| [^{}]++'. # default matching bahaviour (possessive) + // '| (?1)'. # recurse 1st capturing group + // ')*'. # zero to many times of atomic group + // ')'. # (2 end) + // '}'. # closing brace + // ')~'; # (1 end) + preg_match($pattern, $contents, $match); @@ -184,7 +208,7 @@ private function parseKeywordJobSyntax($match, $contents) } else { // nope it's just Space::class, we will figure // out the namespace from a "use" statement. - $name = str_replace('::class', '', $match); + $name = str_replace(['::class', ');'], '', $match); preg_match("/use\s(.*$name)/", $contents, $namespace); // it is necessary to have a \ at the beginning. $namespace = '\\'.preg_replace('/^\\\/', '', $namespace[1]); @@ -207,7 +231,7 @@ private function parseInitJobSyntax($match, $contents) $match = str_replace('new ', '', $match); // match the job's class name - preg_match('/(.*Job).*\);/', $match, $name); + preg_match('/(.*Job).*[\);]?/', $match, $name); $name = $name[1]; // Determine Namespace