2

我正在尝试使用 phpcs 获取类变量列表,但只能获取文件中所有变量的列表。有没有人对实现这一目标有任何想法?

嗅探代码

for ($i = $stackPtr; $i < count ($tokens); $i++) {
    $variable = $phpcsFile->findNext(T_VARIABLE, ($i));
    if ($variable !== false) {
        print ($variable);
    } else {
        break;
    }
}

要分析的文件

<?PHP
/** 
 * @package     xxxx
 * @subpackage  Pro/System/Classes
 * @copyright   
 * @author      
 * @link        
 */
class Test_Class_AAA {

    const CONSTANT = 'constant value';
    public $var1 = 12;
    const CONSTANT2 = 'constant value 2';

    /**
     * Return record schema details given a LabelIndentifier/LabelNumber or
     * TransactionCode.
     *
     * @param   string  $type             record type
     * @param   string  $myextralongparam   record type
     * @return  array                       record schema details (or null)
     */
    private function func ($type,$myextralongparam) {
        if (true) {
            $x = 10;
            $y = 11;
            $z = $x + $y;
            $f = $x - $y
            $f = $x * $t;
            $f = $x / $y;
            $f = $x % $y;
            $f = $x ** $y;
            $f += 1;
            $f -= 1;
            $f *= 1;
            $f /= 1;
            $f %= 1;
            if ($x === $y) {
            }
            if ($x !== $y) {
            }
            if ($x < $y) {
            }
            if ($x > $y) {
            }
            if ($x <= $y) {
            }
            if ($x >= $y) {
            }

            ++$x;
        } else {
        }

        while (true) {
        }

        do {
        } while (true);

        for ($i = 0; $i < 5; $i++) {
        }
    };

    /**
     * Return record schema details given a LabelIndentifier/LabelNumber or
     * TransactionCode.
     *
     * @return  array                       record schema details (or null)
     */
    public function __construct () {
        print "In BaseClass constructor\n";
    }
}
4

1 回答 1

1

ClassWrapper正是为这些用例制作的。

你可以在这里找到灵感,方法getPropertyNames()

/**
 * Inspired by @see TokensAnalyzer.
 *
 * @return string[]
 */
public function getPropertyNames(): array
{
    if ($this->propertyNames) {
        return $this->propertyNames;
    }

    $classOpenerPosition = $this->classToken['scope_opener'] + 1;
    $classCloserPosition = $this->classToken['scope_closer'] - 1;

    $propertyTokens = $this->findClassLevelTokensType($classOpenerPosition, $classCloserPosition, T_VARIABLE);

    $this->propertyNames = $this->extractPropertyNamesFromPropertyTokens($propertyTokens);
    $this->propertyNames = array_merge($this->propertyNames, $this->getParentClassPropertyNames());

    return $this->propertyNames;
}
于 2017-10-27T23:54:32.837 回答