From 06bbf958dd97fa28b761c13e40e128cd8ace4f56 Mon Sep 17 00:00:00 2001 From: Bryan Sharpe Date: Fri, 25 Oct 2024 11:33:07 -0700 Subject: [PATCH] Adds better detection of DI classes to global sniff. --- .../DrupalPractice/Sniffs/Objects/GlobalDrupalSniff.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalDrupalSniff.php b/coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalDrupalSniff.php index 4148ba8e..4d0ac1f3 100644 --- a/coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalDrupalSniff.php +++ b/coder_sniffer/DrupalPractice/Sniffs/Objects/GlobalDrupalSniff.php @@ -95,8 +95,13 @@ public function process(File $phpcsFile, $stackPtr) $extendsName = $phpcsFile->findExtendedClassName($classPtr); // Check if the class implements ContainerInjectionInterface. + $containerInterfaces = [ + 'ContainerInjectionInterface', + 'ContainerFactoryPluginInterface', + 'ContainerDeriverInterface', + ]; $implementedInterfaceNames = $phpcsFile->findImplementedInterfaceNames($classPtr); - $canAccessContainer = !empty($implementedInterfaceNames) && in_array('ContainerInjectionInterface', $implementedInterfaceNames); + $canAccessContainer = !empty($implementedInterfaceNames) && !empty(array_intersect($containerInterfaces, $implementedInterfaceNames)); if (($extendsName === false || in_array($extendsName, static::$baseClasses) === false) && Project::isServiceClass($phpcsFile, $classPtr) === false