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

Source for file EmbeddedPhpSniff.php

Documentation is available at EmbeddedPhpSniff.php

  1. <?php
  2. /**
  3.  * Checks the indentation of embedded PHP code segments.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class EmbeddedPhpSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         return array(T_OPEN_TAG);
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Processes this test, when one of its tokens is encountered.
  34.      *
  35.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  36.      * @param int                         $stackPtr  The position of the current token in the
  37.      *                                                stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.  
  45.         // If the close php tag is on the same line as the opening
  46.         // then we have an inline embedded PHP block.
  47.         $closeTag $phpcsFile->findNext(T_CLOSE_TAG$stackPtr);
  48.         if ($closeTag === false || $tokens[$stackPtr]['line'!== $tokens[$closeTag]['line']{
  49.             $this->validateMultilineEmbeddedPhp($phpcsFile$stackPtr);
  50.         else {
  51.             $this->validateInlineEmbeddedPhp($phpcsFile$stackPtr);
  52.         }
  53.  
  54.     }//end process()
  55.  
  56.  
  57.     /**
  58.      * Validates embedded PHP that exists on multiple lines.
  59.      *
  60.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  61.      * @param int                         $stackPtr  The position of the current token in the
  62.      *                                                stack passed in $tokens.
  63.      *
  64.      * @return void 
  65.      */
  66.     private function validateMultilineEmbeddedPhp($phpcsFile$stackPtr)
  67.     {
  68.         $tokens $phpcsFile->getTokens();
  69.  
  70.         $prevTag $phpcsFile->findPrevious(T_OPEN_TAG($stackPtr - 1));
  71.         if ($prevTag === false{
  72.             // This is the first open tag.
  73.             return;
  74.         }
  75.  
  76.         $firstContent $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  77.         $closingTag   $phpcsFile->findNext(T_CLOSE_TAG$stackPtr);
  78.         if ($closingTag !== false{
  79.             $nextContent $phpcsFile->findNext(T_WHITESPACE($closingTag + 1)$phpcsFile->numTokenstrue);
  80.             if ($nextContent === false{
  81.                 // Final closing tag. It will be handled elsewhere.
  82.                 return;
  83.             }
  84.  
  85.             // We have an opening and a closing tag, that lie within other content.
  86.             if ($firstContent === $closingTag{
  87.                 $error 'Empty embedded PHP tag found';
  88.                 $fix   $phpcsFile->addFixableError($error$stackPtr'Empty');
  89.                 if ($fix === true{
  90.                     $phpcsFile->fixer->beginChangeset();
  91.                     for ($i $stackPtr$i <= $closingTag$i++{
  92.                         $phpcsFile->fixer->replaceToken($i'');
  93.                     }
  94.  
  95.                     $phpcsFile->fixer->endChangeset();
  96.                 }
  97.  
  98.                 return;
  99.             }
  100.         }//end if
  101.  
  102.         if ($tokens[$firstContent]['line'=== $tokens[$stackPtr]['line']{
  103.             $error 'Opening PHP tag must be on a line by itself';
  104.             $fix   $phpcsFile->addFixableError($error$stackPtr'ContentAfterOpen');
  105.             if ($fix === true{
  106.                 $first   $phpcsFile->findFirstOnLine(T_WHITESPACE$stackPtrtrue);
  107.                 $padding (strlen($tokens[$first]['content']strlen(ltrim($tokens[$first]['content'])));
  108.                 $phpcsFile->fixer->beginChangeset();
  109.                 $phpcsFile->fixer->addNewline($stackPtr);
  110.                 $phpcsFile->fixer->addContent($stackPtrstr_repeat(' '$padding));
  111.                 $phpcsFile->fixer->endChangeset();
  112.             }
  113.         else {
  114.             // Check the indent of the first line, except if it is a scope closer.
  115.             if (isset($tokens[$firstContent]['scope_closer']=== false
  116.                 || $tokens[$firstContent]['scope_closer'!== $firstContent
  117.             {
  118.                 // Check for a blank line at the top.
  119.                 if ($tokens[$firstContent]['line'($tokens[$stackPtr]['line'+ 1)) {
  120.                     // Find a token on the blank line to throw the error on.
  121.                     $i $stackPtr;
  122.                     do {
  123.                         $i++;
  124.                     while ($tokens[$i]['line'!== ($tokens[$stackPtr]['line'+ 1));
  125.  
  126.                     $error 'Blank line found at start of embedded PHP content';
  127.                     $fix   $phpcsFile->addFixableError($error$i'SpacingBefore');
  128.                     if ($fix === true{
  129.                         $phpcsFile->fixer->beginChangeset();
  130.                         for ($i ($stackPtr + 1)$i $firstContent$i++{
  131.                             if ($tokens[$i]['line'=== $tokens[$firstContent]['line']
  132.                                 || $tokens[$i]['line'=== $tokens[$stackPtr]['line']
  133.                             {
  134.                                 continue;
  135.                             }
  136.  
  137.                             $phpcsFile->fixer->replaceToken($i'');
  138.                         }
  139.  
  140.                         $phpcsFile->fixer->endChangeset();
  141.                     }
  142.                 }//end if
  143.  
  144.                 $first $phpcsFile->findFirstOnLine(T_WHITESPACE$stackPtr);
  145.                 if ($first === false{
  146.                     $first  $phpcsFile->findFirstOnLine(T_INLINE_HTML$stackPtr);
  147.                     $indent (strlen($tokens[$first]['content']strlen(ltrim($tokens[$first]['content'])));
  148.                 else {
  149.                     $indent ($tokens[($first + 1)]['column'- 1);
  150.                 }
  151.  
  152.                 $contentColumn ($tokens[$firstContent]['column'- 1);
  153.                 if ($contentColumn !== $indent{
  154.                     $error 'First line of embedded PHP code must be indented %s spaces; %s found';
  155.                     $data  = array(
  156.                               $indent,
  157.                               $contentColumn,
  158.                              );
  159.                     $fix   $phpcsFile->addFixableError($error$firstContent'Indent'$data);
  160.                     if ($fix === true{
  161.                         $padding str_repeat(' '$indent);
  162.                         if ($contentColumn === 0{
  163.                             $phpcsFile->fixer->addContentBefore($firstContent$padding);
  164.                         else {
  165.                             $phpcsFile->fixer->replaceToken(($firstContent - 1)$padding);
  166.                         }
  167.                     }
  168.                 }
  169.             }//end if
  170.         }//end if
  171.  
  172.         $lastContent $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  173.         if ($tokens[$lastContent]['line'=== $tokens[$stackPtr]['line']
  174.             && trim($tokens[$lastContent]['content']!== ''
  175.         {
  176.             $error 'Opening PHP tag must be on a line by itself';
  177.             $fix   $phpcsFile->addFixableError($error$stackPtr'ContentBeforeOpen');
  178.             if ($fix === true{
  179.                 $first $phpcsFile->findFirstOnLine(T_WHITESPACE$stackPtr);
  180.                 if ($first === false{
  181.                     $first   $phpcsFile->findFirstOnLine(T_INLINE_HTML$stackPtr);
  182.                     $padding (strlen($tokens[$first]['content']strlen(ltrim($tokens[$first]['content'])));
  183.                 else {
  184.                     $padding ($tokens[($first + 1)]['column'- 1);
  185.                 }
  186.  
  187.                 $phpcsFile->fixer->addContentBefore($stackPtr$phpcsFile->eolChar.str_repeat(' '$padding));
  188.             }
  189.         else {
  190.             // Find the first token on the first non-empty line we find.
  191.             for ($first ($stackPtr - 1)$first > 0; $first--{
  192.                 if ($tokens[$first]['line'=== $tokens[$stackPtr]['line']{
  193.                     continue;
  194.                 else if (trim($tokens[$first]['content']!== ''{
  195.                     $first $phpcsFile->findFirstOnLine(array()$firsttrue);
  196.                     break;
  197.                 }
  198.             }
  199.  
  200.             $expected = 0;
  201.             if ($tokens[$first]['code'=== T_INLINE_HTML
  202.                 && trim($tokens[$first]['content']!== ''
  203.             {
  204.                 $expected (strlen($tokens[$first]['content']strlen(ltrim($tokens[$first]['content'])));
  205.             else if ($tokens[$first]['code'=== T_WHITESPACE{
  206.                 $expected ($tokens[($first + 1)]['column'- 1);
  207.             }
  208.  
  209.             $expected += 4;
  210.             $found     ($tokens[$stackPtr]['column'- 1);
  211.             if ($found $expected{
  212.                 $error 'Opening PHP tag indent incorrect; expected no more than %s spaces but found %s';
  213.                 $data  = array(
  214.                           $expected,
  215.                           $found,
  216.                          );
  217.                 $fix   $phpcsFile->addFixableError($error$stackPtr'OpenTagIndent'$data);
  218.                 if ($fix === true{
  219.                     $phpcsFile->fixer->replaceToken(($stackPtr - 1)str_repeat(' '$expected));
  220.                 }
  221.             }
  222.         }//end if
  223.  
  224.         if ($closingTag === false{
  225.             return;
  226.         }
  227.  
  228.         $lastContent $phpcsFile->findPrevious(T_WHITESPACE($closingTag - 1)($stackPtr + 1)true);
  229.         $nextContent $phpcsFile->findNext(T_WHITESPACE($closingTag + 1)nulltrue);
  230.  
  231.         if ($tokens[$lastContent]['line'=== $tokens[$closingTag]['line']{
  232.             $error 'Closing PHP tag must be on a line by itself';
  233.             $fix   $phpcsFile->addFixableError($error$closingTag'ContentBeforeEnd');
  234.             if ($fix === true{
  235.                 $first $phpcsFile->findFirstOnLine(T_WHITESPACE$closingTagtrue);
  236.                 $phpcsFile->fixer->beginChangeset();
  237.                 $phpcsFile->fixer->addContentBefore($closingTagstr_repeat(' '($tokens[$first]['column'- 1)));
  238.                 $phpcsFile->fixer->addNewlineBefore($closingTag);
  239.                 $phpcsFile->fixer->endChangeset();
  240.             }
  241.         else if ($tokens[$nextContent]['line'=== $tokens[$closingTag]['line']{
  242.             $error 'Closing PHP tag must be on a line by itself';
  243.             $fix   $phpcsFile->addFixableError($error$closingTag'ContentAfterEnd');
  244.             if ($fix === true{
  245.                 $first $phpcsFile->findFirstOnLine(T_WHITESPACE$closingTagtrue);
  246.                 $phpcsFile->fixer->beginChangeset();
  247.                 $phpcsFile->fixer->addNewline($closingTag);
  248.                 $phpcsFile->fixer->addContent($closingTagstr_repeat(' '($tokens[$first]['column'- 1)));
  249.                 $phpcsFile->fixer->endChangeset();
  250.             }
  251.         }//end if
  252.  
  253.         $next $phpcsFile->findNext(T_OPEN_TAG($closingTag + 1));
  254.         if ($next === false{
  255.             return;
  256.         }
  257.  
  258.         // Check for a blank line at the bottom.
  259.         if ((isset($tokens[$lastContent]['scope_closer']=== false
  260.             || $tokens[$lastContent]['scope_closer'!== $lastContent)
  261.             && $tokens[$lastContent]['line'($tokens[$closingTag]['line'- 1)
  262.         {
  263.             // Find a token on the blank line to throw the error on.
  264.             $i $closingTag;
  265.             do {
  266.                 $i--;
  267.             while ($tokens[$i]['line'!== ($tokens[$closingTag]['line'- 1));
  268.  
  269.             $error 'Blank line found at end of embedded PHP content';
  270.             $fix   $phpcsFile->addFixableError($error$i'SpacingAfter');
  271.             if ($fix === true{
  272.                 $phpcsFile->fixer->beginChangeset();
  273.                 for ($i ($lastContent + 1)$i $closingTag$i++{
  274.                     if ($tokens[$i]['line'=== $tokens[$lastContent]['line']
  275.                         || $tokens[$i]['line'=== $tokens[$closingTag]['line']
  276.                     {
  277.                         continue;
  278.                     }
  279.  
  280.                     $phpcsFile->fixer->replaceToken($i'');
  281.                 }
  282.  
  283.                 $phpcsFile->fixer->endChangeset();
  284.             }
  285.         }//end if
  286.  
  287.     }//end validateMultilineEmbeddedPhp()
  288.  
  289.  
  290.     /**
  291.      * Validates embedded PHP that exists on one line.
  292.      *
  293.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  294.      * @param int                         $stackPtr  The position of the current token in the
  295.      *                                                stack passed in $tokens.
  296.      *
  297.      * @return void 
  298.      */
  299.     private function validateInlineEmbeddedPhp($phpcsFile$stackPtr)
  300.     {
  301.         $tokens $phpcsFile->getTokens();
  302.  
  303.         // We only want one line PHP sections, so return if the closing tag is
  304.         // on the next line.
  305.         $closeTag $phpcsFile->findNext(T_CLOSE_TAG$stackPtrnullfalse);
  306.         if ($tokens[$stackPtr]['line'!== $tokens[$closeTag]['line']{
  307.             return;
  308.         }
  309.  
  310.         // Check that there is one, and only one space at the start of the statement.
  311.         $firstContent $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)$closeTagtrue);
  312.  
  313.         if ($firstContent === false{
  314.             $error 'Empty embedded PHP tag found';
  315.             $fix   $phpcsFile->addFixableError($error$stackPtr'Empty');
  316.             if ($fix === true{
  317.                 $phpcsFile->fixer->beginChangeset();
  318.                 for ($i $stackPtr$i <= $closeTag$i++{
  319.                     $phpcsFile->fixer->replaceToken($i'');
  320.                 }
  321.  
  322.                 $phpcsFile->fixer->endChangeset();
  323.             }
  324.  
  325.             return;
  326.         }
  327.  
  328.         // The open tag token always contains a single space after it.
  329.         $leadingSpace = 1;
  330.         if ($tokens[($stackPtr + 1)]['code'=== T_WHITESPACE{
  331.             $leadingSpace (strlen($tokens[($stackPtr + 1)]['content']+ 1);
  332.         }
  333.  
  334.         if ($leadingSpace !== 1{
  335.             $error 'Expected 1 space after opening PHP tag; %s found';
  336.             $data  = array($leadingSpace);
  337.             $fix   $phpcsFile->addFixableError($error$stackPtr'SpacingAfterOpen'$data);
  338.             if ($fix === true{
  339.                 $phpcsFile->fixer->replaceToken(($stackPtr + 1)'');
  340.             }
  341.         }
  342.  
  343.         $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($closeTag - 1)$stackPtrtrue);
  344.         if ($prev !== $stackPtr{
  345.             if ((isset($tokens[$prev]['scope_opener']=== false
  346.                 || $tokens[$prev]['scope_opener'!== $prev)
  347.                 && (isset($tokens[$prev]['scope_closer']=== false
  348.                 || $tokens[$prev]['scope_closer'!== $prev)
  349.                 && $tokens[$prev]['code'!== T_SEMICOLON
  350.             {
  351.                 $error 'Inline PHP statement must end with a semicolon';
  352.                 $fix   $phpcsFile->addFixableError($error$stackPtr'NoSemicolon');
  353.                 if ($fix === true{
  354.                     $phpcsFile->fixer->addContent($prev';');
  355.                 }
  356.             else if ($tokens[$prev]['code'=== T_SEMICOLON{
  357.                 $statementCount = 1;
  358.                 for ($i ($stackPtr + 1)$i $prev$i++{
  359.                     if ($tokens[$i]['code'=== T_SEMICOLON{
  360.                         $statementCount++;
  361.                     }
  362.                 }
  363.  
  364.                 if ($statementCount > 1{
  365.                     $error 'Inline PHP statement must contain a single statement; %s found';
  366.                     $data  = array($statementCount);
  367.                     $phpcsFile->addError($error$stackPtr'MultipleStatements'$data);
  368.                 }
  369.             }
  370.         }//end if
  371.  
  372.         $trailingSpace = 0;
  373.         if ($tokens[($closeTag - 1)]['code'=== T_WHITESPACE{
  374.             $trailingSpace strlen($tokens[($closeTag - 1)]['content']);
  375.         else if ($tokens[($closeTag - 1)]['code'=== T_COMMENT
  376.             && substr($tokens[($closeTag - 1)]['content']-1=== ' '
  377.         {
  378.             $trailingSpace (strlen($tokens[($closeTag - 1)]['content']strlen(rtrim($tokens[($closeTag - 1)]['content'])));
  379.         }
  380.  
  381.         if ($trailingSpace !== 1{
  382.             $error 'Expected 1 space before closing PHP tag; %s found';
  383.             $data  = array($trailingSpace);
  384.             $fix   $phpcsFile->addFixableError($error$stackPtr'SpacingBeforeClose'$data);
  385.             if ($fix === true{
  386.                 if ($trailingSpace === 0{
  387.                     $phpcsFile->fixer->addContentBefore($closeTag' ');
  388.                 else if ($tokens[($closeTag - 1)]['code'=== T_COMMENT{
  389.                     $phpcsFile->fixer->replaceToken(($closeTag - 1)rtrim($tokens[($closeTag - 1)]['content']).' ');
  390.                 else {
  391.                     $phpcsFile->fixer->replaceToken(($closeTag - 1)' ');
  392.                 }
  393.             }
  394.         }
  395.  
  396.     }//end validateInlineEmbeddedPhp()
  397.  
  398.  
  399. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.