From 4cef07c6fea95edba83f93237c5ce42996330f80 Mon Sep 17 00:00:00 2001 From: Joao Silva Date: Thu, 7 Jul 2022 17:55:49 +0200 Subject: [PATCH 1/6] DQA-3678: Sniff to check form_alter() usage. --- .../Functions/DrupalForbiddenHooksSniff.php | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php diff --git a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php new file mode 100644 index 0000000..9ba66e6 --- /dev/null +++ b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php @@ -0,0 +1,89 @@ + 'hook_form_FORM_ID_alter() or hook_form_BASE_FORM_ID_alter()', + ]; + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return [T_FUNCTION]; + + }//end register() + + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + public function process(File $phpcsFile, $stackPtr) + { + $fileExtension = strtolower(substr($phpcsFile->getFilename(), -7)); + if (in_array($fileExtension, ['.module', '.theme', '.inc'])) + if ($fileExtension !== '.module') { + return; + } + + $tokens = $phpcsFile->getTokens(); + $functionName = $tokens[($stackPtr + 2)]['content']; + $moduleName = substr(basename($phpcsFile->getFilename()), 0, -7); + + foreach ($this->forbiddenHooks as $hook => $replacement) { + if ($functionName === str_replace('hook', $moduleName, $hook)) { + $warning = 'The usage of the hook %s() is forbidden'; + $data = [$hook]; + if (!empty($replacement)) { + $warning .= ', instead use %s.'; + $data[] = $replacement; + } else { + $warning .= '.'; + } + $phpcsFile->addError($warning, $stackPtr, 'ForbiddenHook', $data); + } + } + + }//end process() + + +}//end class From d34084c37b6fa7162785875295b5a6a2ee3a3a62 Mon Sep 17 00:00:00 2001 From: Joao Silva Date: Thu, 7 Jul 2022 17:57:14 +0200 Subject: [PATCH 2/6] DQA-3678: Fix typo. --- .../Functions/DrupalForbiddenHooksSniff.php | 106 +++++++++--------- 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php index 9ba66e6..b8db3b4 100644 --- a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php +++ b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php @@ -24,66 +24,68 @@ class DrupalForbiddenHooksSniff implements Sniff { - /** - * A list of forbidden functions with their alternatives. - * - * The value is NULL if no alternative exists, i.e., the function should - * just not be used. - * - * @var array|null - */ - public $forbiddenHooks = [ - 'hook_form_alter' => 'hook_form_FORM_ID_alter() or hook_form_BASE_FORM_ID_alter()', - ]; + /** + * A list of forbidden functions with their alternatives. + * + * The value is NULL if no alternative exists, i.e., the function should + * just not be used. + * + * @var array|null + */ + public $forbiddenHooks = [ + 'hook_form_alter' => 'hook_form_FORM_ID_alter() or hook_form_BASE_FORM_ID_alter()', + 'hook_chart_definition_alter' => '', + ]; - /** - * Returns an array of tokens this test wants to listen for. - * - * @return array - */ - public function register() - { - return [T_FUNCTION]; + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return [T_FUNCTION]; - }//end register() + }//end register() - /** - * Processes this test, when one of its tokens is encountered. - * - * @param File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token - * in the stack passed in $tokens. - * - * @return void - */ - public function process(File $phpcsFile, $stackPtr) - { - $fileExtension = strtolower(substr($phpcsFile->getFilename(), -7)); - if (in_array($fileExtension, ['.module', '.theme', '.inc'])) - if ($fileExtension !== '.module') { - return; - } + /** + * Processes this test, when one of its tokens is encountered. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + public function process(File $phpcsFile, $stackPtr) + { + $fileName = basename($phpcsFile->getFilename()); + $fileExtension = explode('.', $fileName); + $fileExtension = end($fileExtension); + if (!in_array($fileExtension, ['module', 'theme', 'inc'])) { + return; + } - $tokens = $phpcsFile->getTokens(); - $functionName = $tokens[($stackPtr + 2)]['content']; - $moduleName = substr(basename($phpcsFile->getFilename()), 0, -7); + $tokens = $phpcsFile->getTokens(); + $functionName = $tokens[($stackPtr + 2)]['content']; + $moduleName = substr($fileName, 0, -(strlen($fileExtension) + 1)); - foreach ($this->forbiddenHooks as $hook => $replacement) { - if ($functionName === str_replace('hook', $moduleName, $hook)) { - $warning = 'The usage of the hook %s() is forbidden'; - $data = [$hook]; - if (!empty($replacement)) { - $warning .= ', instead use %s.'; - $data[] = $replacement; - } else { - $warning .= '.'; + foreach ($this->forbiddenHooks as $hook => $replacement) { + if ($functionName === str_replace('hook', $moduleName, $hook)) { + $warning = 'The usage of the hook %s() is forbidden'; + $data = [$hook]; + if (!empty($replacement)) { + $warning .= ', instead use %s.'; + $data[] = $replacement; + } else { + $warning .= '.'; + } + $phpcsFile->addError($warning, $stackPtr, 'ForbiddenHook', $data); + } } - $phpcsFile->addError($warning, $stackPtr, 'ForbiddenHook', $data); - } - } - }//end process() + }//end process() }//end class From 5954e09935e97dfe44dcd28e61d4af721ab33cf8 Mon Sep 17 00:00:00 2001 From: Joao Silva Date: Thu, 7 Jul 2022 18:32:40 +0200 Subject: [PATCH 3/6] DQA-3678: PHPCS. --- .../Sniffs/Functions/DrupalForbiddenHooksSniff.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php index b8db3b4..78d8dc7 100644 --- a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php +++ b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php @@ -34,9 +34,9 @@ class DrupalForbiddenHooksSniff implements Sniff */ public $forbiddenHooks = [ 'hook_form_alter' => 'hook_form_FORM_ID_alter() or hook_form_BASE_FORM_ID_alter()', - 'hook_chart_definition_alter' => '', ]; + /** * Returns an array of tokens this test wants to listen for. * @@ -63,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr) $fileName = basename($phpcsFile->getFilename()); $fileExtension = explode('.', $fileName); $fileExtension = end($fileExtension); - if (!in_array($fileExtension, ['module', 'theme', 'inc'])) { + if (false === in_array($fileExtension, ['module', 'theme', 'inc'])) { return; } @@ -72,7 +72,7 @@ public function process(File $phpcsFile, $stackPtr) $moduleName = substr($fileName, 0, -(strlen($fileExtension) + 1)); foreach ($this->forbiddenHooks as $hook => $replacement) { - if ($functionName === str_replace('hook', $moduleName, $hook)) { + if (true === ($functionName === str_replace('hook', $moduleName, $hook))) { $warning = 'The usage of the hook %s() is forbidden'; $data = [$hook]; if (!empty($replacement)) { @@ -81,6 +81,7 @@ public function process(File $phpcsFile, $stackPtr) } else { $warning .= '.'; } + $phpcsFile->addError($warning, $stackPtr, 'ForbiddenHook', $data); } } From 63e5fc7e9155d119cefec672a892c1a4a01edea5 Mon Sep 17 00:00:00 2001 From: Joao Silva Date: Thu, 7 Jul 2022 18:58:18 +0200 Subject: [PATCH 4/6] DQA-3678: PHPCS. --- .../Sniffs/Functions/DrupalForbiddenHooksSniff.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php index 78d8dc7..0d32da7 100644 --- a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php +++ b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php @@ -32,9 +32,7 @@ class DrupalForbiddenHooksSniff implements Sniff * * @var array|null */ - public $forbiddenHooks = [ - 'hook_form_alter' => 'hook_form_FORM_ID_alter() or hook_form_BASE_FORM_ID_alter()', - ]; + public $forbiddenHooks = ['hook_form_alter' => 'hook_form_FORM_ID_alter() or hook_form_BASE_FORM_ID_alter()']; /** @@ -75,7 +73,7 @@ public function process(File $phpcsFile, $stackPtr) if (true === ($functionName === str_replace('hook', $moduleName, $hook))) { $warning = 'The usage of the hook %s() is forbidden'; $data = [$hook]; - if (!empty($replacement)) { + if (false === empty($replacement)) { $warning .= ', instead use %s.'; $data[] = $replacement; } else { From b44953f9d46b2713fba5cb560c8f3f855731ef8a Mon Sep 17 00:00:00 2001 From: Joao Silva Date: Fri, 8 Jul 2022 09:09:22 +0200 Subject: [PATCH 5/6] DQA-3678: Improve file detection. --- .../Functions/DrupalForbiddenHooksSniff.php | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php index 0d32da7..b6d4767 100644 --- a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php +++ b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php @@ -34,6 +34,20 @@ class DrupalForbiddenHooksSniff implements Sniff */ public $forbiddenHooks = ['hook_form_alter' => 'hook_form_FORM_ID_alter() or hook_form_BASE_FORM_ID_alter()']; + /** + * A list of forbidden hooks with their alternatives. + * + * The value is empty string if no alternative exists, i.e. the hook should + * just not be used. + * + * @var array + */ + public $extensions = [ + 'inc', + 'module', + 'theme', + ]; + /** * Returns an array of tokens this test wants to listen for. @@ -58,19 +72,18 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $fileName = basename($phpcsFile->getFilename()); - $fileExtension = explode('.', $fileName); - $fileExtension = end($fileExtension); - if (false === in_array($fileExtension, ['module', 'theme', 'inc'])) { + $filename = basename($phpcsFile->getFilename()); + $exploded = explode('.', $filename); + $extension = end($exploded); + if (false === in_array($extension, $this->extensions)) { return; } - $tokens = $phpcsFile->getTokens(); - $functionName = $tokens[($stackPtr + 2)]['content']; - $moduleName = substr($fileName, 0, -(strlen($fileExtension) + 1)); - + $module = $exploded[0]; + $tokens = $phpcsFile->getTokens(); + $function = $tokens[($stackPtr + 2)]['content']; foreach ($this->forbiddenHooks as $hook => $replacement) { - if (true === ($functionName === str_replace('hook', $moduleName, $hook))) { + if (true === ($function === str_replace('hook', $module, $hook))) { $warning = 'The usage of the hook %s() is forbidden'; $data = [$hook]; if (false === empty($replacement)) { From 430e9815561b69c70000642ca16efce6744906a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Costa=20Silva?= <1574795+joaocsilva@users.noreply.github.com> Date: Fri, 8 Jul 2022 12:35:19 +0200 Subject: [PATCH 6/6] DQA-3678: Update comments. --- .../Sniffs/Functions/DrupalForbiddenHooksSniff.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php index b6d4767..a954569 100644 --- a/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php +++ b/phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php @@ -25,9 +25,9 @@ class DrupalForbiddenHooksSniff implements Sniff { /** - * A list of forbidden functions with their alternatives. + * A list of forbidden hooks with their alternatives. * - * The value is NULL if no alternative exists, i.e., the function should + * The value is empty string if no alternative exists, i.e. the hook should * just not be used. * * @var array|null @@ -35,10 +35,7 @@ class DrupalForbiddenHooksSniff implements Sniff public $forbiddenHooks = ['hook_form_alter' => 'hook_form_FORM_ID_alter() or hook_form_BASE_FORM_ID_alter()']; /** - * A list of forbidden hooks with their alternatives. - * - * The value is empty string if no alternative exists, i.e. the hook should - * just not be used. + * A list of file extensions to check. * * @var array */