CodeCheck.php 4.71 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\TestFramework\Utility;

/**
 * Searches for usage of classes, namespaces, functions, etc in PHP files
 */
class CodeCheck
{
    /**
     * Check if the class is used in the content
     *
     * @param string $className
     * @param string $content
     * @return bool
     */
    public static function isClassUsed($className, $content)
    {
        /* avoid matching namespace instead of class */
        $content = preg_replace('/namespace[^;]+;/', '', $content);
        return self::_isRegexpMatched('/[^a-z\d_\$]' . preg_quote($className, '/') . '[^a-z\d_\\\\]/iS', $content);
    }

    /**
     * Check if the namespace is used in the content
     *
     * @param string $namespace
     * @param string $content
     * @return bool
     */
    public static function isNamespaceUsed($namespace, $content)
    {
        return self::_isRegexpMatched('/namespace\s+' . preg_quote($namespace, '/') . ';/S', $content)
        || self::_isRegexpMatched('/[^a-zA-Z\d_]' . preg_quote($namespace . '\\', '/') . '/S', $content);
    }

    /**
     * Check if the specified function is called in the content.
     * Note that declarations are not considered.
     *
     * If class context is not specified, invocation of all functions or methods (of any class)
     * will be matched across the board
     *
     * If some class is specified, only its methods will be matched as follows:
     * - usage of class::method
     * - usage of $this, self and static within the class and its descendants
     *
     * @param string $method
     * @param string $content
     * @param string $class
     * @return bool
     */
    public static function isFunctionCalled($method, $content, $class = null)
    {
        $quotedMethod = preg_quote($method, '/');
        if (!$class) {
            return self::_isRegexpMatched(
                '/(?<![a-z\d_:]|->|function\s)' . $quotedMethod . '\s*\(/iS',
                $content
            );
        }
        // without opening parentheses to match static callbacks notation
        if (self::_isRegexpMatched(
            '/' . preg_quote($class, '/') . '::\s*' . $quotedMethod . '[^a-z\d_]/iS',
            $content
        )
        ) {
            return true;
        }
        if (self::isClassOrInterface($content, $class) || self::isDirectDescendant($content, $class)) {
            return self::_isRegexpMatched('/this->' . $quotedMethod . '\s*\(/iS', $content)
            || self::_isRegexpMatched(
                '/(self|static|parent)::\s*' . $quotedMethod . '\s*\(/iS',
                $content
            );
        }
    }

    /**
     * Check if methods or functions are used in the content
     *
     * If class context is specified, only the class and its descendants will be checked.
     *
     * @param string $property
     * @param string $content
     * @param string $class
     * @return bool
     */
    public static function isPropertyUsed($property, $content, $class = null)
    {
        if ($class) {
            if (!self::isClassOrInterface($content, $class) && !self::isDirectDescendant($content, $class)) {
                return false;
            }
        }
        return self::_isRegexpMatched(
            '/[^a-z\d_]' . preg_quote($property, '/') . '[^a-z\d_]/iS',
            $content
        );
    }

    /**
     * Analyze content to determine whether it is a declaration of the specified class/interface
     *
     * @param string $content
     * @param string $name
     * @return bool
     */
    public static function isClassOrInterface($content, $name)
    {
        return self::_isRegexpMatched('/\b(?:class|interface)\s+' . preg_quote($name, '/') . '\b[^{]*\{/iS', $content);
    }

    /**
     * Analyze content to determine whether this is a direct descendant of the specified class/interface
     *
     * @param string $content
     * @param string $name
     * @return bool
     */
    public static function isDirectDescendant($content, $name)
    {
        $name = preg_quote($name, '/');
        return self::_isRegexpMatched(
            '/\s+extends\s+\\\\?' . $name . '\b|\s+implements\s+[^{]*\b' . $name . '\b[^{^\\\\]*\{/iS',
            $content
        );
    }

    /**
     * Check if content matches the regexp
     *
     * @param string $regexp
     * @param string $content
     * @throws \Exception
     * @return bool True if the content matches the regexp
     */
    protected static function _isRegexpMatched($regexp, $content)
    {
        $result = preg_match($regexp, $content);
        if ($result === false) {
            throw new \Exception('An error occurred when matching regexp "' . $regexp . '""');
        }
        return 1 === $result;
    }
}