propelorm/Propel2

src/Propel/Generator/Util/PhpParser.php
B

Complex method in findMethod

    public function findMethod($methodName)
    {
        if (isset($this->knownMethodCache[$methodName])) {
            return $this->knownMethodCache[$methodName];
        }
Found in src/Propel/Generator/Util/PhpParser.php - About 3 hrs to fix

Long or complex methods can make code harder to understand. In most circumstances, methods are best as a small chunk of code (the "how") with a clear, understandable name (the "what"). Long methods can also lead to duplication, as it's harder to reuse logic that is tightly coupled to surrounding code.

Refactorings

Read More

Method findMethod is too long

    public function findMethod($methodName)
    {
        if (isset($this->knownMethodCache[$methodName])) {
            return $this->knownMethodCache[$methodName];
        }
Severity: Minor
Found in src/Propel/Generator/Util/PhpParser.php - About 1 hr to fix

Cyclomatic complexity for function findMethod is too high

    public function findMethod($methodName)
    {
        if (isset($this->knownMethodCache[$methodName])) {
            return $this->knownMethodCache[$methodName];
        }
Severity: Minor
Found in src/Propel/Generator/Util/PhpParser.php - About 1 hr to fix

The variable name, functionBracketBalance, is too long

        $functionBracketBalance = 0;
Severity: Minor
Found in src/Propel/Generator/Util/PhpParser.php - About 5 mins to fix

There are no issues that match your filters.

<?php

/**
 * This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license MIT License
 */

namespace Propel\Generator\Util;

if (!defined('T_ML_COMMENT')) {
    define('T_ML_COMMENT', T_COMMENT);
} else {
    define('T_DOC_COMMENT', T_ML_COMMENT);
}

/**
 * Service class for parsing PHP code strings and editing them
 * @example Basic usage:
 * <code>
 * $script = file_get_contents($fileName);
 * $parser = new PhpParser($script);
 * $parser->removeMethod('foo');
 * $parser->replaceMethod('bar', '// bar method was removed');
 * file_put_contents($fileName, $parser->getCode());
 * </code>
 *
 * @author Fran├žois Zaninotto
 */
class PhpParser
{
    protected $code;

    protected $isAddPhp;

    /**
     * methodName => methodCode
     *
     * @var string[]
     */
    private $knownMethodCache = array();

    /**
     * Parser constructor
     *
     * @param string  $code     PHP code to parse
     * @param boolean $isAddPhp Whether the supplied code needs a supplementary '<?php '
     *                          to be seen as code by the tokenizer.
     */
    public function __construct($code, $isAddPhp = false)
    {
        $this->code = $isAddPhp ? $this->addPhp($code) : $code;
        $this->isAddPhp = $isAddPhp;
    }

    /**
     * Get the modified code
     *
     * @return string PHP code
     */
    public function getCode()
    {
        return $this->isAddPhp ? $this->removePhp($this->code) : $this->code;
    }

    protected function addPhp($code)
    {
        return '<?php '. $code;
    }

    protected function removePhp($code)
    {
        return substr($code, 6);
    }

    /**
     * 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
     */
    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 removes 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
     */
    public function removeMethod($methodName)
    {
        if ($methodCode = $this->findMethod($methodName)) {
            $this->code = str_replace($methodCode, '', $this->code);
            $this->knownMethodCache[$methodName] = false;

            return $methodCode;
        }

        return false;
    }

    /**
     * Parse the code looking for a method definition, and replaces the code if found
     *
     * @param string $methodName The name of the method to find, e.g. 'getAuthor'
     * @param string $newCode    The code to use in place of the old method definition
     *
     * @return mixed false if not found, or the method code string if found
     */
    public function replaceMethod($methodName, $newCode)
    {
        if ($methodCode = $this->findMethod($methodName)) {
            $this->code = str_replace($methodCode, $newCode, $this->code);
            $this->knownMethodCache[$methodName] = $newCode;

            return $methodCode;
        }

        return false;
    }

    /**
     * Parse the code looking for a method definition, and adds the code after if found
     *
     * @param string $methodName The name of the method to find, e.g. 'getAuthor'
     * @param string $newCode    The code to add to the class
     *
     * @return mixed false if not found, or the method code string if found
     */
    public function addMethodAfter($methodName, $newCode)
    {
        if ($methodCode = $this->findMethod($methodName)) {
            $this->code = str_replace($methodCode, $methodCode. $newCode, $this->code);

            return $methodCode;
        }

        return false;
    }

    /**
     * Parse the code looking for a method definition, and adds the code before if found
     *
     * @param string $methodName The name of the method to find, e.g. 'getAuthor'
     * @param string $newCode    The code to add to the class
     *
     * @return mixed false if not found, or the method code string if found
     */
    public function addMethodBefore($methodName, $newCode)
    {
        if ($methodCode = $this->findMethod($methodName)) {
            $this->code = str_replace($methodCode, $newCode . $methodCode, $this->code);

            return $methodCode;
        }

        return false;
    }
}

Size

Lines of code
122