From a03fe61aef3950cbf3b9351f8cb3fc8479e56a95 Mon Sep 17 00:00:00 2001
From: Klaus Purer <klaus.purer@gmail.com>
Date: Sun, 5 Feb 2017 20:54:27 +0100
Subject: [PATCH] feat(GlobalFunctionSniff): Detect t() calls in service
 classes (#2847690)

---
 .../Sniffs/Objects/GlobalFunctionSniff.php      |  5 ++++-
 .../Test/Objects/GlobalFunctionUnitTest.php     | 17 ++++++++++++++++-
 .../Test/Objects/src/ExampleService.php         |  7 +++++++
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalFunctionSniff.php b/coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalFunctionSniff.php
index 940fc9c5..275d43c8 100644
--- a/coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalFunctionSniff.php
+++ b/coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalFunctionSniff.php
@@ -79,7 +79,10 @@ class DrupalPractice_Sniffs_Objects_GlobalFunctionSniff implements PHP_CodeSniff
         $classPtr    = key($tokens[$stackPtr]['conditions']);
         $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;
         }
 
diff --git a/coder_sniffer/DrupalPractice/Test/Objects/GlobalFunctionUnitTest.php b/coder_sniffer/DrupalPractice/Test/Objects/GlobalFunctionUnitTest.php
index 5ffcb944..a5857e41 100644
--- a/coder_sniffer/DrupalPractice/Test/Objects/GlobalFunctionUnitTest.php
+++ b/coder_sniffer/DrupalPractice/Test/Objects/GlobalFunctionUnitTest.php
@@ -29,9 +29,24 @@ class DrupalPractice_Sniffs_Objects_GlobalFunctionUnitTest extends CoderSniffUni
      */
     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()
 
 
+    /**
+     * 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
diff --git a/coder_sniffer/DrupalPractice/Test/Objects/src/ExampleService.php b/coder_sniffer/DrupalPractice/Test/Objects/src/ExampleService.php
index 8046abd4..8b8acfcf 100644
--- a/coder_sniffer/DrupalPractice/Test/Objects/src/ExampleService.php
+++ b/coder_sniffer/DrupalPractice/Test/Objects/src/ExampleService.php
@@ -23,4 +23,11 @@ class ExampleService {
     return Node::load(1);
   }
 
+  /**
+   * t() should not be used, translation service should be injected.
+   */
+  public function test3() {
+    return t('Test');
+  }
+
 }
-- 
GitLab