1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-22 14:52:40 +01:00

Add a linter rule for parentheses being used with PHP language constructs

Summary: Fixes T5648.

Test Plan: Added some test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Maniphest Tasks: T5648

Differential Revision: https://secure.phabricator.com/D10122
This commit is contained in:
Joshua Spence 2014-08-05 22:35:34 +10:00
parent 26b71baf09
commit f6e26a7133
3 changed files with 84 additions and 2 deletions

View file

@ -45,6 +45,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
const LINT_SEMICOLON_SPACING = 43;
const LINT_CONCATENATION_OPERATOR = 44;
const LINT_PHP_COMPATIBILITY = 45;
const LINT_LANGUAGE_CONSTRUCT_PAREN = 46;
private $naminghook;
private $switchhook;
@ -101,6 +102,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
self::LINT_SEMICOLON_SPACING => 'Semicolon Spacing',
self::LINT_CONCATENATION_OPERATOR => 'Concatenation Spacing',
self::LINT_PHP_COMPATIBILITY => 'PHP Compatibility',
self::LINT_LANGUAGE_CONSTRUCT_PAREN => 'Language Construct Parentheses',
);
}
@ -138,6 +140,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
self::LINT_ELSEIF_USAGE => $advice,
self::LINT_SEMICOLON_SPACING => $advice,
self::LINT_CONCATENATION_OPERATOR => $warning,
self::LINT_LANGUAGE_CONSTRUCT_PAREN => $warning,
);
}
@ -187,7 +190,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
return '7';
return '8';
}
protected function resolveFuture($path, Future $future) {
@ -255,6 +258,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
'lintSpaceAroundConcatenationOperators' =>
self::LINT_CONCATENATION_OPERATOR,
'lintPHPCompatibility' => self::LINT_PHP_COMPATIBILITY,
'lintLanguageConstructParentheses' => self::LINT_LANGUAGE_CONSTRUCT_PAREN,
);
foreach ($method_codes as $method => $codes) {
@ -2521,6 +2525,36 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
}
}
protected function lintLanguageConstructParentheses(XHPASTNode $root) {
$nodes = $root->selectDescendantsOfTypes(array(
'n_INCLUDE_FILE',
'n_ECHO_LIST',
));
foreach ($nodes as $node) {
$child = head($node->getChildren());
if ($child->getTypeName() === 'n_PARENTHETICAL_EXPRESSION') {
list($before, $after) = $child->getSurroundingNonsemanticTokens();
$replace = preg_replace(
'/^\((.*)\)$/',
'$1',
$child->getConcreteString());
if (!$before) {
$replace = ' '.$replace;
}
$this->raiseLintAtNode(
$child,
self::LINT_LANGUAGE_CONSTRUCT_PAREN,
pht('Language constructs do not require parentheses.'),
$replace);
}
}
}
public function getSuperGlobalNames() {
return array(
'$GLOBALS',

View file

@ -0,0 +1,48 @@
<?php
include 'foo.php';
include_once 'foo.php';
require 'foo.php';
require_once 'foo.php';
echo 'foo';
include('bar.php');
include_once('bar.php');
require('bar.php');
require_once('bar.php');
echo('bar');
include ('baz.php');
include_once ('baz.php');
require ('baz.php');
require_once ('baz.php');
echo ('baz');
~~~~~~~~~~
warning:8:8
warning:9:13
warning:10:8
warning:11:13
warning:12:5
warning:14:9
warning:15:14
warning:16:9
warning:17:14
warning:18:6
~~~~~~~~~~
<?php
include 'foo.php';
include_once 'foo.php';
require 'foo.php';
require_once 'foo.php';
echo 'foo';
include 'bar.php';
include_once 'bar.php';
require 'bar.php';
require_once 'bar.php';
echo 'bar';
include 'baz.php';
include_once 'baz.php';
require 'baz.php';
require_once 'baz.php';
echo 'baz';

View file

@ -141,7 +141,7 @@ abstract class P {
function x() {
$lib = $_SERVER['PHP_ROOT'].'/lib/titan/display/read/init.php';
require_once($lib);
require_once $lib;
f(((($lib)))); // Tests for paren expressions.
f(((($lub))));
}