/**
  * Process this function definition.
  *
  * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
  * @param int                  $stackPtr    The position of the function name in the stack.
  *                                           name in the stack.
  * @param int                  $functionPtr The position of the function keyword in the stack.
  *                                           keyword in the stack.
  *
  * @return void
  */
 public function processFunction(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionPtr)
 {
     $fileExtension = strtolower(substr($phpcsFile->getFilename(), -7));
     // Only check in *.install files.
     if ($fileExtension !== 'install') {
         return;
     }
     $fileName = substr(basename($phpcsFile->getFilename()), 0, -8);
     $tokens = $phpcsFile->getTokens();
     if ($tokens[$stackPtr]['content'] !== $fileName . '_install' && $tokens[$stackPtr]['content'] !== $fileName . '_requirements') {
         return;
     }
     // This check only applies to Drupal 7, not Drupal 8.
     if (DrupalPractice_Project::getCoreVersion($phpcsFile) !== '7.x') {
         return;
     }
     // Search in the function body for t() calls.
     $string = $phpcsFile->findNext(T_STRING, $tokens[$functionPtr]['scope_opener'], $tokens[$functionPtr]['scope_closer']);
     while ($string !== false) {
         if ($tokens[$string]['content'] === 't' || $tokens[$string]['content'] === 'st') {
             $opener = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $string + 1, null, true);
             if ($opener !== false && $tokens[$opener]['code'] === T_OPEN_PARENTHESIS) {
                 $error = 'Do not use t() or st() in installation phase hooks, use $t = get_t() to retrieve the appropriate localization function name';
                 $phpcsFile->addError($error, $string, 'TranslationFound');
             }
         }
         $string = $phpcsFile->findNext(T_STRING, $string + 1, $tokens[$functionPtr]['scope_closer']);
     }
     //end while
 }
 /**
  * Process this function definition.
  *
  * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
  * @param int                  $stackPtr    The position of the function name in the stack.
  *                                           name in the stack.
  * @param int                  $functionPtr The position of the function keyword in the stack.
  *                                           keyword in the stack.
  *
  * @return void
  */
 public function processFunction(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionPtr)
 {
     $fileExtension = strtolower(substr($phpcsFile->getFilename(), -6));
     // Only check in *.module files.
     if ($fileExtension !== 'module') {
         return;
     }
     // This check only applies to Drupal 7, not Drupal 6.
     if (DrupalPractice_Project::getCoreVersion($phpcsFile) !== '7.x') {
         return;
     }
     $fileName = substr(basename($phpcsFile->getFilename()), 0, -7);
     $tokens = $phpcsFile->getTokens();
     if ($tokens[$stackPtr]['content'] !== $fileName . '_init' && $tokens[$stackPtr]['content'] !== $fileName . '_page_build') {
         return;
     }
     // Search in the function body for drupal_add_css() calls.
     $string = $phpcsFile->findNext(T_STRING, $tokens[$functionPtr]['scope_opener'], $tokens[$functionPtr]['scope_closer']);
     while ($string !== false) {
         if ($tokens[$string]['content'] === 'drupal_add_css' || $tokens[$string]['content'] === 'drupal_add_js') {
             $opener = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $string + 1, null, true);
             if ($opener !== false && $tokens[$opener]['code'] === T_OPEN_PARENTHESIS) {
                 if ($tokens[$stackPtr]['content'] === $fileName . '_init') {
                     $warning = 'Do not use %s() in hook_init(), move it to your page/form callback or use hook_page_build() instead';
                     $phpcsFile->addWarning($warning, $string, 'AddFunctionFound', array($tokens[$string]['content']));
                 } else {
                     $warning = 'Do not use %s() in hook_page_build(), use #attached on the $page render array instead';
                     $phpcsFile->addWarning($warning, $string, 'AddFunctionFoundPageBuild', array($tokens[$string]['content']));
                 }
             }
         }
         $string = $phpcsFile->findNext(T_STRING, $string + 1, $tokens[$functionPtr]['scope_closer']);
     }
     //end while
 }
Example #3
0
 /**
  * Processes this function call.
  *
  * @param PHP_CodeSniffer_File $phpcsFile
  *   The file being scanned.
  * @param int                  $stackPtr
  *   The position of the function call in the stack.
  * @param int                  $openBracket
  *   The position of the opening parenthesis in the stack.
  * @param int                  $closeBracket
  *   The position of the closing parenthesis in the stack.
  *
  * @return void
  */
 public function processFunctionCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $closeBracket)
 {
     // This check only applies to Drupal 7, not Drupal 6.
     if (DrupalPractice_Project::getCoreVersion($phpcsFile) !== '7.x') {
         return;
     }
     $tokens = $phpcsFile->getTokens();
     $argument = $this->getArgument(1);
     $query_start = '';
     for ($start = $argument['start']; $tokens[$start]['code'] === T_CONSTANT_ENCAPSED_STRING && empty($query_start) === true; $start++) {
         // Remove quote and white space from the beginning.
         $query_start = trim(substr($tokens[$start]['content'], 1));
         // Just look at the first word.
         $parts = explode(' ', $query_start);
         $query_start = $parts[0];
         if (in_array(strtoupper($query_start), array('INSERT', 'UPDATE', 'DELETE', 'TRUNCATE')) === true) {
             $warning = 'Do not use %s queries with db_query(), use %s instead';
             $phpcsFile->addWarning($warning, $start, 'DbQuery', array($query_start, 'db_' . strtolower($query_start) . '()'));
         }
     }
 }
 /**
  * Tests the extending classes Sniff class.
  *
  * @dataProvider coreVersionProvider
  */
 public function testCoreVersion($filename, $core_version)
 {
     $this->phpcsFile->expects($this->any())->method('getFilename')->will($this->returnValue($filename));
     $this->assertEquals(DrupalPractice_Project::getCoreVersion($this->phpcsFile), $core_version);
 }