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

Source for file DuplicatePropertySniff.php

Documentation is available at DuplicatePropertySniff.php

  1. <?php
  2. /**
  3.  * Ensures JS classes don't contain duplicate property names.
  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\Classes;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class DuplicatePropertySniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array('JS');
  24.  
  25.  
  26.     /**
  27.      * Returns an array of tokens this test wants to listen for.
  28.      *
  29.      * @return array 
  30.      */
  31.     public function register()
  32.     {
  33.         return array(T_OBJECT);
  34.  
  35.     }//end register()
  36.  
  37.  
  38.     /**
  39.      * Processes this test, when one of its tokens is encountered.
  40.      *
  41.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.
  42.      * @param int                         $stackPtr  The position of the current token in the
  43.      *                                                stack passed in $tokens.
  44.      *
  45.      * @return void 
  46.      */
  47.     public function process(File $phpcsFile$stackPtr)
  48.     {
  49.         $tokens $phpcsFile->getTokens();
  50.  
  51.         $properties   = array();
  52.         $wantedTokens = array(
  53.                          T_PROPERTY,
  54.                          T_OBJECT,
  55.                         );
  56.  
  57.         $next $phpcsFile->findNext($wantedTokens($stackPtr + 1)$tokens[$stackPtr]['bracket_closer']);
  58.         while ($next !== false && $next $tokens[$stackPtr]['bracket_closer']{
  59.             if ($tokens[$next]['code'=== T_OBJECT{
  60.                 // Skip nested objects.
  61.                 $next $tokens[$next]['bracket_closer'];
  62.             else {
  63.                 $propName $tokens[$next]['content'];
  64.                 if (isset($properties[$propName]=== true{
  65.                     $error 'Duplicate property definition found for "%s"; previously defined on line %s';
  66.                     $data  = array(
  67.                               $propName,
  68.                               $tokens[$properties[$propName]]['line'],
  69.                              );
  70.                     $phpcsFile->addError($error$next'Found'$data);
  71.                 }
  72.  
  73.                 $properties[$propName$next;
  74.             }//end if
  75.  
  76.             $next $phpcsFile->findNext($wantedTokens($next + 1)$tokens[$stackPtr]['bracket_closer']);
  77.         }//end while
  78.  
  79.     }//end process()
  80.  
  81.  
  82. }//end class

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