2013-01-29 07:03:03 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class ArcanistPhutilXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
|
|
|
|
2014-06-17 17:28:40 +02:00
|
|
|
const LINT_ARRAY_COMBINE = 2;
|
|
|
|
const LINT_DEPRECATED_FUNCTION = 3;
|
|
|
|
const LINT_UNSAFE_DYNAMIC_STRING = 4;
|
|
|
|
const LINT_RAGGED_CLASSTREE_EDGE = 5;
|
2013-01-29 07:03:03 +01:00
|
|
|
|
2014-06-17 17:28:40 +02:00
|
|
|
private $deprecatedFunctions = array();
|
2013-02-10 15:35:25 +01:00
|
|
|
private $dynamicStringFunctions = array();
|
2014-06-17 17:28:40 +02:00
|
|
|
private $dynamicStringClasses = array();
|
2013-01-29 07:03:03 +01:00
|
|
|
|
2014-05-12 14:06:04 +02:00
|
|
|
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.');
|
|
|
|
}
|
|
|
|
|
2013-02-10 15:35:25 +01:00
|
|
|
public function setDeprecatedFunctions($map) {
|
|
|
|
$this->deprecatedFunctions = $map;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDynamicStringFunctions($map) {
|
|
|
|
$this->dynamicStringFunctions = $map;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDynamicStringClasses($map) {
|
|
|
|
$this->dynamicStringClasses = $map;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-01-29 07:03:03 +01:00
|
|
|
public function getLintNameMap() {
|
|
|
|
return array(
|
2014-06-17 17:28:40 +02:00
|
|
|
self::LINT_ARRAY_COMBINE => 'array_combine() Unreliable',
|
|
|
|
self::LINT_DEPRECATED_FUNCTION => 'Use of Deprecated Function',
|
|
|
|
self::LINT_UNSAFE_DYNAMIC_STRING => 'Unsafe Usage of Dynamic String',
|
|
|
|
self::LINT_RAGGED_CLASSTREE_EDGE => 'Class Not abstract Or final',
|
2013-01-29 07:03:03 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLintSeverityMap() {
|
2013-02-02 21:33:22 +01:00
|
|
|
$warning = ArcanistLintSeverity::SEVERITY_WARNING;
|
2013-01-29 07:03:03 +01:00
|
|
|
return array(
|
2014-06-17 17:28:40 +02:00
|
|
|
self::LINT_ARRAY_COMBINE => $warning,
|
|
|
|
self::LINT_DEPRECATED_FUNCTION => $warning,
|
|
|
|
self::LINT_UNSAFE_DYNAMIC_STRING => $warning,
|
|
|
|
self::LINT_RAGGED_CLASSTREE_EDGE => $warning,
|
2013-01-29 07:03:03 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLinterName() {
|
|
|
|
return 'PHLXHP';
|
|
|
|
}
|
|
|
|
|
2014-05-12 04:33:40 +02:00
|
|
|
public function getLinterConfigurationName() {
|
|
|
|
return 'phutil-xhpast';
|
|
|
|
}
|
|
|
|
|
2014-05-28 15:33:58 +02:00
|
|
|
public function getVersion() {
|
|
|
|
// The version number should be incremented whenever a new rule is added.
|
2014-06-17 17:28:40 +02:00
|
|
|
return '3';
|
2013-02-02 21:33:22 +01:00
|
|
|
}
|
|
|
|
|
2014-05-12 13:46:41 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-02-19 22:42:09 +01:00
|
|
|
protected function resolveFuture($path, Future $future) {
|
2014-05-12 04:28:46 +02:00
|
|
|
$tree = $this->getXHPASTLinter()->getXHPASTTreeForPath($path);
|
2013-01-29 07:03:03 +01:00
|
|
|
if (!$tree) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$root = $tree->getRootNode();
|
|
|
|
|
2013-02-15 00:49:22 +01:00
|
|
|
$method_codes = array(
|
|
|
|
'lintArrayCombine' => self::LINT_ARRAY_COMBINE,
|
|
|
|
'lintUnsafeDynamicString' => self::LINT_UNSAFE_DYNAMIC_STRING,
|
|
|
|
'lintDeprecatedFunctions' => self::LINT_DEPRECATED_FUNCTION,
|
2014-06-17 17:28:40 +02:00
|
|
|
'lintRaggedClasstreeEdges' => self::LINT_RAGGED_CLASSTREE_EDGE,
|
2013-02-15 00:49:22 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($method_codes as $method => $codes) {
|
|
|
|
foreach ((array)$codes as $code) {
|
|
|
|
if ($this->isCodeEnabled($code)) {
|
|
|
|
call_user_func(array($this, $method), $root);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-29 07:03:03 +01:00
|
|
|
}
|
|
|
|
|
2014-05-10 10:56:06 +02:00
|
|
|
private function lintUnsafeDynamicString(XHPASTNode $root) {
|
2013-02-10 15:35:25 +01:00
|
|
|
$safe = $this->dynamicStringFunctions + array(
|
2013-02-12 03:46:02 +01:00
|
|
|
'pht' => 0,
|
|
|
|
|
2013-02-02 21:33:22 +01:00
|
|
|
'hsprintf' => 0,
|
2013-02-22 01:44:34 +01:00
|
|
|
'jsprintf' => 0,
|
|
|
|
|
|
|
|
'hgsprintf' => 0,
|
2013-02-02 21:33:22 +01:00
|
|
|
|
|
|
|
'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);
|
|
|
|
|
2013-02-10 15:35:25 +01:00
|
|
|
$safe = $this->dynamicStringClasses + array(
|
|
|
|
'ExecFuture' => 0,
|
2013-02-02 21:33:22 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$news = $root->selectDescendantsOfType('n_NEW');
|
|
|
|
$this->lintUnsafeDynamicStringCall($news, $safe);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function lintUnsafeDynamicStringCall(
|
|
|
|
AASTNodeList $calls,
|
|
|
|
array $safe) {
|
|
|
|
|
2013-02-10 15:35:25 +01:00
|
|
|
$safe = array_combine(
|
|
|
|
array_map('strtolower', array_keys($safe)),
|
|
|
|
$safe);
|
|
|
|
|
2013-02-02 21:33:22 +01:00
|
|
|
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,
|
|
|
|
"Parameter ".($param + 1)." of {$name}() should be a scalar string, ".
|
2014-06-17 17:28:40 +02:00
|
|
|
"otherwise it's not safe.");
|
2013-02-02 21:33:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 10:56:06 +02:00
|
|
|
private function lintArrayCombine(XHPASTNode $root) {
|
2013-01-29 07:03:03 +01:00
|
|
|
$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,
|
|
|
|
'Prior to PHP 5.4, array_combine() fails when given empty '.
|
|
|
|
'arrays. Prefer to write array_combine(x, x) as array_fuse(x).');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 10:56:06 +02:00
|
|
|
private function lintDeprecatedFunctions(XHPASTNode $root) {
|
2013-07-28 20:00:00 +02:00
|
|
|
$map = $this->deprecatedFunctions;
|
2013-02-09 02:38:56 +01:00
|
|
|
|
|
|
|
$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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:28:40 +02:00
|
|
|
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,
|
|
|
|
"This class is neither 'final' nor 'abstract', and does not have ".
|
|
|
|
"a docblock marking it '@concrete-extensible'.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 07:03:03 +01:00
|
|
|
}
|