Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F9582682
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
9 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/lint/linter/xhpast/rules/ArcanistClassNameLiteralXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistClassNameLiteralXHPASTLinterRule.php
index a22a9859..288fc05c 100644
--- a/src/lint/linter/xhpast/rules/ArcanistClassNameLiteralXHPASTLinterRule.php
+++ b/src/lint/linter/xhpast/rules/ArcanistClassNameLiteralXHPASTLinterRule.php
@@ -1,49 +1,52 @@
<?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) {
+ if ($class_declaration->getChildByIndex(1)->getTypeName() == 'n_EMPTY') {
+ continue;
+ }
$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);
}
}
}
}
diff --git a/src/lint/linter/xhpast/rules/ArcanistConstructorParenthesesXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistConstructorParenthesesXHPASTLinterRule.php
index d18f0335..4179c899 100644
--- a/src/lint/linter/xhpast/rules/ArcanistConstructorParenthesesXHPASTLinterRule.php
+++ b/src/lint/linter/xhpast/rules/ArcanistConstructorParenthesesXHPASTLinterRule.php
@@ -1,32 +1,34 @@
<?php
final class ArcanistConstructorParenthesesXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 49;
public function getLintName() {
return pht('Constructor Parentheses');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_ADVICE;
}
public function process(XHPASTNode $root) {
$nodes = $root->selectDescendantsOfType('n_NEW');
foreach ($nodes as $node) {
$class = $node->getChildByIndex(0);
$params = $node->getChildByIndex(1);
- if ($params->getTypeName() == 'n_EMPTY') {
+ if ($class->getTypeName() != 'n_CLASS_DECLARATION' &&
+ $params->getTypeName() == 'n_EMPTY') {
+
$this->raiseLintAtNode(
$class,
pht('Use parentheses when invoking a constructor.'),
$class->getConcreteString().'()');
}
}
}
}
diff --git a/src/lint/linter/xhpast/rules/ArcanistSelfClassReferenceXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistSelfClassReferenceXHPASTLinterRule.php
index 7ad7c238..c87974d7 100644
--- a/src/lint/linter/xhpast/rules/ArcanistSelfClassReferenceXHPASTLinterRule.php
+++ b/src/lint/linter/xhpast/rules/ArcanistSelfClassReferenceXHPASTLinterRule.php
@@ -1,45 +1,48 @@
<?php
final class ArcanistSelfClassReferenceXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 95;
public function getLintName() {
return pht('Self Class Reference');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_WARNING;
}
public function process(XHPASTNode $root) {
$class_declarations = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($class_declarations as $class_declaration) {
+ if ($class_declaration->getChildByIndex(1)->getTypeName() == 'n_EMPTY') {
+ continue;
+ }
$class_name = $class_declaration
->getChildOfType(1, 'n_CLASS_NAME')
->getConcreteString();
$instantiations = $class_declaration
->selectDescendantsOfType('n_NEW');
foreach ($instantiations as $instantiation) {
$type = $instantiation->getChildByIndex(0);
if ($type->getTypeName() != 'n_CLASS_NAME') {
continue;
}
if (strtolower($type->getConcreteString()) == strtolower($class_name)) {
$this->raiseLintAtNode(
$type,
pht(
'Use `%s` to instantiate the current class.',
'self'),
'self');
}
}
}
}
}
diff --git a/src/lint/linter/xhpast/rules/ArcanistUnnecessaryFinalModifierXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUnnecessaryFinalModifierXHPASTLinterRule.php
index 1abbaa01..5c197a60 100644
--- a/src/lint/linter/xhpast/rules/ArcanistUnnecessaryFinalModifierXHPASTLinterRule.php
+++ b/src/lint/linter/xhpast/rules/ArcanistUnnecessaryFinalModifierXHPASTLinterRule.php
@@ -1,52 +1,55 @@
<?php
final class ArcanistUnnecessaryFinalModifierXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 55;
public function getLintName() {
return pht('Unnecessary Final Modifier');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_ADVICE;
}
public function process(XHPASTNode $root) {
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
foreach ($classes as $class) {
+ if ($class->getChildByIndex(0)->getTypeName() == 'n_EMPTY') {
+ continue;
+ }
$attributes = $class->getChildOfType(0, 'n_CLASS_ATTRIBUTES');
$is_final = false;
foreach ($attributes->getChildren() as $attribute) {
if ($attribute->getConcreteString() == 'final') {
$is_final = true;
break;
}
}
if (!$is_final) {
continue;
}
$methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION');
foreach ($methods as $method) {
$attributes = $method->getChildOfType(0, 'n_METHOD_MODIFIER_LIST');
foreach ($attributes->getChildren() as $attribute) {
if ($attribute->getConcreteString() == 'final') {
$this->raiseLintAtNode(
$attribute,
pht(
'Unnecessary `%s` modifier in `%s` class.',
'final',
'final'));
}
}
}
}
}
}
diff --git a/src/lint/linter/xhpast/rules/__tests__/class-name-literal/class-name-literal.lint-test b/src/lint/linter/xhpast/rules/__tests__/class-name-literal/class-name-literal.lint-test
index f6050e6f..263fa6bc 100644
--- a/src/lint/linter/xhpast/rules/__tests__/class-name-literal/class-name-literal.lint-test
+++ b/src/lint/linter/xhpast/rules/__tests__/class-name-literal/class-name-literal.lint-test
@@ -1,30 +1,42 @@
<?php
class MyClass {
public function someMethod() {
return 'MyClass';
}
public function someOtherMethod() {
$x = 'MyClass is awesome';
$y = 'MyClassIsAwesome';
return __CLASS__;
}
}
+
+$c = new class {
+ public function someMethod() {
+ return __CLASS__;
+ }
+};
~~~~~~~~~~
advice:5:12
advice:9:10
~~~~~~~~~~
<?php
class MyClass {
public function someMethod() {
return __CLASS__;
}
public function someOtherMethod() {
$x = 'MyClass is awesome';
$y = 'MyClassIsAwesome';
return __CLASS__;
}
}
+
+$c = new class {
+ public function someMethod() {
+ return __CLASS__;
+ }
+};
diff --git a/src/lint/linter/xhpast/rules/__tests__/constructor-parentheses/constructor-parentheses.lint-test b/src/lint/linter/xhpast/rules/__tests__/constructor-parentheses/constructor-parentheses.lint-test
index fa4ede7b..5bfa198d 100644
--- a/src/lint/linter/xhpast/rules/__tests__/constructor-parentheses/constructor-parentheses.lint-test
+++ b/src/lint/linter/xhpast/rules/__tests__/constructor-parentheses/constructor-parentheses.lint-test
@@ -1,14 +1,16 @@
<?php
new Foo;
new Bar();
new Foo\Bar;
+new class {};
~~~~~~~~~~
advice:3:5
advice:5:5
~~~~~~~~~~
<?php
new Foo();
new Bar();
new Foo\Bar();
+new class {};
diff --git a/src/lint/linter/xhpast/rules/__tests__/self-class-reference/self-class-references.lint-test b/src/lint/linter/xhpast/rules/__tests__/self-class-reference/self-class-references.lint-test
index 2f2c1978..33c30f4d 100644
--- a/src/lint/linter/xhpast/rules/__tests__/self-class-reference/self-class-references.lint-test
+++ b/src/lint/linter/xhpast/rules/__tests__/self-class-reference/self-class-references.lint-test
@@ -1,25 +1,37 @@
<?php
class Foo extends Bar {
public static function newInstance() {
return new Foo();
}
public static function init() {
return new self();
}
}
+
+$c = new class {
+ public function newInstance() {
+ return new self();
+ }
+};
~~~~~~~~~~
warning:5:16
~~~~~~~~~~
<?php
class Foo extends Bar {
public static function newInstance() {
return new self();
}
public static function init() {
return new self();
}
}
+
+$c = new class {
+ public function newInstance() {
+ return new self();
+ }
+};
diff --git a/src/lint/linter/xhpast/rules/__tests__/unnecessary-final-modifier/unnecessary-final-modifier.lint-test b/src/lint/linter/xhpast/rules/__tests__/unnecessary-final-modifier/unnecessary-final-modifier.lint-test
index 22185c01..0859d5ab 100644
--- a/src/lint/linter/xhpast/rules/__tests__/unnecessary-final-modifier/unnecessary-final-modifier.lint-test
+++ b/src/lint/linter/xhpast/rules/__tests__/unnecessary-final-modifier/unnecessary-final-modifier.lint-test
@@ -1,8 +1,9 @@
<?php
final class Foo {
public function bar() {}
final public function baz() {}
}
+$c = new class {};
~~~~~~~~~~
advice:5:3
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Oct 11, 07:30 (22 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
983976
Default Alt Text
(9 KB)
Attached To
Mode
R118 Arcanist - fork
Attached
Detach File
Event Timeline
Log In to Comment