Commit 7e61e43f authored by epriestley's avatar epriestley

Add version check whitelists for constants to the version compatibility lint rule

Summary: Ref T13249. We currently allow `if (function_exists('X')) { X(); }` but not `if (defined('X')) { X; }`. Allow the latter.

Test Plan: See D20145, which linted clean with this patch in place.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13249

Differential Revision: https://secure.phabricator.com/D20146
parent 25c23819
......@@ -9,4 +9,4 @@ function f() {
~~~~~~~~~~
warning:3:8
error:7:1
error:9:
error:9:1
......@@ -26,6 +26,7 @@ final class ArcanistPHPCompatibilityXHPASTLinterRule
$whitelist = array(
'class' => array(),
'function' => array(),
'constant' => array(),
);
$conditionals = $root->selectDescendantsOfType('n_IF');
......@@ -51,6 +52,7 @@ final class ArcanistPHPCompatibilityXHPASTLinterRule
case 'class_exists':
case 'function_exists':
case 'interface_exists':
case 'defined':
$type = null;
switch ($function_name) {
case 'class_exists':
......@@ -64,6 +66,10 @@ final class ArcanistPHPCompatibilityXHPASTLinterRule
case 'interface_exists':
$type = 'interface';
break;
case 'defined':
$type = 'constant';
break;
}
$params = $function->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
......@@ -98,7 +104,6 @@ final class ArcanistPHPCompatibilityXHPASTLinterRule
$min = idx($version, 'php.min');
$max = idx($version, 'php.max');
// Check if whitelisted.
$whitelisted = false;
foreach (idx($whitelist['function'], $name, array()) as $range) {
if (array_intersect($range, array_keys($node->getTokens()))) {
......@@ -178,18 +183,18 @@ final class ArcanistPHPCompatibilityXHPASTLinterRule
$version = idx($compat_info['classes'], $name, $version);
$min = idx($version, 'php.min');
$max = idx($version, 'php.max');
// Check if whitelisted.
$whitelisted = false;
foreach (idx($whitelist['class'], $name, array()) as $range) {
if (array_intersect($range, array_keys($node->getTokens()))) {
$whitelisted = true;
break;
}
}
if ($whitelisted) {
continue;
$whitelisted = false;
foreach (idx($whitelist['class'], $name, array()) as $range) {
if (array_intersect($range, array_keys($node->getTokens()))) {
$whitelisted = true;
break;
}
}
if ($whitelisted) {
continue;
}
if ($min && version_compare($min, $this->version, '>')) {
$this->raiseLintAtNode(
......@@ -225,6 +230,18 @@ final class ArcanistPHPCompatibilityXHPASTLinterRule
$min = idx($version, 'php.min');
$max = idx($version, 'php.max');
$whitelisted = false;
foreach (idx($whitelist['constant'], $name, array()) as $range) {
if (array_intersect($range, array_keys($node->getTokens()))) {
$whitelisted = true;
break;
}
}
if ($whitelisted) {
continue;
}
if ($min && version_compare($min, $this->version, '>')) {
$this->raiseLintAtNode(
$node,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment