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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

final class ArcanistControlStatementSpacingXHPASTLinterRule
  extends ArcanistXHPASTLinterRule {

  const ID = 26;

  public function getLintName() {
    return pht('Space After Control Statement');
  }

  public function getLintSeverity() {
    return ArcanistLintSeverity::SEVERITY_WARNING;
  }

  public function process(XHPASTNode $root) {
    foreach ($root->getTokens() as $id => $token) {
      switch ($token->getTypeName()) {
        case 'T_IF':
        case 'T_ELSE':
        case 'T_FOR':
        case 'T_FOREACH':
        case 'T_WHILE':
        case 'T_DO':
        case 'T_SWITCH':
          $after = $token->getNonsemanticTokensAfter();
          if (empty($after)) {
            $this->raiseLintAtToken(
              $token,
              pht('Convention: put a space after control statements.'),
              $token->getValue().' ');
          } else if (count($after) === 1) {
            $space = head($after);

            // If we have an else clause with braces, $space may not be
            // a single white space. e.g.,
            //
            //   if ($x)
            //     echo 'foo'
            //   else          // <- $space is not " " but "\n  ".
            //     echo 'bar'
            //
            // We just require it starts with either a whitespace or a newline.
            if ($token->getTypeName() === 'T_ELSE' ||
                $token->getTypeName() === 'T_DO') {
              break;
            }

            if ($space->isAnyWhitespace() && $space->getValue() !== ' ') {
              $this->raiseLintAtToken(
                $space,
                pht('Convention: put a single space after control statements.'),
                ' ');
            }
          }
          break;
      }
    }
  }

}