1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2025-03-13 12:54:52 +01:00
phorge-arcanist/src/lint/linter/xhpast/rules/ArcanistClassNameLiteralXHPASTLinterRule.php
Joshua Spence 0b1acf0dc0 Split the ArcanistXHPASTLinter into modular rules
Summary:
The `ArcanistXHPASTLinter` class is becoming quite bloated. This diff separates the class into one-class-per-rule, which makes everything much more modular. One downside to this decoupling is that code reuse between linter rules is much more difficult, although this only affects a very small number of linter rules.

There is still some further work that could be done here, but I defer this until a later diff:

  - Rewrite `ArcanistPhutilXHPASTLinter` using `ArcanistXHPASTLinterRule`.
  - Change the unit tests so that they are truly only testing a single linter rule.
  - Maybe improve the way in which linter configuration options are handled.
  - Make it easier to keep track of the linter rule IDs (see T6859).

Test Plan: `arc unit`

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: johnny-bit, epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D10541
2015-06-01 15:49:16 +10:00

49 lines
1.2 KiB
PHP

<?php
final class ArcanistClassNameLiteralXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 62;
public function getLintName() {
return pht('Class Name Literal');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_ADVICE;
}
public function process(XHPASTNode $root) {
$class_declarations = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($class_declarations as $class_declaration) {
$class_name = $class_declaration
->getChildOfType(1, 'n_CLASS_NAME')
->getConcreteString();
$strings = $class_declaration->selectDescendantsOfType('n_STRING_SCALAR');
foreach ($strings as $string) {
$contents = substr($string->getSemanticString(), 1, -1);
$replacement = null;
if ($contents == $class_name) {
$replacement = '__CLASS__';
}
$regex = '/\b'.preg_quote($class_name, '/').'\b/';
if (!preg_match($regex, $contents)) {
continue;
}
$this->raiseLintAtNode(
$string,
pht(
"Don't hard-code class names, use %s instead.",
'__CLASS__'),
$replacement);
}
}
}
}