Skip to content
Snippets Groups Projects
Commit 6d717e1a authored by Klaus Purer's avatar Klaus Purer
Browse files

Isssue #2720343: Allow global t() and \Drupal calls in static methods

parent 43d37dc0
No related branches found
No related tags found
No related merge requests found
......@@ -70,6 +70,13 @@ class DrupalPractice_Sniffs_Objects_GlobalDrupalSniff implements PHP_CodeSniffer
return;
}
// Check that this statement is not in a static function.
foreach ($tokens[$stackPtr]['conditions'] as $conditionPtr => $conditionCode) {
if ($conditionCode === T_FUNCTION && $phpcsFile->getMethodProperties($conditionPtr)['is_static'] === true) {
return;
}
}
// Check if the class extends another class and get the name of the class
// that is extended.
$classPtr = key($tokens[$stackPtr]['conditions']);
......
......@@ -69,6 +69,13 @@ class DrupalPractice_Sniffs_Objects_GlobalFunctionSniff implements PHP_CodeSniff
return;
}
// Check that this statement is not in a static function.
foreach ($tokens[$stackPtr]['conditions'] as $conditionPtr => $conditionCode) {
if ($conditionCode === T_FUNCTION && $phpcsFile->getMethodProperties($conditionPtr)['is_static'] === true) {
return;
}
}
// Check if the class extends another class and get the name of the class
// that is extended.
$classPtr = key($tokens[$stackPtr]['conditions']);
......
......@@ -9,6 +9,7 @@ class TestForm extends FormBase {
}
public static function something() {
// Global classes are allowed in static methods.
return NodeType::load('article');
}
......
......@@ -8,4 +8,9 @@ class TestForm extends FormBase {
$version = \Drupal::VERSION;
}
public static function example() {
// \Drupal calls are allowed in static methods.
return \Drupal::service('foobar')->result();
}
}
......@@ -7,4 +7,9 @@ class TestForm extends FormBase {
$form['another'] = $this->t('test');
}
public static function example() {
// t() calls are allowed in static methods.
return t('Example text');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment