~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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php

final class ArcanistPhutilXHPASTLinter extends ArcanistBaseXHPASTLinter {

  const LINT_ARRAY_COMBINE          = 2;
  const LINT_DEPRECATED_FUNCTION    = 3;
  const LINT_UNSAFE_DYNAMIC_STRING  = 4;
  const LINT_RAGGED_CLASSTREE_EDGE  = 5;

  private $deprecatedFunctions    = array();
  private $dynamicStringFunctions = array();
  private $dynamicStringClasses   = array();

  public function getInfoName() {
    return 'XHPAST/libphutil Lint';
  }

  public function getInfoDescription() {
    return pht(
      'Use XHPAST to run libphutil-specific rules on a PHP library. This '.
      'linter is intended for use in Phabricator libraries and extensions.');
  }

  public function setDeprecatedFunctions(array $map) {
    $this->deprecatedFunctions = $map;
    return $this;
  }

  public function setDynamicStringFunctions(array $map) {
    $this->dynamicStringFunctions = $map;
    return $this;
  }

  public function setDynamicStringClasses(array $map) {
    $this->dynamicStringClasses = $map;
    return $this;
  }

  public function getLintNameMap() {
    return array(
      self::LINT_ARRAY_COMBINE          => pht(
        '%s Unreliable',
        'array_combine()'),
      self::LINT_DEPRECATED_FUNCTION    => pht(
        'Use of Deprecated Function'),
      self::LINT_UNSAFE_DYNAMIC_STRING  => pht(
        'Unsafe Usage of Dynamic String'),
      self::LINT_RAGGED_CLASSTREE_EDGE  => pht(
        'Class Not %s Or %s',
        'abstract',
        'final'),
    );
  }

  public function getLintSeverityMap() {
    $warning = ArcanistLintSeverity::SEVERITY_WARNING;
    return array(
      self::LINT_ARRAY_COMBINE          => $warning,
      self::LINT_DEPRECATED_FUNCTION    => $warning,
      self::LINT_UNSAFE_DYNAMIC_STRING  => $warning,
      self::LINT_RAGGED_CLASSTREE_EDGE  => $warning,
    );
  }

  public function getLinterName() {
    return 'PHLXHP';
  }

  public function getLinterConfigurationName() {
    return 'phutil-xhpast';
  }

  public function getVersion() {
    // The version number should be incremented whenever a new rule is added.
    return '3';
  }

  public function getLinterConfigurationOptions() {
    $options = array(
      'phutil-xhpast.deprecated.functions' => array(
        'type' => 'optional map<string, string>',
        'help' => pht(
          'Functions which should should be considered deprecated.'),
      ),
      'phutil-xhpast.dynamic-string.functions' => array(
        'type' => 'optional map<string, string>',
        'help' => pht(
          'Functions which should should not be used because they represent '.
          'the unsafe usage of dynamic strings.'),
      ),
      'phutil-xhpast.dynamic-string.classes' => array(
        'type' => 'optional map<string, string>',
        'help' => pht(
          'Classes which should should not be used because they represent the '.
          'unsafe usage of dynamic strings.'),
      ),
    );

    return $options + parent::getLinterConfigurationOptions();
  }

  public function setLinterConfigurationValue($key, $value) {
    switch ($key) {
      case 'phutil-xhpast.deprecated.functions':
        $this->setDeprecatedFunctions($value);
        return;
      case 'phutil-xhpast.dynamic-string.functions':
        $this->setDynamicStringFunctions($value);
        return;
      case 'phutil-xhpast.dynamic-string.classes':
        $this->setDynamicStringClasses($value);
        return;
    }

    return parent::setLinterConfigurationValue($key, $value);
  }

  protected function resolveFuture($path, Future $future) {
    $tree = $this->getXHPASTLinter()->getXHPASTTreeForPath($path);
    if (!$tree) {
      return;
    }

    $root = $tree->getRootNode();

    $method_codes = array(
      'lintArrayCombine' => self::LINT_ARRAY_COMBINE,
      'lintUnsafeDynamicString' => self::LINT_UNSAFE_DYNAMIC_STRING,
      'lintDeprecatedFunctions' => self::LINT_DEPRECATED_FUNCTION,
      'lintRaggedClasstreeEdges' => self::LINT_RAGGED_CLASSTREE_EDGE,
    );

    foreach ($method_codes as $method => $codes) {
      foreach ((array)$codes as $code) {
        if ($this->isCodeEnabled($code)) {
          call_user_func(array($this, $method), $root);
          break;
        }
      }
    }
  }

  private function lintUnsafeDynamicString(XHPASTNode $root) {
    $safe = $this->dynamicStringFunctions + array(
      'pht' => 0,

      'hsprintf' => 0,
      'jsprintf' => 0,

      'hgsprintf' => 0,

      'csprintf' => 0,
      'vcsprintf' => 0,
      'execx' => 0,
      'exec_manual' => 0,
      'phutil_passthru' => 0,

      'qsprintf' => 1,
      'vqsprintf' => 1,
      'queryfx' => 1,
      'vqueryfx' => 1,
      'queryfx_all' => 1,
      'vqueryfx_all' => 1,
      'queryfx_one' => 1,
    );

    $calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
    $this->lintUnsafeDynamicStringCall($calls, $safe);

    $safe = $this->dynamicStringClasses + array(
      'ExecFuture' => 0,
    );

    $news = $root->selectDescendantsOfType('n_NEW');
    $this->lintUnsafeDynamicStringCall($news, $safe);
  }

  private function lintUnsafeDynamicStringCall(
    AASTNodeList $calls,
    array $safe) {

    $safe = array_combine(
      array_map('strtolower', array_keys($safe)),
      $safe);

    foreach ($calls as $call) {
      $name = $call->getChildByIndex(0)->getConcreteString();
      $param = idx($safe, strtolower($name));

      if ($param === null) {
        continue;
      }

      $parameters = $call->getChildByIndex(1);
      if (count($parameters->getChildren()) <= $param) {
        continue;
      }

      $identifier = $parameters->getChildByIndex($param);
      if (!$identifier->isConstantString()) {
        $this->raiseLintAtNode(
          $call,
          self::LINT_UNSAFE_DYNAMIC_STRING,
          pht(
            "Parameter %d of %s should be a scalar string, ".
            "otherwise it's not safe.",
            $param + 1,
            $name.'()'));
      }
    }
  }

  private function lintArrayCombine(XHPASTNode $root) {
    $function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
    foreach ($function_calls as $call) {
      $name = $call->getChildByIndex(0)->getConcreteString();
      if (strcasecmp($name, 'array_combine') == 0) {
        $parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
        if (count($parameter_list->getChildren()) !== 2) {
          // Wrong number of parameters, but raise that elsewhere if we want.
          continue;
        }

        $first = $parameter_list->getChildByIndex(0);
        $second = $parameter_list->getChildByIndex(1);

        if ($first->getConcreteString() == $second->getConcreteString()) {
          $this->raiseLintAtNode(
            $call,
            self::LINT_ARRAY_COMBINE,
            pht(
              'Prior to PHP 5.4, `%s` fails when given empty arrays. '.
              'Prefer to write `%s` as `%s`.',
              'array_combine()',
              'array_combine(x, x)',
              'array_fuse(x)'));
        }
      }
    }
  }

  private function lintDeprecatedFunctions(XHPASTNode $root) {
    $map = $this->deprecatedFunctions;

    $function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
    foreach ($function_calls as $call) {
      $name = $call->getChildByIndex(0)->getConcreteString();

      $name = strtolower($name);
      if (empty($map[$name])) {
        continue;
      }

      $this->raiseLintAtNode(
        $call,
        self::LINT_DEPRECATED_FUNCTION,
        $map[$name]);
    }
  }

  private function lintRaggedClasstreeEdges(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'),
          self::LINT_RAGGED_CLASSTREE_EDGE,
          pht(
            "This class is neither '%s' nor '%s', and does not have ".
            "a docblock marking it '%s'.",
            'final',
            'abstract',
            '@concrete-extensible'));
      }
    }
  }

}