1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-25 16:22:42 +01:00

Add a linter rule for spacing after a cast

Summary: Ref T7409. Add a linter rule to ensure that a cast is not followed by a space. This is largely based on [[https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Generic/Sniffs/Formatting/NoSpaceAfterCastSniff.php | Generic_Sniffs_Formatting_NoSpaceAfterCastSniff]].

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin, epriestley

Maniphest Tasks: T7409

Differential Revision: https://secure.phabricator.com/D12321
This commit is contained in:
Joshua Spence 2015-05-18 08:06:05 +10:00
parent e1a057b4d9
commit 2e66d03c68
2 changed files with 37 additions and 1 deletions

View file

@ -65,6 +65,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
const LINT_USELESS_OVERRIDING_METHOD = 63;
const LINT_NO_PARENT_SCOPE = 64;
const LINT_ALIAS_FUNCTION = 65;
const LINT_CAST_SPACING = 66;
private $blacklistedFunctions = array();
private $naminghook;
@ -203,6 +204,8 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
=> pht('No Parent Scope'),
self::LINT_ALIAS_FUNCTION
=> pht('Alias Functions'),
self::LINT_CAST_SPACING
=> pht('Cast Spacing'),
);
}
@ -255,6 +258,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
self::LINT_CLASS_NAME_LITERAL => $advice,
self::LINT_USELESS_OVERRIDING_METHOD => $advice,
self::LINT_ALIAS_FUNCTION => $advice,
self::LINT_CAST_SPACING => $advice,
);
}
@ -322,7 +326,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
return '27';
return '28';
}
protected function resolveFuture($path, Future $future) {
@ -414,6 +418,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
'lintUselessOverridingMethods' => self::LINT_USELESS_OVERRIDING_METHOD,
'lintNoParentScope' => self::LINT_NO_PARENT_SCOPE,
'lintAliasFunctions' => self::LINT_ALIAS_FUNCTION,
'lintCastSpacing' => self::LINT_CAST_SPACING,
);
foreach ($method_codes as $method => $codes) {
@ -4100,6 +4105,25 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
}
}
private function lintCastSpacing(XHPASTNode $root) {
$cast_expressions = $root->selectDescendantsOfType('n_CAST_EXPRESSION');
foreach ($cast_expressions as $cast_expression) {
$cast = $cast_expression->getChildOfType(0, 'n_CAST');
list($before, $after) = $cast->getSurroundingNonsemanticTokens();
$after = head($after);
if ($after) {
$this->raiseLintAtToken(
$after,
self::LINT_CAST_SPACING,
pht('A cast statement must not be followed by a space.'),
'');
}
}
}
/**
* Retrieve all calls to some specified function(s).

View file

@ -0,0 +1,12 @@
<?php
echo (double)0;
echo (int) 1;
echo (string) 2;
~~~~~~~~~~
advice:3:11
advice:4:14
~~~~~~~~~~
<?php
echo (double)0;
echo (int)1;
echo (string)2;