Summary: `instanceof` should be used instead of `is_a`. I need to do a bit more research here to see if there are any edge cases.
Test Plan: Added unit tests.
Reviewers: epriestley, #blessed_reviewers
Reviewed By: epriestley, #blessed_reviewers
Subscribers: Korvin
Differential Revision: https://secure.phabricator.com/D14573
'ArcanistInvalidModifiersXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistInvalidModifiersXHPASTLinterRuleTestCase.php',
'ArcanistInvalidOctalNumericScalarXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistInvalidOctalNumericScalarXHPASTLinterRule.php',
'ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase.php',
+ 'ArcanistIsAShouldBeInstanceOfXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistIsAShouldBeInstanceOfXHPASTLinterRule.php',
+ 'ArcanistIsAShouldBeInstanceOfXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistIsAShouldBeInstanceOfXHPASTLinterRuleTestCase.php',
'ArcanistJSHintLinter' => 'lint/linter/ArcanistJSHintLinter.php',
'ArcanistJSHintLinterTestCase' => 'lint/linter/__tests__/ArcanistJSHintLinterTestCase.php',
'ArcanistJSONLintLinter' => 'lint/linter/ArcanistJSONLintLinter.php',
'ArcanistInvalidModifiersXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistInvalidOctalNumericScalarXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistInvalidOctalNumericScalarXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
+ 'ArcanistIsAShouldBeInstanceOfXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistIsAShouldBeInstanceOfXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistJSHintLinter' => 'ArcanistExternalLinter',
'ArcanistJSHintLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistJSONLintLinter' => 'ArcanistExternalLinter',
--- /dev/null
+<?php
+
+final class ArcanistIsAShouldBeInstanceOfXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 111;
+
+ public function getLintName() {
+ return pht('`%s` Should Be `%s`', 'is_a', 'instanceof');
+ }
+
+ public function getLintSeverity() {
+ return ArcanistLintSeverity::SEVERITY_ADVICE;
+ }
+
+ public function process(XHPASTNode $root) {
+ $calls = $this->getFunctionCalls($root, array('is_a'));
+
+ foreach ($calls as $call) {
+ $parameters = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
+
+ if (count($parameters->getChildren()) > 2) {
+ // If the `$allow_string` parameter is `true` then the `instanceof`
+ // operator cannot be used. Evaluating whether an expression is truthy
+ // or falsely is hard, and so we only check that the `$allow_string`
+ // parameter is either absent or literally `false`.
+ $allow_string = $parameters->getChildByIndex(2);
+
+ if (strtolower($allow_string->getConcreteString()) != 'false') {
+ continue;
+ }
+ }
+
+ $object = $parameters->getChildByIndex(0);
+ $class = $parameters->getChildByIndex(1);
+
+ switch ($class->getTypeName()) {
+ case 'n_STRING_SCALAR':
+ $replacement = stripslashes(
+ substr($class->getConcreteString(), 1, -1));
+ break;
+
+ case 'n_VARIABLE':
+ $replacement = $class->getConcreteString();
+ break;
+
+ default:
+ $replacement = null;
+ break;
+ }
+
+ $this->raiseLintAtNode(
+ $call,
+ pht(
+ 'Use `%s` instead of `%s`. The former is a language '.
+ 'construct whereas the latter is a function call, which '.
+ 'has additional overhead.',
+ 'instanceof',
+ 'is_a'),
+ ($replacement === null)
+ ? null
+ : sprintf(
+ '%s instanceof %s',
+ $object->getConcreteString(),
+ $replacement));
+ }
+ }
+
+}
--- /dev/null
+<?php
+
+final class ArcanistIsAShouldBeInstanceOfXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(
+ dirname(__FILE__).'/is_a-should-be-instanceof/');
+ }
+
+}
--- /dev/null
+<?php
+is_a('SubClass', 'BaseClass', true);
+~~~~~~~~~~
--- /dev/null
+<?php
+is_a($x, $y);
+is_a($x, 'SomeClass');
+is_a($x, '\\SomeClass');
+is_a($x, '\\'.'Some'.'Class');
+~~~~~~~~~~
+advice:2:1
+advice:3:1
+advice:4:1
+advice:5:1
+~~~~~~~~~~
+<?php
+$x instanceof $y;
+$x instanceof SomeClass;
+$x instanceof \SomeClass;
+is_a($x, '\\'.'Some'.'Class');