Example #1
0
 /**
  * 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 int
  */
 public function process(File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $fileName = $phpcsFile->getFileName();
     $extension = substr($fileName, strrpos($fileName, '.'));
     $nextClass = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_TRAIT), $stackPtr);
     if ($nextClass !== false) {
         $phpcsFile->recordMetric($stackPtr, 'File extension for class files', $extension);
         if ($extension === '.php') {
             $error = '%s found in ".php" file; use ".inc" extension instead';
             $data = array(ucfirst($tokens[$nextClass]['content']));
             $phpcsFile->addError($error, $stackPtr, 'ClassFound', $data);
         }
     } else {
         $phpcsFile->recordMetric($stackPtr, 'File extension for non-class files', $extension);
         if ($extension === '.inc') {
             $error = 'No interface or class found in ".inc" file; use ".php" extension instead';
             $phpcsFile->addError($error, $stackPtr, 'NoClass');
         }
     }
     // Ignore the rest of the file.
     return $phpcsFile->numTokens + 1;
 }
 /**
  * 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(File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     // Make sure this is the first PHP open tag so we don't process the
     // same file twice.
     $prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1);
     if ($prevOpenTag !== false) {
         return;
     }
     $path = $phpcsFile->getFileName();
     $properties = $this->getProperties($path);
     if ($properties === null) {
         // Not under version control.
         return;
     }
     $allProperties = $properties + $this->properties;
     foreach ($allProperties as $key => $value) {
         if (isset($properties[$key]) === true && isset($this->properties[$key]) === false) {
             $error = 'Unexpected Subversion property "%s" = "%s"';
             $data = array($key, $properties[$key]);
             $phpcsFile->addError($error, $stackPtr, 'Unexpected', $data);
             continue;
         }
         if (isset($properties[$key]) === false && isset($this->properties[$key]) === true) {
             $error = 'Missing Subversion property "%s" = "%s"';
             $data = array($key, $this->properties[$key]);
             $phpcsFile->addError($error, $stackPtr, 'Missing', $data);
             continue;
         }
         if ($properties[$key] !== null && $properties[$key] !== $this->properties[$key]) {
             $error = 'Subversion property "%s" = "%s" does not match "%s"';
             $data = array($key, $properties[$key], $this->properties[$key]);
             $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
         }
     }
     //end foreach
 }