Permalink
Browse files

Added Generic SAPIUsageSniff to ensure the PHP_SAPI constant is used …

…instead of php_sapi_name() (request #19863)
  • Loading branch information...
1 parent bc8c394 commit 9ed379422f81fa378fcf2af5f9f8eddf39d07cc5 @gsherwood gsherwood committed Mar 25, 2013
@@ -0,0 +1,81 @@
+<?php
+/**
+ * Generic_Sniffs_PHP_SAPIUsageSniff.
+ *
+ * PHP version 5
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Greg Sherwood <gsherwood@squiz.net>
+ * @copyright 2006-2012 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_PHP_SAPIUsageSniff.
+ *
+ * Ensures the PHP_SAPI constant is used instead of php_sapi_name().
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Greg Sherwood <gsherwood@squiz.net>
+ * @copyright 2006-2012 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_PHP_SAPIUsageSniff implements PHP_CodeSniffer_Sniff
+{
+
+
+ /**
+ * Returns an array of tokens this test wants to listen for.
+ *
+ * @return array
+ */
+ public function register()
+ {
+ return array(T_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();
+
+ $ignore = array(
+ T_DOUBLE_COLON,
+ T_OBJECT_OPERATOR,
+ T_FUNCTION,
+ T_CONST,
+ );
+
+ $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
+ if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
+ // Not a call to a PHP function.
+ return;
+ }
+
+ $function = strtolower($tokens[$stackPtr]['content']);
+ if ($function === 'php_sapi_name') {
+ $error = 'Use the PHP_SAPI constant instead of calling php_sapi_name()';
+ $phpcsFile->addError($error, $stackPtr, 'FunctionFound');
+ }
+
+ }//end process()
+
+
+}//end class
+
+?>
@@ -0,0 +1,4 @@
+<?php
+if (php_sapi_name() !== 'cli') {}
+if (PHP_SAPI !== 'cli') {}
+if ($object->php_sapi_name() === true) {}
@@ -0,0 +1,67 @@
+<?php
+/**
+ * Unit test class for the SAPIUsage sniff.
+ *
+ * PHP version 5
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Greg Sherwood <gsherwood@squiz.net>
+ * @copyright 2006-2012 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
+ */
+
+/**
+ * Unit test class for the SAPIUsage sniff.
+ *
+ * A sniff unit test checks a .inc file for expected violations of a single
+ * coding standard. Expected errors and warnings are stored in this class.
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Greg Sherwood <gsherwood@squiz.net>
+ * @copyright 2006-2012 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_Tests_PHP_SAPIUsageUnitTest extends AbstractSniffUnitTest
+{
+
+
+ /**
+ * Returns the lines where errors should occur.
+ *
+ * The key of the array should represent the line number and the value
+ * should represent the number of errors that should occur on that line.
+ *
+ * @return array(int => int)
+ */
+ public function getErrorList()
+ {
+ return array(
+ 2 => 1,
+ );
+
+ }//end getErrorList()
+
+
+ /**
+ * Returns the lines where warnings should occur.
+ *
+ * The key of the array should represent the line number and the value
+ * should represent the number of warnings that should occur on that line.
+ *
+ * @return array(int => int)
+ */
+ public function getWarningList()
+ {
+ return array();
+
+ }//end getWarningList()
+
+
+}//end class
+
+?>

0 comments on commit 9ed3794

Please sign in to comment.