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

feat(GlobalFunctionSniff): Detect t() calls in service classes (#2847690)

parent 2ddb5081
Branches
Tags
No related merge requests found
...@@ -79,7 +79,10 @@ class DrupalPractice_Sniffs_Objects_GlobalFunctionSniff implements PHP_CodeSniff ...@@ -79,7 +79,10 @@ class DrupalPractice_Sniffs_Objects_GlobalFunctionSniff implements PHP_CodeSniff
$classPtr = key($tokens[$stackPtr]['conditions']); $classPtr = key($tokens[$stackPtr]['conditions']);
$extendsName = $phpcsFile->findExtendedClassName($classPtr); $extendsName = $phpcsFile->findExtendedClassName($classPtr);
if ($extendsName === false || in_array($extendsName, DrupalPractice_Sniffs_Objects_GlobalDrupalSniff::$baseClasses) === false) { if (($extendsName === false
|| in_array($extendsName, DrupalPractice_Sniffs_Objects_GlobalDrupalSniff::$baseClasses) === false)
&& DrupalPractice_Project::isServiceClass($phpcsFile, $classPtr) === false
) {
return; return;
} }
......
...@@ -29,9 +29,24 @@ class DrupalPractice_Sniffs_Objects_GlobalFunctionUnitTest extends CoderSniffUni ...@@ -29,9 +29,24 @@ class DrupalPractice_Sniffs_Objects_GlobalFunctionUnitTest extends CoderSniffUni
*/ */
protected function getWarningList($testFile) protected function getWarningList($testFile)
{ {
return array(6 => 1); switch ($testFile) {
case 'GlobalFunctionUnitTest.inc':
return array(6 => 1);
case 'ExampleService.php':
return array(30 => 1);
}
}//end getWarningList() }//end getWarningList()
/**
* Returns a list of test files that should be checked.
*
* @return array The list of test files.
*/
protected function getTestFiles() {
return [__DIR__.'/GlobalFunctionUnitTest.inc', __DIR__.'/src/ExampleService.php'];
}
}//end class }//end class
...@@ -23,4 +23,11 @@ class ExampleService { ...@@ -23,4 +23,11 @@ class ExampleService {
return Node::load(1); return Node::load(1);
} }
/**
* t() should not be used, translation service should be injected.
*/
public function test3() {
return t('Test');
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment