Differential D21032 Diff 50113 src/lint/linter/xhpast/rules/ArcanistClassNameLiteralXHPASTLinterRule.php
Changeset View
Changeset View
Standalone View
Standalone View
src/lint/linter/xhpast/rules/ArcanistClassNameLiteralXHPASTLinterRule.php
| Show All 28 Lines | foreach ($class_declarations as $class_declaration) { | ||||
| foreach ($strings as $string) { | foreach ($strings as $string) { | ||||
| $contents = substr($string->getSemanticString(), 1, -1); | $contents = substr($string->getSemanticString(), 1, -1); | ||||
| $replacement = null; | $replacement = null; | ||||
| if ($contents == $class_name) { | if ($contents == $class_name) { | ||||
| $replacement = '__CLASS__'; | $replacement = '__CLASS__'; | ||||
| } | } | ||||
| $regex = '/\b'.preg_quote($class_name, '/').'\b/'; | // NOTE: We're only warning when the entire string content is the | ||||
| // class name. It's okay to hard-code the class name as part of a | |||||
| // longer string, like an error or exception message. | |||||
| // Sometimes the class name (like "Filesystem") is also a valid part | |||||
| // of the message, which makes this warning a false positive. | |||||
| // Even when we're generating a true positive by detecting a class | |||||
| // name in part of a longer string, the cost of an error message | |||||
| // being out-of-date is generally very small (mild confusion, but | |||||
| // no incorrect beahavior) and using "__CLASS__" in errors is often | |||||
| // clunky. | |||||
| $regex = '(^'.preg_quote($class_name).'$)'; | |||||
| if (!preg_match($regex, $contents)) { | if (!preg_match($regex, $contents)) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| $this->raiseLintAtNode( | $this->raiseLintAtNode( | ||||
| $string, | $string, | ||||
| pht( | pht( | ||||
| "Don't hard-code class names, use `%s` instead.", | 'Prefer "__CLASS__" over hard-coded class names.'), | ||||
| '__CLASS__'), | |||||
| $replacement); | $replacement); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||