mirror of
https://we.phorge.it/source/arcanist.git
synced 2025-02-20 02:38:38 +01:00
Raise a lint warning on array_combine(x, x)
Summary: Suggest array_fuse(). Test Plan: Unit tests, and: $ arc lint --lintall src/unit/engine/phutil/ArcanistPhutilTestCase.php >>> Lint for src/unit/engine/phutil/ArcanistPhutilTestCase.php: Warning (XHP37) array_combine() Unreliable Prior to PHP 5.4, array_combine() fails when given empty arrays. Prefer to write array_combine(x, x) as array_fuse(x). 252 $callable, 253 $exception_class = 'Exception') { 254 return $this->tryTestCases( >>> 255 array_combine(array_keys($map), array_keys($map)), 256 array_values($map), 257 $callable, 258 $exception_class); Reviewers: btrahan, vrana, chad Reviewed By: btrahan CC: aran Differential Revision: https://secure.phabricator.com/D4659
This commit is contained in:
parent
9946e23f9e
commit
44e25495ca
4 changed files with 41 additions and 1 deletions
|
@ -65,6 +65,7 @@ class PhutilLintEngine extends ArcanistLintEngine {
|
||||||
ArcanistXHPASTLinter::LINT_COMMENT_SPACING => $error,
|
ArcanistXHPASTLinter::LINT_COMMENT_SPACING => $error,
|
||||||
ArcanistXHPASTLinter::LINT_RAGGED_CLASSTREE_EDGE => $warning,
|
ArcanistXHPASTLinter::LINT_RAGGED_CLASSTREE_EDGE => $warning,
|
||||||
ArcanistXHPASTLinter::LINT_TODO_COMMENT => $advice,
|
ArcanistXHPASTLinter::LINT_TODO_COMMENT => $advice,
|
||||||
|
ArcanistXHPASTLinter::LINT_ARRAY_COMBINE => $warning,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
|
||||||
const LINT_COMMENT_SPACING = 34;
|
const LINT_COMMENT_SPACING = 34;
|
||||||
const LINT_PHP_54_FEATURES = 35;
|
const LINT_PHP_54_FEATURES = 35;
|
||||||
const LINT_SLOWNESS = 36;
|
const LINT_SLOWNESS = 36;
|
||||||
|
const LINT_ARRAY_COMBINE = 37;
|
||||||
|
|
||||||
|
|
||||||
public function getLintNameMap() {
|
public function getLintNameMap() {
|
||||||
|
@ -83,6 +84,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
|
||||||
self::LINT_PHT_WITH_DYNAMIC_STRING => 'Use of pht() on Dynamic String',
|
self::LINT_PHT_WITH_DYNAMIC_STRING => 'Use of pht() on Dynamic String',
|
||||||
self::LINT_COMMENT_SPACING => 'Comment Spaces',
|
self::LINT_COMMENT_SPACING => 'Comment Spaces',
|
||||||
self::LINT_SLOWNESS => 'Slow Construct',
|
self::LINT_SLOWNESS => 'Slow Construct',
|
||||||
|
self::LINT_ARRAY_COMBINE => 'array_combine() Unreliable',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,6 +120,10 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
|
||||||
// a specific minimum version.
|
// a specific minimum version.
|
||||||
self::LINT_PHP_53_FEATURES => $disabled,
|
self::LINT_PHP_53_FEATURES => $disabled,
|
||||||
self::LINT_PHP_54_FEATURES => $disabled,
|
self::LINT_PHP_54_FEATURES => $disabled,
|
||||||
|
|
||||||
|
// This message specifically recommends array_fuse(), a libphutil
|
||||||
|
// function.
|
||||||
|
self::LINT_ARRAY_COMBINE => $disabled,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,6 +209,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
|
||||||
$this->lintPHT($root);
|
$this->lintPHT($root);
|
||||||
$this->lintStrposUsedForStart($root);
|
$this->lintStrposUsedForStart($root);
|
||||||
$this->lintStrstrUsedForCheck($root);
|
$this->lintStrstrUsedForCheck($root);
|
||||||
|
$this->lintArrayCombine($root);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function lintStrstrUsedForCheck($root) {
|
public function lintStrstrUsedForCheck($root) {
|
||||||
|
@ -1799,6 +1806,32 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function lintArrayCombine($root) {
|
||||||
|
$function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
|
||||||
|
foreach ($function_calls as $call) {
|
||||||
|
$name = $call->getChildByIndex(0)->getConcreteString();
|
||||||
|
if (strtolower($name) === 'array_combine') {
|
||||||
|
$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).');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exit is parsed as an expression, but using it as such is almost always
|
* Exit is parsed as an expression, but using it as such is almost always
|
||||||
* wrong. That is, this is valid:
|
* wrong. That is, this is valid:
|
||||||
|
|
6
src/lint/linter/__tests__/xhpast/array-combine.lint-test
Normal file
6
src/lint/linter/__tests__/xhpast/array-combine.lint-test
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
array_combine($x, $x);
|
||||||
|
array_combine($x, $y);
|
||||||
|
~~~~~~~~~~
|
||||||
|
disabled:3:1
|
|
@ -252,7 +252,7 @@ abstract class ArcanistPhutilTestCase {
|
||||||
$callable,
|
$callable,
|
||||||
$exception_class = 'Exception') {
|
$exception_class = 'Exception') {
|
||||||
return $this->tryTestCases(
|
return $this->tryTestCases(
|
||||||
array_combine(array_keys($map), array_keys($map)),
|
array_fuse(array_keys($map)),
|
||||||
array_values($map),
|
array_values($map),
|
||||||
$callable,
|
$callable,
|
||||||
$exception_class);
|
$exception_class);
|
||||||
|
|
Loading…
Add table
Reference in a new issue