public function findMethod($methodName)
    {
        if (isset($this->knownMethodCache[$methodName])) {
            return $this->knownMethodCache[$methodName];
        }

        // Tokenize the source
        $tokens = token_get_all($this->code);
        $methodCode = '';

        // Some flags and counters
        $isInFunction = false;
        $functionBracketBalance = 0;
        $buffer = '';

        // Iterate over all tokens
        foreach ($tokens as $token) {
            // Single-character tokens.
            if (is_string($token)) {
                if (!$isInFunction) {
                    if ($token == '{' || $token == ';') {
                        // class-opening bracket or end of line
                        $buffer = '';
                    } else {
                        // comment or public|protected|private
                        $buffer .= $token;
                    }
                    continue;
                }
                $methodCode .= $token;
                if ($token == '{') {
                    // Increase the bracket-counter (not the class-brackets: `$isInFunction` must be true!)
                    $functionBracketBalance++;
                }
                if ($token == '}') {
                    // Decrease the bracket-counter (not the class-brackets: `$isInFunction` must be true!)
                    $functionBracketBalance--;
                    if ($functionBracketBalance == 0) {
                        if (strpos($methodCode, 'function ' . $methodName . '(') !== false) {
                            $this->knownMethodCache[$methodName] = $methodCode;

                            return $methodCode;
                        } else {
                            // If it's the closing bracket of the function, reset `$isInFunction`
                            $isInFunction = false;
                            $methodCode = '';
                            $buffer = '';
                        }
                    }
                }
            } else {
                // Tokens consisting of (possibly) more than one character.
                list($id, $text) = $token;
                switch ($id) {
                    case T_FUNCTION:
                        // If we encounter the keyword 'function', flip the `isInFunction` flag to
                        // true and reset the `buffer`
                        $isInFunction = true;
                        $methodCode .= $buffer . $text;
                        $buffer = '';
                        break;
                    default:
                        if ($isInFunction) {
                            $methodCode .= $text;
                        } else {
                            $buffer .= $text;
                        }
                        break;
                }
            }
        }

        // method not found
        $this->knownMethodCache[$methodName] = false;
        return false;
    }
public function findMethod($methodName)
    {
        if (isset($this->knownMethodCache[$methodName])) {
            return $this->knownMethodCache[$methodName];
        }

        // Tokenize the source
        $tokens = token_get_all($this->code);
        $methodCode = '';

        // Some flags and counters
        $isInFunction = false;
        $functionBracketBalance = 0;
        $buffer = '';

        // Iterate over all tokens
        foreach ($tokens as $token) {
            // Single-character tokens.
            if (is_string($token)) {
                if (!$isInFunction) {
                    if ($token == '{' || $token == ';') {
                        // class-opening bracket or end of line
                        $buffer = '';
                    } else {
                        // comment or public|protected|private
                        $buffer .= $token;
                    }
                    continue;
                }
                $methodCode .= $token;
                if ($token == '{') {
                    // Increase the bracket-counter (not the class-brackets: `$isInFunction` must be true!)
                    $functionBracketBalance++;
                }
                if ($token == '}') {
                    // Decrease the bracket-counter (not the class-brackets: `$isInFunction` must be true!)
                    $functionBracketBalance--;
                    if ($functionBracketBalance == 0) {
                        if (strpos($methodCode, 'function ' . $methodName . '(') !== false) {
                            $this->knownMethodCache[$methodName] = $methodCode;

                            return $methodCode;
                        } else {
                            // If it's the closing bracket of the function, reset `$isInFunction`
                            $isInFunction = false;
                            $methodCode = '';
                            $buffer = '';
                        }
                    }
                }
            } else {
                // Tokens consisting of (possibly) more than one character.
                list($id, $text) = $token;
                switch ($id) {
                    case T_FUNCTION:
                        // If we encounter the keyword 'function', flip the `isInFunction` flag to
                        // true and reset the `buffer`
                        $isInFunction = true;
                        $methodCode .= $buffer . $text;
                        $buffer = '';
                        break;
                    default:
                        if ($isInFunction) {
                            $methodCode .= $text;
                        } else {
                            $buffer .= $text;
                        }
                        break;
                }
            }
        }

        // method not found
        $this->knownMethodCache[$methodName] = false;
        return false;
    }
public function findMethod ( $ methodName ) { if ( isset ( $ this -> knownMethodCache [ $ methodName ] ) ) { return $ this -> knownMethodCache [ $ methodName ] ; } // Tokenize the source $ tokens = token_get_all ( $ this -> code ) ; $ methodCode = '' ; // Some flags and counters $ isInFunction = false ; $ functionBracketBalance = 0 ; $ buffer = '' ; // Iterate over all tokens foreach ( $ tokens as $ token ) { // Single-character tokens. if ( is_string ( $ token ) ) { if ( ! $ isInFunction ) { if ( $ token == '{' || $ token == ';' ) { // class-opening bracket or end of line $ buffer = '' ; } else { // comment or public|protected|private $ buffer .= $ token ; } continue ; } $ methodCode .= $ token ; if ( $ token == '{' ) { // Increase the bracket-counter (not the class-brackets: `$isInFunction` must be true!) $ functionBracketBalance ++ ; } if ( $ token == '}' ) { // Decrease the bracket-counter (not the class-brackets: `$isInFunction` must be true!) $ functionBracketBalance -- ; if ( $ functionBracketBalance == 0 ) { if ( strpos ( $ methodCode , 'function ' . $ methodName . '(' ) !== false ) { $ this -> knownMethodCache [ $ methodName ] = $ methodCode ; return $ methodCode ; } else { // If it's the closing bracket of the function, reset `$isInFunction` $ isInFunction = false ; $ methodCode = '' ; $ buffer = '' ; } } } } else { // Tokens consisting of (possibly) more than one character. list ( $ id , $ text ) = $ token ; switch ( $ id ) { case T_FUNCTION : // If we encounter the keyword 'function', flip the `isInFunction` flag to // true and reset the `buffer` $ isInFunction = true ; $ methodCode .= $ buffer . $ text ; $ buffer = '' ; break ; default : if ( $ isInFunction ) { $ methodCode .= $ text ; } else { $ buffer .= $ text ; } break ; } } } // method not found $ this -> knownMethodCache [ $ methodName ] = false ; return false ; }
Parse the code looking for a method definition, and returns the code if found

@param string $methodName The name of the method to find, e.g. 'getAuthor'

@return mixed false if not found, or the method code string if found
Parse the code looking for a method definition and returns the code if found