~ubuntu-branches/ubuntu/wily/phabricator/wily

« back to all changes in this revision

Viewing changes to arcanist/src/lint/linter/xhpast/rules/ArcanistControlStatementSpacingXHPASTLinterRule.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2015-06-13 10:52:10 UTC
  • mfrom: (0.30.1) (0.29.1) (0.17.4) (2.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20150613105210-5uirr7tvnk0n6e6y
Tags: 0~git20150613-1
* New snapshot release (closes: #787805)
* fixed typo in logrotate script (closes: #787645)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
final class ArcanistControlStatementSpacingXHPASTLinterRule
 
4
  extends ArcanistXHPASTLinterRule {
 
5
 
 
6
  const ID = 26;
 
7
 
 
8
  public function getLintName() {
 
9
    return pht('Space After Control Statement');
 
10
  }
 
11
 
 
12
  public function getLintSeverity() {
 
13
    return ArcanistLintSeverity::SEVERITY_WARNING;
 
14
  }
 
15
 
 
16
  public function process(XHPASTNode $root) {
 
17
    foreach ($root->getTokens() as $id => $token) {
 
18
      switch ($token->getTypeName()) {
 
19
        case 'T_IF':
 
20
        case 'T_ELSE':
 
21
        case 'T_FOR':
 
22
        case 'T_FOREACH':
 
23
        case 'T_WHILE':
 
24
        case 'T_DO':
 
25
        case 'T_SWITCH':
 
26
          $after = $token->getNonsemanticTokensAfter();
 
27
          if (empty($after)) {
 
28
            $this->raiseLintAtToken(
 
29
              $token,
 
30
              pht('Convention: put a space after control statements.'),
 
31
              $token->getValue().' ');
 
32
          } else if (count($after) === 1) {
 
33
            $space = head($after);
 
34
 
 
35
            // If we have an else clause with braces, $space may not be
 
36
            // a single white space. e.g.,
 
37
            //
 
38
            //   if ($x)
 
39
            //     echo 'foo'
 
40
            //   else          // <- $space is not " " but "\n  ".
 
41
            //     echo 'bar'
 
42
            //
 
43
            // We just require it starts with either a whitespace or a newline.
 
44
            if ($token->getTypeName() === 'T_ELSE' ||
 
45
                $token->getTypeName() === 'T_DO') {
 
46
              break;
 
47
            }
 
48
 
 
49
            if ($space->isAnyWhitespace() && $space->getValue() !== ' ') {
 
50
              $this->raiseLintAtToken(
 
51
                $space,
 
52
                pht('Convention: put a single space after control statements.'),
 
53
                ' ');
 
54
            }
 
55
          }
 
56
          break;
 
57
      }
 
58
    }
 
59
  }
 
60
 
 
61
}