PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file SyntaxSniff.php

Documentation is available at SyntaxSniff.php

  1. <?php
  2. /**
  3.  * Ensures PHP believes the syntax is clean.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @author    Blaine Schmeisser <blainesch@gmail.com>
  7.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  8.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  9.  */
  10.  
  11. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
  12.  
  13. use PHP_CodeSniffer\Sniffs\Sniff;
  14. use PHP_CodeSniffer\Files\File;
  15. use PHP_CodeSniffer\Config;
  16.  
  17. class SyntaxSniff implements Sniff
  18. {
  19.  
  20.  
  21.     /**
  22.      * Returns an array of tokens this test wants to listen for.
  23.      *
  24.      * @return array 
  25.      */
  26.     public function register()
  27.     {
  28.         return array(T_OPEN_TAG);
  29.  
  30.     }//end register()
  31.  
  32.  
  33.     /**
  34.      * Processes this test, when one of its tokens is encountered.
  35.      *
  36.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  37.      * @param int                  $stackPtr  The position of the current token in
  38.      *                                         the stack passed in $tokens.
  39.      *
  40.      * @return void 
  41.      */
  42.     public function process(File $phpcsFile$stackPtr)
  43.     {
  44.         $phpPath = Config::getExecutablePath('php');
  45.         if ($phpPath === null{
  46.             // PHP_BINARY is available in PHP 5.4+.
  47.             if (defined('PHP_BINARY'=== true{
  48.                 $phpPath = PHP_BINARY;
  49.             else {
  50.                 return;
  51.             }
  52.         }
  53.  
  54.         $fileName $phpcsFile->getFilename();
  55.         $cmd      = "$phpPath -l \"$fileName\" 2>&1";
  56.         $output   shell_exec($cmd);
  57.  
  58.         $matches = array();
  59.         if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/'trim($output)$matches=== 1{
  60.             $error trim($matches[1]);
  61.             $line  = (int) $matches[2];
  62.             $phpcsFile->addErrorOnLine("PHP syntax error: $error"$line'PHPSyntax');
  63.         }
  64.  
  65.         // Ignore the rest of the file.
  66.         return ($phpcsFile->numTokens + 1);
  67.  
  68.     }//end process()
  69.  
  70.  
  71. }//end class

Documentation generated on Mon, 11 Mar 2019 14:53:43 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.