protected function parseFunction($match) { $function = new TFunction(); //Matches go [1] = type, [2] = name, [3] = arguments preg_match('/(public|protected|private|)\\s*function (.+)\\s*\\((.*)\\)/', $match, $matches); $function->setType(!empty($matches[1]) ? $matches[1] : 'public'); $function->setName($matches[2]); //Parse arguments if (!empty($matches[3])) { $parts = explode(',', $matches[3]); foreach ($parts as $part) { $part = trim($part); $part = str_replace('&', '', $part); //remove by reference $arg = new TArgument(); if (strpos($part, '=') !== false) { //Default value, trim it off preg_match('/(.+)=/', $part, $argMatches); $part = trim($argMatches[1]); } $partExploded = explode(' ', $part); if (count($partExploded) === 1) { //No type for argument $arg->setName($partExploded[0]); } else { $type = $partExploded[0]; $arg->setType($type); $arg->setName($partExploded[1]); if ($this->class->getUseByAlias($type) !== null) { $class = $this->class->getUseByAlias($type)->getClass(); $arg->setNamespace(str_replace($type, '', $class)); } elseif (!in_array($type, $this->typeHintable)) { $arg->setNamespace($this->class->getNamespace()); } } $function->addArgument($arg); } } //Read function lines to determine code blocks and when class variables are used $root = new TControl('function', $this->getLineNumber()); $function->setRootControl($root); $currentNode = $root; while (($line = $this->readLine()) !== null) { $endFunction = false; $closingBrackets = substr_count($line, '}'); while ($closingBrackets > 0) { $closingBrackets--; if ($currentNode != $root) { $currentNode = $currentNode->getParent(); } else { $endFunction = true; //End of function } } //Determine control blocks like if, for etc foreach ($this->controlRegexes as $controlType => $regex) { if (preg_match($regex, $line) === 1) { $node = new TControl($controlType, $this->getLineNumber()); $currentNode->addChild($node); $currentNode = $node; continue; } } //See if any class variables are used like $this->service->login($user) // [1] = variable, [2] = function, [3] = function args if (preg_match('/\\$this->(\\w+)->(\\w+)\\((.*)\\)/', $line, $matches) === 1) { $functionCall = new TFunctionCall('$this->' . $matches[1], $matches[2], $matches[3]); $currentNode->addFunctionCall($functionCall); } //See if any arguments to the function are used like $param1->login($user) // [1] = variable, [2] = function, [3] = function args if (preg_match('/\\$(\\w+)->(\\w+)\\((.*)\\)/', $line, $matches) === 1) { if ($matches[1] !== 'this' && $function->getArgument($matches[1]) !== null) { $functionCall = new TFunctionCall('$' . $matches[1], $matches[2], $matches[3]); $currentNode->addFunctionCall($functionCall); } } if ($currentNode->getType() == 'return') { //Return statements don't span more than 1 line $currentNode = $currentNode->getParent(); } //Stop parsing more lines if ($endFunction) { break; } } $this->class->addFunction($function); }
public function addFunction(TFunction $function) { $this->functions[$function->getName()] = $function; }