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

Source for file BrowserSpecificStylesSniff.php

Documentation is available at BrowserSpecificStylesSniff.php

  1. <?php
  2. /**
  3.  * Ensure that browser-specific styles are not used.
  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\MySource\Sniffs\CSS;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class BrowserSpecificStylesSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array('CSS');
  24.  
  25.     /**
  26.      * A list of specific stylesheet suffixes we allow.
  27.      *
  28.      * These stylesheets contain browser specific styles
  29.      * so this sniff ignore them files in the form:
  30.      * *_moz.css and *_ie7.css etc.
  31.      *
  32.      * @var array 
  33.      */
  34.     protected $specificStylesheets = array(
  35.                                       'moz'    => true,
  36.                                       'ie'     => true,
  37.                                       'ie7'    => true,
  38.                                       'ie8'    => true,
  39.                                       'webkit' => true,
  40.                                      );
  41.  
  42.  
  43.     /**
  44.      * Returns the token types that this sniff is interested in.
  45.      *
  46.      * @return int[] 
  47.      */
  48.     public function register()
  49.     {
  50.         return array(T_STYLE);
  51.  
  52.     }//end register()
  53.  
  54.  
  55.     /**
  56.      * Processes the tokens that this sniff is interested in.
  57.      *
  58.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  59.      * @param int                         $stackPtr  The position in the stack where
  60.      *                                                the token was found.
  61.      *
  62.      * @return void 
  63.      */
  64.     public function process(File $phpcsFile$stackPtr)
  65.     {
  66.         // Ignore files with browser-specific suffixes.
  67.         $filename  $phpcsFile->getFilename();
  68.         $breakChar strrpos($filename'_');
  69.         if ($breakChar !== false && substr($filename-4=== '.css'{
  70.             $specific substr($filename($breakChar + 1)-4);
  71.             if (isset($this->specificStylesheets[$specific]=== true{
  72.                 return;
  73.             }
  74.         }
  75.  
  76.         $tokens  $phpcsFile->getTokens();
  77.         $content $tokens[$stackPtr]['content'];
  78.  
  79.         if ($content{0=== '-'{
  80.             $error 'Browser-specific styles are not allowed';
  81.             $phpcsFile->addError($error$stackPtr'ForbiddenStyle');
  82.         }
  83.  
  84.     }//end process()
  85.  
  86.  
  87. }//end class

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