如何使用phpcs获取所有php成员变量

时间:2017-05-19 14:56:26

标签: php phpcs

我正在尝试使用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";
    }
}

1 个答案:

答案 0 :(得分:1)

我完全针对这些用例制作了ClassWrapper

你可以在这里找到灵感,in getPropertyNames() method

/**
 * 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;
}