Skip to content

Instantly share code, notes, and snippets.

@gsherwood
Created January 12, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsherwood/d332be78f499d7c28f7b to your computer and use it in GitHub Desktop.
Save gsherwood/d332be78f499d7c28f7b to your computer and use it in GitHub Desktop.
Example sniff to find a string with specific content
<?php
/**
* Generic_Sniffs_Strings_SearchStringSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Generic_Sniffs_Strings_SearchStringSniff.
*
* Searches for a specific string and does something with it.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Generic_Sniffs_Strings_SearchStringSniff implements PHP_CodeSniffer_Sniff
{
/**
* The string content to search for.
*
* @var string
*/
public $searchString = 'foo';
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_CONSTANT_ENCAPSED_STRING);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$stringContent = trim($tokens[$stackPtr]['content'], '\'" ');
if (strtolower($stringContent) !== $this->searchString) {
return;
}
// The rest of your code here.
$error = "You can't have a string with \"%s\" as the content";
$data = array($stringContent);
$phpcsFile->addError($error, $stackPtr, 'Found', $data);
}//end process()
}//end class
When running that sniff against this code:
<?php
echo "foo";
echo 'Foo';
You get these errors:
FILE: temp.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
3 | ERROR | [ ] You can't have a string with "foo" as the content
4 | ERROR | [ ] You can't have a string with "Foo" as the content
----------------------------------------------------------------------
As this is a public sniff var, you can override it in a ruleset.xml file:
<rule ref="Generic.Strings.SearchString">
<properties>
<property name="searchString" value="bar"/>
</properties>
</rule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment