forked from phacility/arcanist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a "product name literal in pht()" linter
Summary: Ref T13658. One challenge in forking Phabricator is product name references in user-visible strings, particularly in "pht()". Add a linter to identify the use of product name literals in "pht()" text. The linter could fix these automatically, but it looks like there are fewer than 200 across both Arcanist and Phabricator and some sampling suggests that many are probably clearer when rewritten into generic language anyway. Test Plan: Ran linter, saw it pop out reasonable warnings. Maniphest Tasks: T13658 Differential Revision: https://secure.phabricator.com/D21763
- Loading branch information
epriestley
committed
Apr 25, 2022
1 parent
f098e8d
commit a33aeb3
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/lint/linter/xhpast/rules/ArcanistProductNameLiteralXHPASTLinterRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
final class ArcanistProductNameLiteralXHPASTLinterRule | ||
extends ArcanistXHPASTLinterRule { | ||
|
||
const ID = 134; | ||
|
||
public function getLintName() { | ||
return pht('Use of Product Name Literal'); | ||
} | ||
|
||
public function getLintSeverity() { | ||
return ArcanistLintSeverity::SEVERITY_WARNING; | ||
} | ||
|
||
public function process(XHPASTNode $root) { | ||
$calls = $root->selectDescendantsOfType('n_FUNCTION_CALL'); | ||
|
||
$product_names = PlatformSymbols::getProductNames(); | ||
foreach ($product_names as $k => $product_name) { | ||
$product_names[$k] = preg_quote($product_name); | ||
} | ||
|
||
$search_pattern = '(\b(?:'.implode('|', $product_names).')\b)i'; | ||
|
||
foreach ($calls as $call) { | ||
$name = $call->getChildByIndex(0)->getConcreteString(); | ||
|
||
if ($name !== 'pht') { | ||
continue; | ||
} | ||
|
||
$parameters = $call->getChildByIndex(1); | ||
|
||
if (!$parameters->getChildren()) { | ||
continue; | ||
} | ||
|
||
$identifier = $parameters->getChildByIndex(0); | ||
if (!$identifier->isConstantString()) { | ||
continue; | ||
} | ||
|
||
$literal_value = $identifier->getStringLiteralValue(); | ||
|
||
$matches = phutil_preg_match_all($search_pattern, $literal_value); | ||
if (!$matches[0]) { | ||
continue; | ||
} | ||
|
||
$name_list = array(); | ||
foreach ($matches[0] as $match) { | ||
$name_list[phutil_utf8_strtolower($match)] = $match; | ||
} | ||
$name_list = implode(', ', $name_list); | ||
|
||
$this->raiseLintAtNode( | ||
$identifier, | ||
pht( | ||
'Avoid use of product name literals in "pht()": use generic '. | ||
'language or an appropriate method from the "PlatformSymbols" class '. | ||
'instead so the software can be forked. String uses names: %s.', | ||
$name_list)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
final class PlatformSymbols | ||
extends Phobject { | ||
|
||
public static function getPlatformClientName() { | ||
return 'Arcanist'; | ||
} | ||
|
||
public static function getPlatformServerName() { | ||
return 'Phabricator'; | ||
} | ||
|
||
public static function getProductNames() { | ||
return array( | ||
self::getPlatformClientName(), | ||
self::getPlatformServerName(), | ||
); | ||
} | ||
|
||
} |