本文整理汇总了PHP中XHPASTNode类的典型用法代码示例。如果您正苦于以下问题:PHP XHPASTNode类的具体用法?PHP XHPASTNode怎么用?PHP XHPASTNode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了XHPASTNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: process
public function process(XHPASTNode $root)
{
$all_paren_groups = $root->selectDescendantsOfTypes(array('n_ARRAY_VALUE_LIST', 'n_ASSIGNMENT_LIST', 'n_CALL_PARAMETER_LIST', 'n_DECLARATION_PARAMETER_LIST', 'n_CONTROL_CONDITION', 'n_FOR_EXPRESSION', 'n_FOREACH_EXPRESSION'));
foreach ($all_paren_groups as $group) {
$tokens = $group->getTokens();
$token_o = array_shift($tokens);
$token_c = array_pop($tokens);
$nonsem_o = $token_o->getNonsemanticTokensAfter();
$nonsem_c = $token_c->getNonsemanticTokensBefore();
if (!$nonsem_o) {
continue;
}
$raise = array();
$string_o = implode('', mpull($nonsem_o, 'getValue'));
if (preg_match('/^[ ]+$/', $string_o)) {
$raise[] = array($nonsem_o, $string_o);
}
if ($nonsem_o !== $nonsem_c) {
$string_c = implode('', mpull($nonsem_c, 'getValue'));
if (preg_match('/^[ ]+$/', $string_c)) {
$raise[] = array($nonsem_c, $string_c);
}
}
foreach ($raise as $warning) {
list($tokens, $string) = $warning;
$this->raiseLintAtOffset(reset($tokens)->getOffset(), pht('Parentheses should hug their contents.'), $string, '');
}
}
}
示例2: process
public function process(XHPASTNode $root)
{
$parser = new PhutilDocblockParser();
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($classes as $class) {
$is_final = false;
$is_abstract = false;
$is_concrete_extensible = false;
$attributes = $class->getChildOfType(0, 'n_CLASS_ATTRIBUTES');
foreach ($attributes->getChildren() as $child) {
if ($child->getConcreteString() == 'final') {
$is_final = true;
}
if ($child->getConcreteString() == 'abstract') {
$is_abstract = true;
}
}
$docblock = $class->getDocblockToken();
if ($docblock) {
list($text, $specials) = $parser->parse($docblock->getValue());
$is_concrete_extensible = idx($specials, 'concrete-extensible');
}
if (!$is_final && !$is_abstract && !$is_concrete_extensible) {
$this->raiseLintAtNode($class->getChildOfType(1, 'n_CLASS_NAME'), pht('This class is neither `%s` nor `%s`, and does not have ' . 'a docblock marking it `%s`.', 'final', 'abstract', '@concrete-extensible'));
}
}
}
示例3: process
public function process(XHPASTNode $root)
{
$expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
static $operators = array('-' => true, '/' => true, '-=' => true, '/=' => true, '<=' => true, '<' => true, '==' => true, '===' => true, '!=' => true, '!==' => true, '>=' => true, '>' => true);
static $logical = array('||' => true, '&&' => true);
foreach ($expressions as $expr) {
$operator = $expr->getChildByIndex(1)->getConcreteString();
if (!empty($operators[$operator])) {
$left = $expr->getChildByIndex(0)->getSemanticString();
$right = $expr->getChildByIndex(2)->getSemanticString();
if ($left === $right) {
$this->raiseLintAtNode($expr, pht('Both sides of this expression are identical, so it always ' . 'evaluates to a constant.'));
}
}
if (!empty($logical[$operator])) {
$left = $expr->getChildByIndex(0)->getSemanticString();
$right = $expr->getChildByIndex(2)->getSemanticString();
// NOTE: These will be null to indicate "could not evaluate".
$left = $this->evaluateStaticBoolean($left);
$right = $this->evaluateStaticBoolean($right);
if ($operator === '||' && ($left === true || $right === true) || $operator === '&&' && ($left === false || $right === false)) {
$this->raiseLintAtNode($expr, pht('The logical value of this expression is static. ' . 'Did you forget to remove some debugging code?'));
}
}
}
}
开发者ID:barcelonascience,项目名称:arcanist,代码行数:26,代码来源:ArcanistTautologicalExpressionXHPASTLinterRule.php
示例4: process
public function process(XHPASTNode $root)
{
$nodes = $root->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST');
foreach ($nodes as $node) {
$this->raiseLintAtNode($node, pht('Limit the use of global variables. Global variables are ' . 'generally a bad idea and should be avoided when possible.'));
}
}
示例5: lintStrposUsedForStart
private function lintStrposUsedForStart(XHPASTNode $root)
{
$expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
foreach ($expressions as $expression) {
$operator = $expression->getChildOfType(1, 'n_OPERATOR');
$operator = $operator->getConcreteString();
if ($operator !== '===' && $operator !== '!==') {
continue;
}
$zero = $expression->getChildByIndex(0);
if ($zero->getTypeName() === 'n_NUMERIC_SCALAR' && $zero->getConcreteString() === '0') {
$strpos = $expression->getChildByIndex(2);
} else {
$strpos = $zero;
$zero = $expression->getChildByIndex(2);
if ($zero->getTypeName() !== 'n_NUMERIC_SCALAR' || $zero->getConcreteString() !== '0') {
continue;
}
}
if ($strpos->getTypeName() !== 'n_FUNCTION_CALL') {
continue;
}
$name = strtolower($strpos->getChildByIndex(0)->getConcreteString());
if ($name === 'strpos') {
$this->raiseLintAtNode($strpos, pht('Use %s for checking if the string starts with something.', 'strncmp()'));
} else {
if ($name === 'stripos') {
$this->raiseLintAtNode($strpos, pht('Use %s for checking if the string starts with something.', 'strncasecmp()'));
}
}
}
}
示例6: process
public function process(XHPASTNode $root)
{
$tokens = $root->selectTokensOfType('T_ELSEIF');
foreach ($tokens as $token) {
$this->raiseLintAtToken($token, pht('Usage of `%s` is preferred over `%s`.', 'else if', 'elseif'), 'else if');
}
}
示例7: process
public function process(XHPASTNode $root)
{
$keywords = $root->selectTokensOfTypes(array('T_REQUIRE_ONCE', 'T_REQUIRE', 'T_EVAL', 'T_INCLUDE_ONCE', 'T_INCLUDE', 'T_LOGICAL_OR', 'T_LOGICAL_XOR', 'T_LOGICAL_AND', 'T_PRINT', 'T_INSTANCEOF', 'T_CLONE', 'T_NEW', 'T_EXIT', 'T_IF', 'T_ELSEIF', 'T_ELSE', 'T_ENDIF', 'T_ECHO', 'T_DO', 'T_WHILE', 'T_ENDWHILE', 'T_FOR', 'T_ENDFOR', 'T_FOREACH', 'T_ENDFOREACH', 'T_DECLARE', 'T_ENDDECLARE', 'T_AS', 'T_SWITCH', 'T_ENDSWITCH', 'T_CASE', 'T_DEFAULT', 'T_BREAK', 'T_CONTINUE', 'T_GOTO', 'T_FUNCTION', 'T_CONST', 'T_RETURN', 'T_TRY', 'T_CATCH', 'T_THROW', 'T_USE', 'T_GLOBAL', 'T_PUBLIC', 'T_PROTECTED', 'T_PRIVATE', 'T_FINAL', 'T_ABSTRACT', 'T_STATIC', 'T_VAR', 'T_UNSET', 'T_ISSET', 'T_EMPTY', 'T_HALT_COMPILER', 'T_CLASS', 'T_INTERFACE', 'T_EXTENDS', 'T_IMPLEMENTS', 'T_LIST', 'T_ARRAY', 'T_NAMESPACE', 'T_INSTEADOF', 'T_CALLABLE', 'T_TRAIT', 'T_YIELD', 'T_FINALLY'));
foreach ($keywords as $keyword) {
$value = $keyword->getValue();
if ($value != strtolower($value)) {
$this->raiseLintAtToken($keyword, pht('Convention: spell keyword `%s` as `%s`.', $value, strtolower($value)), strtolower($value));
}
}
$symbols = $root->selectDescendantsOfType('n_SYMBOL_NAME');
foreach ($symbols as $symbol) {
static $interesting_symbols = array('false' => true, 'null' => true, 'true' => true);
$symbol_name = $symbol->getConcreteString();
if ($symbol->getParentNode()->getTypeName() == 'n_FUNCTION_CALL') {
continue;
}
if (idx($interesting_symbols, strtolower($symbol_name))) {
if ($symbol_name != strtolower($symbol_name)) {
$this->raiseLintAtNode($symbol, pht('Convention: spell keyword `%s` as `%s`.', $symbol_name, strtolower($symbol_name)), strtolower($symbol_name));
}
}
}
$magic_constants = $root->selectTokensOfTypes(array('T_CLASS_C', 'T_METHOD_C', 'T_FUNC_C', 'T_LINE', 'T_FILE', 'T_NS_C', 'T_DIR', 'T_TRAIT_C'));
foreach ($magic_constants as $magic_constant) {
$value = $magic_constant->getValue();
if ($value != strtoupper($value)) {
$this->raiseLintAtToken($magic_constant, pht('Magic constants should be uppercase.'), strtoupper($value));
}
}
}
示例8: process
public function process(XHPASTNode $root)
{
static $functions = array('fprintf' => 1, 'printf' => 0, 'sprintf' => 0, 'vfprintf' => 1, 'csprintf' => 0, 'execx' => 0, 'exec_manual' => 0, 'hgsprintf' => 0, 'hsprintf' => 0, 'jsprintf' => 0, 'pht' => 0, 'phutil_passthru' => 0, 'qsprintf' => 1, 'queryfx' => 1, 'queryfx_all' => 1, 'queryfx_one' => 1, 'vcsprintf' => 0, 'vqsprintf' => 1);
$function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
foreach ($function_calls as $call) {
$name = $call->getChildByIndex(0)->getConcreteString();
$name = strtolower($name);
$start = idx($functions + $this->printfFunctions, $name);
if ($start === null) {
continue;
}
$parameters = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
$argc = count($parameters->getChildren()) - $start;
if ($argc < 1) {
$this->raiseLintAtNode($call, pht('This function is expected to have a format string.'));
continue;
}
$format = $parameters->getChildByIndex($start);
if ($format->getTypeName() != 'n_STRING_SCALAR') {
continue;
}
$argv = array($format->evalStatic()) + array_fill(0, $argc, null);
try {
xsprintf(null, null, $argv);
} catch (BadFunctionCallException $ex) {
$this->raiseLintAtNode($call, str_replace('xsprintf', $name, $ex->getMessage()));
} catch (InvalidArgumentException $ex) {
// Ignore.
}
}
}
示例9: process
public function process(XHPASTNode $root)
{
$calls = $root->selectDescendantsOfTypes(array('n_FUNCTION_CALL', 'n_METHOD_CALL'));
foreach ($calls as $call) {
$params = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
$tokens = $params->getTokens();
$first = head($tokens);
$leading = $first->getNonsemanticTokensBefore();
$leading_text = implode('', mpull($leading, 'getValue'));
if (preg_match('/^\\s+$/', $leading_text)) {
$this->raiseLintAtOffset($first->getOffset() - strlen($leading_text), pht('Convention: no spaces before opening parenthesis in calls.'), $leading_text, '');
}
}
foreach ($calls as $call) {
// If the last parameter of a call is a HEREDOC, don't apply this rule.
$params = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST')->getChildren();
if ($params) {
$last_param = last($params);
if ($last_param->getTypeName() === 'n_HEREDOC') {
continue;
}
}
$tokens = $call->getTokens();
$last = array_pop($tokens);
$trailing = $last->getNonsemanticTokensBefore();
$trailing_text = implode('', mpull($trailing, 'getValue'));
if (preg_match('/^\\s+$/', $trailing_text)) {
$this->raiseLintAtOffset($last->getOffset() - strlen($trailing_text), pht('Convention: no spaces before closing parenthesis in calls.'), $trailing_text, '');
}
}
}
示例10: process
public function process(XHPASTNode $root)
{
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($classes as $class) {
$attributes = $class->getChildOfType(0, 'n_CLASS_ATTRIBUTES');
$is_final = false;
foreach ($attributes->getChildren() as $attribute) {
if ($attribute->getConcreteString() == 'final') {
$is_final = true;
break;
}
}
if (!$is_final) {
continue;
}
$methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION');
foreach ($methods as $method) {
$attributes = $method->getChildOfType(0, 'n_METHOD_MODIFIER_LIST');
foreach ($attributes->getChildren() as $attribute) {
if ($attribute->getConcreteString() == 'final') {
$this->raiseLintAtNode($attribute, pht('Unnecessary %s modifier in %s class.', 'final', 'final'));
}
}
}
}
}
示例11: process
public function process(XHPASTNode $root)
{
$arrays = $root->selectDescendantsOfType('n_ARRAY_LITERAL');
foreach ($arrays as $array) {
$array_values = $array->getChildOfType(0, 'n_ARRAY_VALUE_LIST')->getChildrenOfType('n_ARRAY_VALUE');
if (!$array_values) {
// There is no need to check an empty array.
continue;
}
$multiline = $array->getLineNumber() != $array->getEndLineNumber();
if (!$multiline) {
continue;
}
foreach ($array_values as $value) {
list($before, $after) = $value->getSurroundingNonsemanticTokens();
if (strpos(implode('', mpull($before, 'getValue')), "\n") === false) {
if (last($before) && last($before)->isAnyWhitespace()) {
$token = last($before);
$replacement = "\n" . $value->getIndentation();
} else {
$token = head($value->getTokens());
$replacement = "\n" . $value->getIndentation() . $token->getValue();
}
$this->raiseLintAtToken($token, pht('Array elements should each occupy a single line.'), $replacement);
}
}
}
}
示例12: process
public function process(XHPASTNode $root)
{
$index_accesses = $root->selectDescendantsOfType('n_INDEX_ACCESS');
foreach ($index_accesses as $index_access) {
$tokens = $index_access->getChildByIndex(1)->getTokens();
if (!$tokens) {
continue;
}
$left_brace = head($tokens)->getPrevToken();
while (!$left_brace->isSemantic()) {
$left_brace = $left_brace->getPrevToken();
}
$right_brace = last($tokens)->getNextToken();
while (!$right_brace->isSemantic()) {
$right_brace = $right_brace->getNextToken();
}
if ($left_brace->getValue() == '{' || $right_brace->getValue() == '}') {
$replacement = null;
foreach ($index_access->getTokens() as $token) {
if ($token === $left_brace) {
$replacement .= '[';
} else {
if ($token === $right_brace) {
$replacement .= ']';
} else {
$replacement .= $token->getValue();
}
}
}
$this->raiseLintAtNode($index_access, pht('Use `%s` instead of `%s`.', "\$x['key']", "\$x{'key'}"), $replacement);
}
}
}
示例13: process
public function process(XHPASTNode $root)
{
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($classes as $class) {
$methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION');
foreach ($methods as $method) {
$attributes = $method->getChildByIndex(0, 'n_METHOD_MODIFIER_LIST')->selectDescendantsOfType('n_STRING');
$method_is_static = false;
$method_is_abstract = false;
foreach ($attributes as $attribute) {
if (strtolower($attribute->getConcreteString()) === 'static') {
$method_is_static = true;
}
if (strtolower($attribute->getConcreteString()) === 'abstract') {
$method_is_abstract = true;
}
}
if ($method_is_abstract) {
continue;
}
if (!$method_is_static) {
continue;
}
$body = $method->getChildOfType(5, 'n_STATEMENT_LIST');
$variables = $body->selectDescendantsOfType('n_VARIABLE');
foreach ($variables as $variable) {
if ($method_is_static && strtolower($variable->getConcreteString()) === '$this') {
$this->raiseLintAtNode($variable, pht('You can not reference `%s` inside a static method.', '$this'));
}
}
}
}
}
示例14: process
public function process(XHPASTNode $root)
{
$vvars = $root->selectDescendantsOfType('n_VARIABLE_VARIABLE');
foreach ($vvars as $vvar) {
$this->raiseLintAtNode($vvar, pht('Rewrite this code to use an array. Variable variables are unclear ' . 'and hinder static analysis.'));
}
}
示例15: process
public function process(XHPASTNode $root)
{
$class_declarations = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($class_declarations as $class_declaration) {
$class_name = $class_declaration->getChildOfType(1, 'n_CLASS_NAME')->getConcreteString();
$class_static_accesses = $class_declaration->selectDescendantsOfType('n_CLASS_STATIC_ACCESS');
$closures = $this->getAnonymousClosures($class_declaration);
foreach ($class_static_accesses as $class_static_access) {
$class_ref = $class_static_access->getChildByIndex(0);
if ($class_ref->getTypeName() != 'n_CLASS_NAME') {
continue;
}
$class_ref_name = $class_ref->getConcreteString();
if (strtolower($class_name) == strtolower($class_ref_name)) {
$in_closure = false;
foreach ($closures as $closure) {
if ($class_ref->isDescendantOf($closure)) {
$in_closure = true;
break;
}
}
if (version_compare($this->version, '5.4.0', '>=') || !$in_closure) {
$this->raiseLintAtNode($class_ref, pht('Use `%s` for local static member references.', 'self::'), 'self');
}
}
}
}
}