-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from ec-europa/feature/DQA-3678
DQA-3678: [8.x] Sniff to check form_alter() usage.
- Loading branch information
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
phpcs/QualityAssurance/Sniffs/Functions/DrupalForbiddenHooksSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
/** | ||
* \QualityAssurance\Sniffs\Functions\DrupalFormAlterSniff. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
namespace QualityAssurance\Sniffs\Functions; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
/** | ||
* \QualityAssurance\Sniffs\Functions\DrupalFormAlterSniff. | ||
* | ||
* Checks the presence of form_alter function on .module files. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class DrupalForbiddenHooksSniff implements Sniff | ||
{ | ||
|
||
/** | ||
* 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|null | ||
*/ | ||
public $forbiddenHooks = ['hook_form_alter' => 'hook_form_FORM_ID_alter() or hook_form_BASE_FORM_ID_alter()']; | ||
|
||
/** | ||
* A list of file extensions to check. | ||
* | ||
* @var array | ||
*/ | ||
public $extensions = [ | ||
'inc', | ||
'module', | ||
'theme', | ||
]; | ||
|
||
|
||
/** | ||
* 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) | ||
{ | ||
$filename = basename($phpcsFile->getFilename()); | ||
$exploded = explode('.', $filename); | ||
$extension = end($exploded); | ||
if (false === in_array($extension, $this->extensions)) { | ||
return; | ||
} | ||
|
||
$module = $exploded[0]; | ||
$tokens = $phpcsFile->getTokens(); | ||
$function = $tokens[($stackPtr + 2)]['content']; | ||
foreach ($this->forbiddenHooks as $hook => $replacement) { | ||
if (true === ($function === str_replace('hook', $module, $hook))) { | ||
$warning = 'The usage of the hook %s() is forbidden'; | ||
$data = [$hook]; | ||
if (false === empty($replacement)) { | ||
$warning .= ', instead use %s.'; | ||
$data[] = $replacement; | ||
} else { | ||
$warning .= '.'; | ||
} | ||
|
||
$phpcsFile->addError($warning, $stackPtr, 'ForbiddenHook', $data); | ||
} | ||
} | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |