PageRenderTime 66ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lint/linter/xhpast/ArcanistXHPASTLinter.php

https://github.com/seporaitis/arcanist
PHP | 1739 lines | 1293 code | 218 blank | 228 comment | 179 complexity | 7fb366214565f193746488efce4cb39a MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. * Copyright 2012 Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * Uses XHPAST to apply lint rules to PHP.
  19. *
  20. * @group linter
  21. */
  22. final class ArcanistXHPASTLinter extends ArcanistLinter {
  23. protected $trees = array();
  24. const LINT_PHP_SYNTAX_ERROR = 1;
  25. const LINT_UNABLE_TO_PARSE = 2;
  26. const LINT_VARIABLE_VARIABLE = 3;
  27. const LINT_EXTRACT_USE = 4;
  28. const LINT_UNDECLARED_VARIABLE = 5;
  29. const LINT_PHP_SHORT_TAG = 6;
  30. const LINT_PHP_ECHO_TAG = 7;
  31. const LINT_PHP_CLOSE_TAG = 8;
  32. const LINT_NAMING_CONVENTIONS = 9;
  33. const LINT_IMPLICIT_CONSTRUCTOR = 10;
  34. const LINT_DYNAMIC_DEFINE = 12;
  35. const LINT_STATIC_THIS = 13;
  36. const LINT_PREG_QUOTE_MISUSE = 14;
  37. const LINT_PHP_OPEN_TAG = 15;
  38. const LINT_TODO_COMMENT = 16;
  39. const LINT_EXIT_EXPRESSION = 17;
  40. const LINT_COMMENT_STYLE = 18;
  41. const LINT_CLASS_FILENAME_MISMATCH = 19;
  42. const LINT_TAUTOLOGICAL_EXPRESSION = 20;
  43. const LINT_PLUS_OPERATOR_ON_STRINGS = 21;
  44. const LINT_DUPLICATE_KEYS_IN_ARRAY = 22;
  45. const LINT_REUSED_ITERATORS = 23;
  46. const LINT_BRACE_FORMATTING = 24;
  47. const LINT_PARENTHESES_SPACING = 25;
  48. const LINT_CONTROL_STATEMENT_SPACING = 26;
  49. const LINT_BINARY_EXPRESSION_SPACING = 27;
  50. const LINT_ARRAY_INDEX_SPACING = 28;
  51. const LINT_RAGGED_CLASSTREE_EDGE = 29;
  52. const LINT_IMPLICIT_FALLTHROUGH = 30;
  53. const LINT_PHP_53_FEATURES = 31;
  54. const LINT_REUSED_AS_ITERATOR = 32;
  55. public function getLintNameMap() {
  56. return array(
  57. self::LINT_PHP_SYNTAX_ERROR => 'PHP Syntax Error!',
  58. self::LINT_UNABLE_TO_PARSE => 'Unable to Parse',
  59. self::LINT_VARIABLE_VARIABLE => 'Use of Variable Variable',
  60. self::LINT_EXTRACT_USE => 'Use of extract()',
  61. self::LINT_UNDECLARED_VARIABLE => 'Use of Undeclared Variable',
  62. self::LINT_PHP_SHORT_TAG => 'Use of Short Tag "<?"',
  63. self::LINT_PHP_ECHO_TAG => 'Use of Echo Tag "<?="',
  64. self::LINT_PHP_CLOSE_TAG => 'Use of Close Tag "?>"',
  65. self::LINT_NAMING_CONVENTIONS => 'Naming Conventions',
  66. self::LINT_IMPLICIT_CONSTRUCTOR => 'Implicit Constructor',
  67. self::LINT_DYNAMIC_DEFINE => 'Dynamic define()',
  68. self::LINT_STATIC_THIS => 'Use of $this in Static Context',
  69. self::LINT_PREG_QUOTE_MISUSE => 'Misuse of preg_quote()',
  70. self::LINT_PHP_OPEN_TAG => 'Expected Open Tag',
  71. self::LINT_TODO_COMMENT => 'TODO Comment',
  72. self::LINT_EXIT_EXPRESSION => 'Exit Used as Expression',
  73. self::LINT_COMMENT_STYLE => 'Comment Style',
  74. self::LINT_CLASS_FILENAME_MISMATCH => 'Class-Filename Mismatch',
  75. self::LINT_TAUTOLOGICAL_EXPRESSION => 'Tautological Expression',
  76. self::LINT_PLUS_OPERATOR_ON_STRINGS => 'Not String Concatenation',
  77. self::LINT_DUPLICATE_KEYS_IN_ARRAY => 'Duplicate Keys in Array',
  78. self::LINT_REUSED_ITERATORS => 'Reuse of Iterator Variable',
  79. self::LINT_BRACE_FORMATTING => 'Brace placement',
  80. self::LINT_PARENTHESES_SPACING => 'Spaces Inside Parentheses',
  81. self::LINT_CONTROL_STATEMENT_SPACING => 'Space After Control Statement',
  82. self::LINT_BINARY_EXPRESSION_SPACING => 'Space Around Binary Operator',
  83. self::LINT_ARRAY_INDEX_SPACING => 'Spacing Before Array Index',
  84. self::LINT_RAGGED_CLASSTREE_EDGE => 'Class Not abstract Or final',
  85. self::LINT_IMPLICIT_FALLTHROUGH => 'Implicit Fallthrough',
  86. self::LINT_PHP_53_FEATURES => 'Use Of PHP 5.3 Features',
  87. self::LINT_REUSED_AS_ITERATOR => 'Variable Reused As Iterator',
  88. );
  89. }
  90. public function getLinterName() {
  91. return 'XHP';
  92. }
  93. public function getLintSeverityMap() {
  94. return array(
  95. self::LINT_TODO_COMMENT => ArcanistLintSeverity::SEVERITY_ADVICE,
  96. self::LINT_UNABLE_TO_PARSE
  97. => ArcanistLintSeverity::SEVERITY_WARNING,
  98. self::LINT_NAMING_CONVENTIONS
  99. => ArcanistLintSeverity::SEVERITY_WARNING,
  100. self::LINT_PREG_QUOTE_MISUSE
  101. => ArcanistLintSeverity::SEVERITY_WARNING,
  102. self::LINT_BRACE_FORMATTING
  103. => ArcanistLintSeverity::SEVERITY_WARNING,
  104. self::LINT_PARENTHESES_SPACING
  105. => ArcanistLintSeverity::SEVERITY_WARNING,
  106. self::LINT_CONTROL_STATEMENT_SPACING
  107. => ArcanistLintSeverity::SEVERITY_WARNING,
  108. self::LINT_BINARY_EXPRESSION_SPACING
  109. => ArcanistLintSeverity::SEVERITY_WARNING,
  110. self::LINT_ARRAY_INDEX_SPACING
  111. => ArcanistLintSeverity::SEVERITY_WARNING,
  112. self::LINT_IMPLICIT_FALLTHROUGH
  113. => ArcanistLintSeverity::SEVERITY_WARNING,
  114. // This is disabled by default because it implies a very strict policy
  115. // which isn't necessary in the general case.
  116. self::LINT_RAGGED_CLASSTREE_EDGE
  117. => ArcanistLintSeverity::SEVERITY_DISABLED,
  118. // This is disabled by default because projects don't necessarily target
  119. // a specific minimum version.
  120. self::LINT_PHP_53_FEATURES
  121. => ArcanistLintSeverity::SEVERITY_DISABLED,
  122. );
  123. }
  124. public function willLintPaths(array $paths) {
  125. $futures = array();
  126. foreach ($paths as $path) {
  127. $futures[$path] = xhpast_get_parser_future($this->getData($path));
  128. }
  129. foreach ($futures as $path => $future) {
  130. $this->willLintPath($path);
  131. try {
  132. $this->trees[$path] = XHPASTTree::newFromDataAndResolvedExecFuture(
  133. $this->getData($path),
  134. $future->resolve());
  135. } catch (XHPASTSyntaxErrorException $ex) {
  136. $this->raiseLintAtLine(
  137. $ex->getErrorLine(),
  138. 1,
  139. self::LINT_PHP_SYNTAX_ERROR,
  140. 'This file contains a syntax error: '.$ex->getMessage());
  141. $this->stopAllLinters();
  142. return;
  143. } catch (Exception $ex) {
  144. $this->raiseLintAtPath(
  145. self::LINT_UNABLE_TO_PARSE,
  146. 'XHPAST could not parse this file, probably because the AST is too '.
  147. 'deep. Some lint issues may not have been detected. You may safely '.
  148. 'ignore this warning.');
  149. return;
  150. }
  151. }
  152. }
  153. public function getXHPASTTreeForPath($path) {
  154. return idx($this->trees, $path);
  155. }
  156. public function lintPath($path) {
  157. if (empty($this->trees[$path])) {
  158. return;
  159. }
  160. $root = $this->trees[$path]->getRootNode();
  161. $root->buildSelectCache();
  162. $root->buildTokenCache();
  163. $this->lintUseOfThisInStaticMethods($root);
  164. $this->lintDynamicDefines($root);
  165. $this->lintSurpriseConstructors($root);
  166. $this->lintPHPTagUse($root);
  167. $this->lintVariableVariables($root);
  168. $this->lintTODOComments($root);
  169. $this->lintExitExpressions($root);
  170. $this->lintSpaceAroundBinaryOperators($root);
  171. $this->lintSpaceAfterControlStatementKeywords($root);
  172. $this->lintParenthesesShouldHugExpressions($root);
  173. $this->lintNamingConventions($root);
  174. $this->lintPregQuote($root);
  175. $this->lintUndeclaredVariables($root);
  176. $this->lintArrayIndexWhitespace($root);
  177. $this->lintHashComments($root);
  178. $this->lintPrimaryDeclarationFilenameMatch($root);
  179. $this->lintTautologicalExpressions($root);
  180. $this->lintPlusOperatorOnStrings($root);
  181. $this->lintDuplicateKeysInArray($root);
  182. $this->lintReusedIterators($root);
  183. $this->lintBraceFormatting($root);
  184. $this->lintRaggedClasstreeEdges($root);
  185. $this->lintImplicitFallthrough($root);
  186. $this->lintPHP53Features($root);
  187. }
  188. public function lintPHP53Features($root) {
  189. $functions = $root->selectTokensOfType('T_FUNCTION');
  190. foreach ($functions as $function) {
  191. $next = $function->getNextToken();
  192. while ($next) {
  193. if ($next->isSemantic()) {
  194. break;
  195. }
  196. $next = $next->getNextToken();
  197. }
  198. if ($next) {
  199. if ($next->getTypeName() == '(') {
  200. $this->raiseLintAtToken(
  201. $function,
  202. self::LINT_PHP_53_FEATURES,
  203. 'This codebase targets PHP 5.2, but anonymous functions were '.
  204. 'not introduced until PHP 5.3.');
  205. }
  206. }
  207. }
  208. $namespaces = $root->selectTokensOfType('T_NAMESPACE');
  209. foreach ($namespaces as $namespace) {
  210. $this->raiseLintAtToken(
  211. $namespace,
  212. self::LINT_PHP_53_FEATURES,
  213. 'This codebase targets PHP 5.2, but namespaces were not introduced '.
  214. 'until PHP 5.3.');
  215. }
  216. // NOTE: This is only "use x;", in anonymous functions the node type is
  217. // n_LEXICAL_VARIABLE_LIST even though both tokens are T_USE.
  218. // TODO: We parse n_USE in a slightly crazy way right now; that would be
  219. // a better selector once it's fixed.
  220. $uses = $root->selectDescendantsOfType('n_USE_LIST');
  221. foreach ($uses as $use) {
  222. $this->raiseLintAtNode(
  223. $use,
  224. self::LINT_PHP_53_FEATURES,
  225. 'This codebase targets PHP 5.2, but namespaces were not introduced '.
  226. 'until PHP 5.3.');
  227. }
  228. }
  229. private function lintImplicitFallthrough($root) {
  230. $switches = $root->selectDescendantsOfType('n_SWITCH');
  231. foreach ($switches as $switch) {
  232. $blocks = array();
  233. $cases = $switch->selectDescendantsOfType('n_CASE');
  234. foreach ($cases as $case) {
  235. $blocks[] = $case;
  236. }
  237. $defaults = $switch->selectDescendantsOfType('n_DEFAULT');
  238. foreach ($defaults as $default) {
  239. $blocks[] = $default;
  240. }
  241. foreach ($blocks as $key => $block) {
  242. $tokens = $block->getTokens();
  243. // Get all the trailing nonsemantic tokens, since we need to look for
  244. // "fallthrough" comments past the end of the semantic block.
  245. $last = end($tokens);
  246. while ($last && $last = $last->getNextToken()) {
  247. if (!$last->isSemantic()) {
  248. $tokens[] = $last;
  249. }
  250. }
  251. $blocks[$key] = $tokens;
  252. }
  253. foreach ($blocks as $tokens) {
  254. // Test each block (case or default statement) to see if it's OK. It's
  255. // OK if:
  256. //
  257. // - it is empty; or
  258. // - it ends in break, return, throw, continue or exit; or
  259. // - it has a comment with "fallthrough" in its text.
  260. // Empty blocks are OK, so we start this at `true` and only set it to
  261. // false if we find a statement.
  262. $block_ok = true;
  263. // Keeps track of whether the current statement is one that validates
  264. // the block (break, return, throw, continue) or something else.
  265. $statement_ok = false;
  266. foreach ($tokens as $token) {
  267. if (!$token->isSemantic()) {
  268. // Liberally match "fall" in the comment text so that comments like
  269. // "fallthru", "fall through", "fallthrough", etc., are accepted.
  270. if (preg_match('/fall/i', $token->getValue())) {
  271. $block_ok = true;
  272. break;
  273. }
  274. continue;
  275. }
  276. $tok_type = $token->getTypeName();
  277. if ($tok_type == ';') {
  278. if ($statement_ok) {
  279. $statment_ok = false;
  280. } else {
  281. $block_ok = false;
  282. }
  283. continue;
  284. }
  285. if ($tok_type == 'T_RETURN' ||
  286. $tok_type == 'T_BREAK' ||
  287. $tok_type == 'T_CONTINUE' ||
  288. $tok_type == 'T_THROW' ||
  289. $tok_type == 'T_EXIT') {
  290. $statement_ok = true;
  291. $block_ok = true;
  292. }
  293. }
  294. if (!$block_ok) {
  295. $this->raiseLintAtToken(
  296. head($tokens),
  297. self::LINT_IMPLICIT_FALLTHROUGH,
  298. "This 'case' or 'default' has a nonempty block which does not ".
  299. "end with 'break', 'continue', 'return', 'throw' or 'exit'. Did ".
  300. "you forget to add one of those? If you intend to fall through, ".
  301. "add a '// fallthrough' comment to silence this warning.");
  302. }
  303. }
  304. }
  305. }
  306. private function lintBraceFormatting($root) {
  307. foreach ($root->selectDescendantsOfType('n_STATEMENT_LIST') as $list) {
  308. $tokens = $list->getTokens();
  309. if (!$tokens || head($tokens)->getValue() != '{') {
  310. continue;
  311. }
  312. list($before, $after) = $list->getSurroundingNonsemanticTokens();
  313. if (!$before) {
  314. $first = head($tokens);
  315. // Only insert the space if we're after a closing parenthesis. If
  316. // we're in a construct like "else{}", other rules will insert space
  317. // after the 'else' correctly.
  318. $prev = $first->getPrevToken();
  319. if (!$prev || $prev->getValue() != ')') {
  320. continue;
  321. }
  322. $this->raiseLintAtToken(
  323. $first,
  324. self::LINT_BRACE_FORMATTING,
  325. 'Put opening braces on the same line as control statements and '.
  326. 'declarations, with a single space before them.',
  327. ' '.$first->getValue());
  328. } else if (count($before) == 1) {
  329. $before = reset($before);
  330. if ($before->getValue() != ' ') {
  331. $this->raiseLintAtToken(
  332. $before,
  333. self::LINT_BRACE_FORMATTING,
  334. 'Put opening braces on the same line as control statements and '.
  335. 'declarations, with a single space before them.',
  336. ' ');
  337. }
  338. }
  339. }
  340. }
  341. private function lintTautologicalExpressions($root) {
  342. $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
  343. static $operators = array(
  344. '-' => true,
  345. '/' => true,
  346. '-=' => true,
  347. '/=' => true,
  348. '<=' => true,
  349. '<' => true,
  350. '==' => true,
  351. '===' => true,
  352. '!=' => true,
  353. '!==' => true,
  354. '>=' => true,
  355. '>' => true,
  356. );
  357. static $logical = array(
  358. '||' => true,
  359. '&&' => true,
  360. );
  361. foreach ($expressions as $expr) {
  362. $operator = $expr->getChildByIndex(1)->getConcreteString();
  363. if (!empty($operators[$operator])) {
  364. $left = $expr->getChildByIndex(0)->getSemanticString();
  365. $right = $expr->getChildByIndex(2)->getSemanticString();
  366. if ($left == $right) {
  367. $this->raiseLintAtNode(
  368. $expr,
  369. self::LINT_TAUTOLOGICAL_EXPRESSION,
  370. 'Both sides of this expression are identical, so it always '.
  371. 'evaluates to a constant.');
  372. }
  373. }
  374. if (!empty($logical[$operator])) {
  375. $left = $expr->getChildByIndex(0)->getSemanticString();
  376. $right = $expr->getChildByIndex(2)->getSemanticString();
  377. // NOTE: These will be null to indicate "could not evaluate".
  378. $left = $this->evaluateStaticBoolean($left);
  379. $right = $this->evaluateStaticBoolean($right);
  380. if (($operator == '||' && ($left === true || $right === true)) ||
  381. ($operator == '&&' && ($left === false || $right === false))) {
  382. $this->raiseLintAtNode(
  383. $expr,
  384. self::LINT_TAUTOLOGICAL_EXPRESSION,
  385. 'The logical value of this expression is static. Did you forget '.
  386. 'to remove some debugging code?');
  387. }
  388. }
  389. }
  390. }
  391. /**
  392. * Statically evaluate a boolean value from an XHP tree.
  393. *
  394. * TODO: Improve this and move it to XHPAST proper?
  395. *
  396. * @param string The "semantic string" of a single value.
  397. * @return mixed ##true## or ##false## if the value could be evaluated
  398. * statically; ##null## if static evaluation was not possible.
  399. */
  400. private function evaluateStaticBoolean($string) {
  401. switch (strtolower($string)) {
  402. case '0':
  403. case 'null':
  404. case 'false':
  405. return false;
  406. case '1':
  407. case 'true':
  408. return true;
  409. }
  410. return null;
  411. }
  412. protected function lintHashComments($root) {
  413. foreach ($root->selectTokensOfType('T_COMMENT') as $comment) {
  414. $value = $comment->getValue();
  415. if ($value[0] != '#') {
  416. continue;
  417. }
  418. $this->raiseLintAtOffset(
  419. $comment->getOffset(),
  420. self::LINT_COMMENT_STYLE,
  421. 'Use "//" single-line comments, not "#".',
  422. '#',
  423. '//');
  424. }
  425. }
  426. /**
  427. * Find cases where loops get nested inside each other but use the same
  428. * iterator variable. For example:
  429. *
  430. * COUNTEREXAMPLE
  431. * foreach ($list as $thing) {
  432. * foreach ($stuff as $thing) { // <-- Raises an error for reuse of $thing
  433. * // ...
  434. * }
  435. * }
  436. *
  437. */
  438. private function lintReusedIterators($root) {
  439. $used_vars = array();
  440. $for_loops = $root->selectDescendantsOfType('n_FOR');
  441. foreach ($for_loops as $for_loop) {
  442. $var_map = array();
  443. // Find all the variables that are assigned to in the for() expression.
  444. $for_expr = $for_loop->getChildOfType(0, 'n_FOR_EXPRESSION');
  445. $bin_exprs = $for_expr->selectDescendantsOfType('n_BINARY_EXPRESSION');
  446. foreach ($bin_exprs as $bin_expr) {
  447. if ($bin_expr->getChildByIndex(1)->getConcreteString() == '=') {
  448. $var_map[$bin_expr->getChildByIndex(0)->getConcreteString()] = true;
  449. }
  450. }
  451. $used_vars[$for_loop->getID()] = $var_map;
  452. }
  453. $foreach_loops = $root->selectDescendantsOfType('n_FOREACH');
  454. foreach ($foreach_loops as $foreach_loop) {
  455. $var_map = array();
  456. $foreach_expr = $foreach_loop->getChildOftype(0, 'n_FOREACH_EXPRESSION');
  457. // We might use one or two vars, i.e. "foreach ($x as $y => $z)" or
  458. // "foreach ($x as $y)".
  459. $possible_used_vars = array(
  460. $foreach_expr->getChildByIndex(1),
  461. $foreach_expr->getChildByIndex(2),
  462. );
  463. foreach ($possible_used_vars as $var) {
  464. if ($var->getTypeName() == 'n_EMPTY') {
  465. continue;
  466. }
  467. $name = $var->getConcreteString();
  468. $name = trim($name, '&'); // Get rid of ref silliness.
  469. $var_map[$name] = true;
  470. }
  471. $used_vars[$foreach_loop->getID()] = $var_map;
  472. }
  473. $all_loops = $for_loops->add($foreach_loops);
  474. foreach ($all_loops as $loop) {
  475. $child_for_loops = $loop->selectDescendantsOfType('n_FOR');
  476. $child_foreach_loops = $loop->selectDescendantsOfType('n_FOREACH');
  477. $child_loops = $child_for_loops->add($child_foreach_loops);
  478. $outer_vars = $used_vars[$loop->getID()];
  479. foreach ($child_loops as $inner_loop) {
  480. $inner_vars = $used_vars[$inner_loop->getID()];
  481. $shared = array_intersect_key($outer_vars, $inner_vars);
  482. if ($shared) {
  483. $shared_desc = implode(', ', array_keys($shared));
  484. $this->raiseLintAtNode(
  485. $inner_loop->getChildByIndex(0),
  486. self::LINT_REUSED_ITERATORS,
  487. "This loop reuses iterator variables ({$shared_desc}) from an ".
  488. "outer loop. You might be clobbering the outer iterator. Change ".
  489. "the inner loop to use a different iterator name.");
  490. }
  491. }
  492. }
  493. }
  494. protected function lintVariableVariables($root) {
  495. $vvars = $root->selectDescendantsOfType('n_VARIABLE_VARIABLE');
  496. foreach ($vvars as $vvar) {
  497. $this->raiseLintAtNode(
  498. $vvar,
  499. self::LINT_VARIABLE_VARIABLE,
  500. 'Rewrite this code to use an array. Variable variables are unclear '.
  501. 'and hinder static analysis.');
  502. }
  503. }
  504. protected function lintUndeclaredVariables($root) {
  505. // These things declare variables in a function:
  506. // Explicit parameters
  507. // Assignment
  508. // Assignment via list()
  509. // Static
  510. // Global
  511. // Lexical vars
  512. // Builtins ($this)
  513. // foreach()
  514. // catch
  515. //
  516. // These things make lexical scope unknowable:
  517. // Use of extract()
  518. // Assignment to variable variables ($$x)
  519. // Global with variable variables
  520. //
  521. // These things don't count as "using" a variable:
  522. // isset()
  523. // empty()
  524. // Static class variables
  525. //
  526. // The general approach here is to find each function/method declaration,
  527. // then:
  528. //
  529. // 1. Identify all the variable declarations, and where they first occur
  530. // in the function/method declaration.
  531. // 2. Identify all the uses that don't really count (as above).
  532. // 3. Everything else must be a use of a variable.
  533. // 4. For each variable, check if any uses occur before the declaration
  534. // and warn about them.
  535. //
  536. // We also keep track of where lexical scope becomes unknowable (e.g.,
  537. // because the function calls extract() or uses dynamic variables,
  538. // preventing us from keeping track of which variables are defined) so we
  539. // can stop issuing warnings after that.
  540. $fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
  541. $mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
  542. $defs = $fdefs->add($mdefs);
  543. foreach ($defs as $def) {
  544. // We keep track of the first offset where scope becomes unknowable, and
  545. // silence any warnings after that. Default it to INT_MAX so we can min()
  546. // it later to keep track of the first problem we encounter.
  547. $scope_destroyed_at = PHP_INT_MAX;
  548. $declarations = array(
  549. '$this' => 0,
  550. ) + array_fill_keys($this->getSuperGlobalNames(), 0);
  551. $declaration_tokens = array();
  552. $exclude_tokens = array();
  553. $vars = array();
  554. // First up, find all the different kinds of declarations, as explained
  555. // above. Put the tokens into the $vars array.
  556. $param_list = $def->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST');
  557. $param_vars = $param_list->selectDescendantsOfType('n_VARIABLE');
  558. foreach ($param_vars as $var) {
  559. $vars[] = $var;
  560. }
  561. // This is PHP5.3 closure syntax: function () use ($x) {};
  562. $lexical_vars = $def
  563. ->getChildByIndex(4)
  564. ->selectDescendantsOfType('n_VARIABLE');
  565. foreach ($lexical_vars as $var) {
  566. $vars[] = $var;
  567. }
  568. $body = $def->getChildByIndex(5);
  569. if ($body->getTypeName() == 'n_EMPTY') {
  570. // Abstract method declaration.
  571. continue;
  572. }
  573. $static_vars = $body
  574. ->selectDescendantsOfType('n_STATIC_DECLARATION')
  575. ->selectDescendantsOfType('n_VARIABLE');
  576. foreach ($static_vars as $var) {
  577. $vars[] = $var;
  578. }
  579. $global_vars = $body
  580. ->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST');
  581. foreach ($global_vars as $var_list) {
  582. foreach ($var_list->getChildren() as $var) {
  583. if ($var->getTypeName() == 'n_VARIABLE') {
  584. $vars[] = $var;
  585. } else {
  586. // Dynamic global variable, i.e. "global $$x;".
  587. $scope_destroyed_at = min($scope_destroyed_at, $var->getOffset());
  588. // An error is raised elsewhere, no need to raise here.
  589. }
  590. }
  591. }
  592. $catches = $body
  593. ->selectDescendantsOfType('n_CATCH')
  594. ->selectDescendantsOfType('n_VARIABLE');
  595. foreach ($catches as $var) {
  596. $vars[] = $var;
  597. }
  598. $binary = $body->selectDescendantsOfType('n_BINARY_EXPRESSION');
  599. foreach ($binary as $expr) {
  600. if ($expr->getChildByIndex(1)->getConcreteString() != '=') {
  601. continue;
  602. }
  603. $lval = $expr->getChildByIndex(0);
  604. if ($lval->getTypeName() == 'n_VARIABLE') {
  605. $vars[] = $lval;
  606. } else if ($lval->getTypeName() == 'n_LIST') {
  607. // Recursivey grab everything out of list(), since the grammar
  608. // permits list() to be nested. Also note that list() is ONLY valid
  609. // as an lval assignments, so we could safely lift this out of the
  610. // n_BINARY_EXPRESSION branch.
  611. $assign_vars = $lval->selectDescendantsOfType('n_VARIABLE');
  612. foreach ($assign_vars as $var) {
  613. $vars[] = $var;
  614. }
  615. }
  616. if ($lval->getTypeName() == 'n_VARIABLE_VARIABLE') {
  617. $scope_destroyed_at = min($scope_destroyed_at, $lval->getOffset());
  618. // No need to raise here since we raise an error elsewhere.
  619. }
  620. }
  621. $calls = $body->selectDescendantsOfType('n_FUNCTION_CALL');
  622. foreach ($calls as $call) {
  623. $name = strtolower($call->getChildByIndex(0)->getConcreteString());
  624. if ($name == 'empty' || $name == 'isset') {
  625. $params = $call
  626. ->getChildOfType(1, 'n_CALL_PARAMETER_LIST')
  627. ->selectDescendantsOfType('n_VARIABLE');
  628. foreach ($params as $var) {
  629. $exclude_tokens[$var->getID()] = true;
  630. }
  631. continue;
  632. }
  633. if ($name != 'extract') {
  634. continue;
  635. }
  636. $scope_destroyed_at = min($scope_destroyed_at, $call->getOffset());
  637. $this->raiseLintAtNode(
  638. $call,
  639. self::LINT_EXTRACT_USE,
  640. 'Avoid extract(). It is confusing and hinders static analysis.');
  641. }
  642. // Now we have every declaration except foreach(), handled below. Build
  643. // two maps, one which just keeps track of which tokens are part of
  644. // declarations ($declaration_tokens) and one which has the first offset
  645. // where a variable is declared ($declarations).
  646. foreach ($vars as $var) {
  647. $concrete = $this->getConcreteVariableString($var);
  648. $declarations[$concrete] = min(
  649. idx($declarations, $concrete, PHP_INT_MAX),
  650. $var->getOffset());
  651. $declaration_tokens[$var->getID()] = true;
  652. }
  653. // Excluded tokens are ones we don't "count" as being uses, described
  654. // above. Put them into $exclude_tokens.
  655. $class_statics = $body
  656. ->selectDescendantsOfType('n_CLASS_STATIC_ACCESS');
  657. $class_static_vars = $class_statics
  658. ->selectDescendantsOfType('n_VARIABLE');
  659. foreach ($class_static_vars as $var) {
  660. $exclude_tokens[$var->getID()] = true;
  661. }
  662. // Find all the variables in scope, and figure out where they are used.
  663. // We want to find foreach() iterators which are both declared before and
  664. // used after the foreach() loop.
  665. $uses = array();
  666. $all_vars = $body->selectDescendantsOfType('n_VARIABLE');
  667. $all = array();
  668. // NOTE: $all_vars is not a real array so we can't unset() it.
  669. foreach ($all_vars as $var) {
  670. // Be strict since it's easier; we don't let you reuse an iterator you
  671. // declared before a loop after the loop, even if you're just assigning
  672. // to it.
  673. $concrete = $var->getConcreteString();
  674. $uses[$concrete][$var->getID()] = $var->getOffset();
  675. if (isset($declaration_tokens[$var->getID()])) {
  676. // We know this is part of a declaration, so it's fine.
  677. continue;
  678. }
  679. if (isset($exclude_tokens[$var->getID()])) {
  680. // We know this is part of isset() or similar, so it's fine.
  681. continue;
  682. }
  683. $all[$var->getID()] = $var;
  684. }
  685. // Do foreach() last, we want to handle implicit redeclaration of a
  686. // variable already in scope since this probably means we're ovewriting a
  687. // local.
  688. // NOTE: Processing foreach expressions in order allows programs which
  689. // reuse iterator variables in other foreach() loops -- this is fine. We
  690. // have a separate warning to prevent nested loops from reusing the same
  691. // iterators.
  692. $foreaches = $body->selectDescendantsOfType('n_FOREACH');
  693. $all_foreach_vars = array();
  694. foreach ($foreaches as $foreach) {
  695. $foreach_expr = $foreach->getChildOfType(0, 'n_FOREACH_EXPRESSION');
  696. $foreach_vars = array();
  697. // Determine the end of the foreach() loop.
  698. $foreach_tokens = $foreach->getTokens();
  699. $last_token = end($foreach_tokens);
  700. $foreach_end = $last_token->getOffset();
  701. $key_var = $foreach_expr->getChildByIndex(1);
  702. if ($key_var->getTypeName() == 'n_VARIABLE') {
  703. $foreach_vars[] = $key_var;
  704. }
  705. $value_var = $foreach_expr->getChildByIndex(2);
  706. if ($value_var->getTypeName() == 'n_VARIABLE') {
  707. $foreach_vars[] = $value_var;
  708. } else {
  709. // The root-level token may be a reference, as in:
  710. // foreach ($a as $b => &$c) { ... }
  711. // Reach into the n_VARIABLE_REFERENCE node to grab the n_VARIABLE
  712. // node.
  713. $foreach_vars[] = $value_var->getChildOfType(0, 'n_VARIABLE');
  714. }
  715. // Remove all uses of the iterators inside of the foreach() loop from
  716. // the $uses map.
  717. foreach ($foreach_vars as $var) {
  718. $concrete = $this->getConcreteVariableString($var);
  719. $offset = $var->getOffset();
  720. foreach ($uses[$concrete] as $id => $use_offset) {
  721. if (($use_offset >= $offset) && ($use_offset < $foreach_end)) {
  722. unset($uses[$concrete][$id]);
  723. }
  724. }
  725. $all_foreach_vars[] = $var;
  726. }
  727. }
  728. foreach ($all_foreach_vars as $var) {
  729. $concrete = $this->getConcreteVariableString($var);
  730. $offset = $var->getOffset();
  731. // If a variable was declared before a foreach() and is used after
  732. // it, raise a message.
  733. if (isset($declarations[$concrete])) {
  734. if ($declarations[$concrete] < $offset) {
  735. if (!empty($uses[$concrete]) &&
  736. max($uses[$concrete]) > $offset) {
  737. $this->raiseLintAtNode(
  738. $var,
  739. self::LINT_REUSED_AS_ITERATOR,
  740. 'This iterator variable is a previously declared local '.
  741. 'variable. To avoid overwriting locals, do not reuse them '.
  742. 'as iterator variables.');
  743. }
  744. }
  745. }
  746. // This is a declration, exclude it from the "declare variables prior
  747. // to use" check below.
  748. unset($all[$var->getID()]);
  749. $vars[] = $var;
  750. }
  751. // Now rebuild declarations to include foreach().
  752. foreach ($vars as $var) {
  753. $concrete = $this->getConcreteVariableString($var);
  754. $declarations[$concrete] = min(
  755. idx($declarations, $concrete, PHP_INT_MAX),
  756. $var->getOffset());
  757. $declaration_tokens[$var->getID()] = true;
  758. }
  759. // Issue a warning for every variable token, unless it appears in a
  760. // declaration, we know about a prior declaration, we have explicitly
  761. // exlcuded it, or scope has been made unknowable before it appears.
  762. $issued_warnings = array();
  763. foreach ($all as $id => $var) {
  764. if ($var->getOffset() >= $scope_destroyed_at) {
  765. // This appears after an extract() or $$var so we have no idea
  766. // whether it's legitimate or not. We raised a harshly-worded warning
  767. // when scope was made unknowable, so just ignore anything we can't
  768. // figure out.
  769. continue;
  770. }
  771. $concrete = $this->getConcreteVariableString($var);
  772. if ($var->getOffset() >= idx($declarations, $concrete, PHP_INT_MAX)) {
  773. // The use appears after the variable is declared, so it's fine.
  774. continue;
  775. }
  776. if (!empty($issued_warnings[$concrete])) {
  777. // We've already issued a warning for this variable so we don't need
  778. // to issue another one.
  779. continue;
  780. }
  781. $this->raiseLintAtNode(
  782. $var,
  783. self::LINT_UNDECLARED_VARIABLE,
  784. 'Declare variables prior to use (even if you are passing them '.
  785. 'as reference parameters). You may have misspelled this '.
  786. 'variable name.');
  787. $issued_warnings[$concrete] = true;
  788. }
  789. }
  790. }
  791. private function getConcreteVariableString($var) {
  792. $concrete = $var->getConcreteString();
  793. // Strip off curly braces as in $obj->{$property}.
  794. $concrete = trim($concrete, '{}');
  795. return $concrete;
  796. }
  797. protected function lintPHPTagUse($root) {
  798. $tokens = $root->getTokens();
  799. foreach ($tokens as $token) {
  800. if ($token->getTypeName() == 'T_OPEN_TAG') {
  801. if (trim($token->getValue()) == '<?') {
  802. $this->raiseLintAtToken(
  803. $token,
  804. self::LINT_PHP_SHORT_TAG,
  805. 'Use the full form of the PHP open tag, "<?php".',
  806. "<?php\n");
  807. }
  808. break;
  809. } else if ($token->getTypeName() == 'T_OPEN_TAG_WITH_ECHO') {
  810. $this->raiseLintAtToken(
  811. $token,
  812. self::LINT_PHP_ECHO_TAG,
  813. 'Avoid the PHP echo short form, "<?=".');
  814. break;
  815. } else {
  816. if (!preg_match('/^#!/', $token->getValue())) {
  817. $this->raiseLintAtToken(
  818. $token,
  819. self::LINT_PHP_OPEN_TAG,
  820. 'PHP files should start with "<?php", which may be preceded by '.
  821. 'a "#!" line for scripts.');
  822. }
  823. break;
  824. }
  825. }
  826. foreach ($root->selectTokensOfType('T_CLOSE_TAG') as $token) {
  827. $this->raiseLintAtToken(
  828. $token,
  829. self::LINT_PHP_CLOSE_TAG,
  830. 'Do not use the PHP closing tag, "?>".');
  831. }
  832. }
  833. protected function lintNamingConventions($root) {
  834. // We're going to build up a list of <type, name, token, error> tuples
  835. // and then try to instantiate a hook class which has the opportunity to
  836. // override us.
  837. $names = array();
  838. $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
  839. foreach ($classes as $class) {
  840. $name_token = $class->getChildByIndex(1);
  841. $name_string = $name_token->getConcreteString();
  842. $names[] = array(
  843. 'class',
  844. $name_string,
  845. $name_token,
  846. ArcanistXHPASTLintNamingHook::isUpperCamelCase($name_string)
  847. ? null
  848. : 'Follow naming conventions: classes should be named using '.
  849. 'UpperCamelCase.',
  850. );
  851. }
  852. $ifaces = $root->selectDescendantsOfType('n_INTERFACE_DECLARATION');
  853. foreach ($ifaces as $iface) {
  854. $name_token = $iface->getChildByIndex(1);
  855. $name_string = $name_token->getConcreteString();
  856. $names[] = array(
  857. 'interface',
  858. $name_string,
  859. $name_token,
  860. ArcanistXHPASTLintNamingHook::isUpperCamelCase($name_string)
  861. ? null
  862. : 'Follow naming conventions: interfaces should be named using '.
  863. 'UpperCamelCase.',
  864. );
  865. }
  866. $functions = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
  867. foreach ($functions as $function) {
  868. $name_token = $function->getChildByIndex(2);
  869. if ($name_token->getTypeName() == 'n_EMPTY') {
  870. // Unnamed closure.
  871. continue;
  872. }
  873. $name_string = $name_token->getConcreteString();
  874. $names[] = array(
  875. 'function',
  876. $name_string,
  877. $name_token,
  878. ArcanistXHPASTLintNamingHook::isLowercaseWithUnderscores(
  879. ArcanistXHPASTLintNamingHook::stripPHPFunction($name_string))
  880. ? null
  881. : 'Follow naming conventions: functions should be named using '.
  882. 'lowercase_with_underscores.',
  883. );
  884. }
  885. $methods = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
  886. foreach ($methods as $method) {
  887. $name_token = $method->getChildByIndex(2);
  888. $name_string = $name_token->getConcreteString();
  889. $names[] = array(
  890. 'method',
  891. $name_string,
  892. $name_token,
  893. ArcanistXHPASTLintNamingHook::isLowerCamelCase(
  894. ArcanistXHPASTLintNamingHook::stripPHPFunction($name_string))
  895. ? null
  896. : 'Follow naming conventions: methods should be named using '.
  897. 'lowerCamelCase.',
  898. );
  899. }
  900. $param_tokens = array();
  901. $params = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER_LIST');
  902. foreach ($params as $param_list) {
  903. foreach ($param_list->getChildren() as $param) {
  904. $name_token = $param->getChildByIndex(1);
  905. if ($name_token->getTypeName() == 'n_VARIABLE_REFERENCE') {
  906. $name_token = $name_token->getChildOfType(0, 'n_VARIABLE');
  907. }
  908. $param_tokens[$name_token->getID()] = true;
  909. $name_string = $name_token->getConcreteString();
  910. $names[] = array(
  911. 'parameter',
  912. $name_string,
  913. $name_token,
  914. ArcanistXHPASTLintNamingHook::isLowercaseWithUnderscores(
  915. ArcanistXHPASTLintNamingHook::stripPHPVariable($name_string))
  916. ? null
  917. : 'Follow naming conventions: parameters should be named using '.
  918. 'lowercase_with_underscores.',
  919. );
  920. }
  921. }
  922. $constants = $root->selectDescendantsOfType(
  923. 'n_CLASS_CONSTANT_DECLARATION_LIST');
  924. foreach ($constants as $constant_list) {
  925. foreach ($constant_list->getChildren() as $constant) {
  926. $name_token = $constant->getChildByIndex(0);
  927. $name_string = $name_token->getConcreteString();
  928. $names[] = array(
  929. 'constant',
  930. $name_string,
  931. $name_token,
  932. ArcanistXHPASTLintNamingHook::isUppercaseWithUnderscores($name_string)
  933. ? null
  934. : 'Follow naming conventions: class constants should be named '.
  935. 'using UPPERCASE_WITH_UNDERSCORES.',
  936. );
  937. }
  938. }
  939. $member_tokens = array();
  940. $props = $root->selectDescendantsOfType('n_CLASS_MEMBER_DECLARATION_LIST');
  941. foreach ($props as $prop_list) {
  942. foreach ($prop_list->getChildren() as $token_id => $prop) {
  943. if ($prop->getTypeName() == 'n_CLASS_MEMBER_MODIFIER_LIST') {
  944. continue;
  945. }
  946. $name_token = $prop->getChildByIndex(0);
  947. $member_tokens[$name_token->getID()] = true;
  948. $name_string = $name_token->getConcreteString();
  949. $names[] = array(
  950. 'member',
  951. $name_string,
  952. $name_token,
  953. ArcanistXHPASTLintNamingHook::isLowerCamelCase(
  954. ArcanistXHPASTLintNamingHook::stripPHPVariable($name_string))
  955. ? null
  956. : 'Follow naming conventions: class properties should be named '.
  957. 'using lowerCamelCase.',
  958. );
  959. }
  960. }
  961. $superglobal_map = array_fill_keys(
  962. $this->getSuperGlobalNames(),
  963. true);
  964. $fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
  965. $mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
  966. $defs = $fdefs->add($mdefs);
  967. foreach ($defs as $def) {
  968. $globals = $def->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST');
  969. $globals = $globals->selectDescendantsOfType('n_VARIABLE');
  970. $globals_map = array();
  971. foreach ($globals as $global) {
  972. $global_string = $global->getConcreteString();
  973. $globals_map[$global_string] = true;
  974. $names[] = array(
  975. 'global',
  976. $global_string,
  977. $global,
  978. // No advice for globals, but hooks have an option to provide some.
  979. null);
  980. }
  981. // Exclude access of static properties, since lint will be raised at
  982. // their declaration if they're invalid and they may not conform to
  983. // variable rules. This is slightly overbroad (includes the entire
  984. // rhs of a "Class::..." token) to cover cases like "Class:$x[0]". These
  985. // varaibles are simply made exempt from naming conventions.
  986. $exclude_tokens = array();
  987. $statics = $def->selectDescendantsOfType('n_CLASS_STATIC_ACCESS');
  988. foreach ($statics as $static) {
  989. $rhs = $static->getChildByIndex(1);
  990. $rhs_vars = $def->selectDescendantsOfType('n_VARIABLE');
  991. foreach ($rhs_vars as $var) {
  992. $exclude_tokens[$var->getID()] = true;
  993. }
  994. }
  995. $vars = $def->selectDescendantsOfType('n_VARIABLE');
  996. foreach ($vars as $token_id => $var) {
  997. if (isset($member_tokens[$token_id])) {
  998. continue;
  999. }
  1000. if (isset($param_tokens[$token_id])) {
  1001. continue;
  1002. }
  1003. if (isset($exclude_tokens[$token_id])) {
  1004. continue;
  1005. }
  1006. $var_string = $var->getConcreteString();
  1007. // Awkward artifact of "$o->{$x}".
  1008. $var_string = trim($var_string, '{}');
  1009. if (isset($superglobal_map[$var_string])) {
  1010. continue;
  1011. }
  1012. if (isset($globals_map[$var_string])) {
  1013. continue;
  1014. }
  1015. $names[] = array(
  1016. 'variable',
  1017. $var_string,
  1018. $var,
  1019. ArcanistXHPASTLintNamingHook::isLowercaseWithUnderscores(
  1020. ArcanistXHPASTLintNamingHook::stripPHPVariable($var_string))
  1021. ? null
  1022. : 'Follow naming conventions: variables should be named using '.
  1023. 'lowercase_with_underscores.',
  1024. );
  1025. }
  1026. }
  1027. $engine = $this->getEngine();
  1028. $working_copy = $engine->getWorkingCopy();
  1029. if ($working_copy) {
  1030. // If a naming hook is configured, give it a chance to override the
  1031. // default results for all the symbol names.
  1032. $hook_class = $working_copy->getConfig('lint.xhpast.naminghook');
  1033. if ($hook_class) {
  1034. $hook_obj = newv($hook_class, array());
  1035. foreach ($names as $k => $name_attrs) {
  1036. list($type, $name, $token, $default) = $name_attrs;
  1037. $result = $hook_obj->lintSymbolName($type, $name, $default);
  1038. $names[$k][3] = $result;
  1039. }
  1040. }
  1041. }
  1042. // Raise anything we're left with.
  1043. foreach ($names as $k => $name_attrs) {
  1044. list($type, $name, $token, $result) = $name_attrs;
  1045. if ($result) {
  1046. $this->raiseLintAtNode(
  1047. $token,
  1048. self::LINT_NAMING_CONVENTIONS,
  1049. $result);
  1050. }
  1051. }
  1052. }
  1053. protected function lintSurpriseConstructors($root) {
  1054. $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
  1055. foreach ($classes as $class) {
  1056. $class_name = $class->getChildByIndex(1)->getConcreteString();
  1057. $methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION');
  1058. foreach ($methods as $method) {
  1059. $method_name_token = $method->getChildByIndex(2);
  1060. $method_name = $method_name_token->getConcreteString();
  1061. if (strtolower($class_name) == strtolower($method_name)) {
  1062. $this->raiseLintAtNode(
  1063. $method_name_token,
  1064. self::LINT_IMPLICIT_CONSTRUCTOR,
  1065. 'Name constructors __construct() explicitly. This method is a '.
  1066. 'constructor because it has the same name as the class it is '.
  1067. 'defined in.');
  1068. }
  1069. }
  1070. }
  1071. }
  1072. protected function lintParenthesesShouldHugExpressions($root) {
  1073. $calls = $root->selectDescendantsOfType('n_CALL_PARAMETER_LIST');
  1074. $controls = $root->selectDescendantsOfType('n_CONTROL_CONDITION');
  1075. $fors = $root->selectDescendantsOfType('n_FOR_EXPRESSION');
  1076. $foreach = $root->selectDescendantsOfType('n_FOREACH_EXPRESSION');
  1077. $decl = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER_LIST');
  1078. $all_paren_groups = $calls
  1079. ->add($controls)
  1080. ->add($fors)
  1081. ->add($foreach)
  1082. ->add($decl);
  1083. foreach ($all_paren_groups as $group) {
  1084. $tokens = $group->getTokens();
  1085. $token_o = array_shift($tokens);
  1086. $token_c = array_pop($tokens);
  1087. if ($token_o->getTypeName() != '(') {
  1088. throw new Exception('Expected open paren!');
  1089. }
  1090. if ($token_c->getTypeName() != ')') {
  1091. throw new Exception('Expected close paren!');
  1092. }
  1093. $nonsem_o = $token_o->getNonsemanticTokensAfter();
  1094. $nonsem_c = $token_c->getNonsemanticTokensBefore();
  1095. if (!$nonsem_o) {
  1096. continue;
  1097. }
  1098. $raise = array();
  1099. $string_o = implode('', mpull($nonsem_o, 'getValue'));
  1100. if (preg_match('/^[ ]+$/', $string_o)) {
  1101. $raise[] = array($nonsem_o, $string_o);
  1102. }
  1103. if ($nonsem_o !== $nonsem_c) {
  1104. $string_c = implode('', mpull($nonsem_c, 'getValue'));
  1105. if (preg_match('/^[ ]+$/', $string_c)) {
  1106. $raise[] = array($nonsem_c, $string_c);
  1107. }
  1108. }
  1109. foreach ($raise as $warning) {
  1110. list($tokens, $string) = $warning;
  1111. $this->raiseLintAtOffset(
  1112. reset($tokens)->getOffset(),
  1113. self::LINT_PARENTHESES_SPACING,
  1114. 'Parentheses should hug their contents.',
  1115. $string,
  1116. '');
  1117. }
  1118. }
  1119. }
  1120. protected function lintSpaceAfterControlStatementKeywords($root) {
  1121. foreach ($root->getTokens() as $id => $token) {
  1122. switch ($token->getTypeName()) {
  1123. case 'T_IF':
  1124. case 'T_ELSE':
  1125. case 'T_FOR':
  1126. case 'T_FOREACH':
  1127. case 'T_WHILE':
  1128. case 'T_DO':
  1129. case 'T_SWITCH':
  1130. $after = $token->getNonsemanticTokensAfter();
  1131. if (empty($after)) {
  1132. $this->raiseLintAtToken(
  1133. $token,
  1134. self::LINT_CONTROL_STATEMENT_SPACING,
  1135. 'Convention: put a space after control statements.',
  1136. $token->getValue().' ');
  1137. } else if (count($after) == 1) {
  1138. $space = head($after);
  1139. // If we have an else clause with braces, $space may not be
  1140. // a single white space. e.g.,
  1141. //
  1142. // if ($x)
  1143. // echo 'foo'
  1144. // else // <- $space is not " " but "\n ".
  1145. // echo 'bar'
  1146. //
  1147. // We just require it starts with either a whitespace or a newline.
  1148. if ($token->getTypeName() == 'T_ELSE' ||
  1149. $token->getTypeName() == 'T_DO') {
  1150. break;
  1151. }
  1152. if ($space->isAnyWhitespace() && $space->getValue() != ' ') {
  1153. $this->raiseLintAtToken(
  1154. $space,
  1155. self::LINT_CONTROL_STATEMENT_SPACING,
  1156. 'Convention: put a single space after control statements.',
  1157. ' ');
  1158. }
  1159. }
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. protected function lintSpaceAroundBinaryOperators($root) {
  1165. // NOTE: '.' is parsed as n_CONCATENATION_LIST, not n_BINARY_EXPRESSION,
  1166. // so we don't select it here.
  1167. $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
  1168. foreach ($expressions as $expression) {
  1169. $operator = $expression->getChildByIndex(1);
  1170. $operator_value = $operator->getConcreteString();
  1171. list($before, $after) = $operator->getSurroundingNonsemanticTokens();
  1172. $replace = null;
  1173. if (empty($before) && empty($after)) {
  1174. $replace = " {$operator_value} ";
  1175. } else if (empty($before)) {
  1176. $replace = " {$operator_value}";
  1177. } else if (empty($after)) {
  1178. $replace = "{$operator_value} ";
  1179. }
  1180. if ($replace !== null) {
  1181. $this->raiseLintAtNode(
  1182. $operator,
  1183. self::LINT_BINARY_EXPRESSION_SPACING,
  1184. 'Convention: logical and arithmetic operators should be '.
  1185. 'surrounded by whitespace.',
  1186. $replace);
  1187. }
  1188. }
  1189. $tokens = $root->selectTokensOfType(',');
  1190. foreach ($tokens as $token) {
  1191. $next = $token->getNextToken();
  1192. switch ($next->getTypeName()) {
  1193. case ')':
  1194. case 'T_WHITESPACE':
  1195. break;
  1196. break;
  1197. default:
  1198. $this->raiseLintAtToken(
  1199. $token,
  1200. self::LINT_BINARY_EXPRESSION_SPACING,
  1201. 'Convention: comma should be followed by space.',
  1202. ', ');
  1203. break;
  1204. }
  1205. }
  1206. // TODO: Spacing around ".".
  1207. // TODO: Spacing around default parameter assignment in function/method
  1208. // declarations (which is not n_BINARY_EXPRESSION).
  1209. }
  1210. protected function lintDynamicDefines($root) {
  1211. $calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
  1212. foreach ($calls as $call) {
  1213. $name = $call->getChildByIndex(0)->getConcreteString();
  1214. if (strtolower($name) == 'define') {
  1215. $parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
  1216. $defined = $parameter_list->getChildByIndex(0);
  1217. if (!$defined->isStaticScalar()) {
  1218. $this->raiseLintAtNode(
  1219. $defined,
  1220. self::LINT_DYNAMIC_DEFINE,
  1221. 'First argument to define() must be a string literal.');
  1222. }
  1223. }
  1224. }
  1225. }
  1226. protected function lintUseOfThisInStaticMethods($root) {
  1227. $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
  1228. foreach ($classes as $class) {
  1229. $methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION');
  1230. foreach ($methods as $method) {
  1231. $attributes = $method
  1232. ->getChildByIndex(0, 'n_METHOD_MODIFIER_LIST')
  1233. ->selectDescendantsOfType('n_STRING');
  1234. $method_is_static = false;
  1235. $method_is_abstract = false;
  1236. foreach ($attributes as $attribute) {
  1237. if (strtolower($attribute->getConcreteString()) == 'static') {
  1238. $method_is_static = true;
  1239. }
  1240. if (strtolower($attribute->getConcreteString()) == 'abstract') {
  1241. $method_is_abstract = true;
  1242. }
  1243. }
  1244. if ($method_is_abstract) {
  1245. continue;
  1246. }
  1247. if (!$method_is_static) {
  1248. continue;
  1249. }
  1250. $body = $method->getChildOfType(5, 'n_STATEMENT_LIST');
  1251. $variables = $body->selectDescendantsOfType('n_VARIABLE');
  1252. foreach ($variables as $variable) {
  1253. if ($method_is_static &&
  1254. strtolower($variable->getConcreteString()) == '$this') {
  1255. $this->raiseLintAtNode(
  1256. $variable,
  1257. self::LINT_STATIC_THIS,
  1258. 'You can not reference "$this" inside a static method.');
  1259. }
  1260. }
  1261. }
  1262. }
  1263. }
  1264. /**
  1265. * preg_quote() takes two arguments, but the second one is optional because

Large files files are truncated, but you can click here to view the full file