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

Source for file ForbiddenStylesSniff.php

Documentation is available at ForbiddenStylesSniff.php

  1. <?php
  2. /**
  3.  * Bans the use of some styles, such as deprecated or browser-specific styles.
  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\CSS;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ForbiddenStylesSniff 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 forbidden styles with their alternatives.
  27.      *
  28.      * The value is NULL if no alternative exists. i.e., the
  29.      * function should just not be used.
  30.      *
  31.      * @var array<string, string|null>
  32.      */
  33.     protected $forbiddenStyles = array(
  34.                                   '-moz-border-radius'             => 'border-radius',
  35.                                   '-webkit-border-radius'          => 'border-radius',
  36.                                   '-moz-border-radius-topleft'     => 'border-top-left-radius',
  37.                                   '-moz-border-radius-topright'    => 'border-top-right-radius',
  38.                                   '-moz-border-radius-bottomright' => 'border-bottom-right-radius',
  39.                                   '-moz-border-radius-bottomleft'  => 'border-bottom-left-radius',
  40.                                   '-moz-box-shadow'                => 'box-shadow',
  41.                                   '-webkit-box-shadow'             => 'box-shadow',
  42.                                  );
  43.  
  44.     /**
  45.      * A cache of forbidden style names, for faster lookups.
  46.      *
  47.      * @var string[] 
  48.      */
  49.     protected $forbiddenStyleNames = array();
  50.  
  51.     /**
  52.      * If true, forbidden styles will be considered regular expressions.
  53.      *
  54.      * @var boolean 
  55.      */
  56.     protected $patternMatch = false;
  57.  
  58.     /**
  59.      * If true, an error will be thrown; otherwise a warning.
  60.      *
  61.      * @var boolean 
  62.      */
  63.     public $error = true;
  64.  
  65.  
  66.     /**
  67.      * Returns an array of tokens this test wants to listen for.
  68.      *
  69.      * @return array 
  70.      */
  71.     public function register()
  72.     {
  73.         $this->forbiddenStyleNames array_keys($this->forbiddenStyles);
  74.  
  75.         if ($this->patternMatch === true{
  76.             foreach ($this->forbiddenStyleNames as $i => $name{
  77.                 $this->forbiddenStyleNames[$i'/'.$name.'/i';
  78.             }
  79.         }
  80.  
  81.         return array(T_STYLE);
  82.  
  83.     }//end register()
  84.  
  85.  
  86.     /**
  87.      * Processes this test, when one of its tokens is encountered.
  88.      *
  89.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  90.      * @param int                         $stackPtr  The position of the current token in
  91.      *                                                the stack passed in $tokens.
  92.      *
  93.      * @return void 
  94.      */
  95.     public function process(File $phpcsFile$stackPtr)
  96.     {
  97.         $tokens  $phpcsFile->getTokens();
  98.         $style   strtolower($tokens[$stackPtr]['content']);
  99.         $pattern = null;
  100.  
  101.         if ($this->patternMatch === true{
  102.             $count   = 0;
  103.             $pattern preg_replace(
  104.                 $this->forbiddenStyleNames,
  105.                 $this->forbiddenStyleNames,
  106.                 $style,
  107.                 1,
  108.                 $count
  109.             );
  110.  
  111.             if ($count === 0{
  112.                 return;
  113.             }
  114.  
  115.             // Remove the pattern delimiters and modifier.
  116.             $pattern substr($pattern1-2);
  117.         else {
  118.             if (in_array($style$this->forbiddenStyleNames=== false{
  119.                 return;
  120.             }
  121.         }//end if
  122.  
  123.         $this->addError($phpcsFile$stackPtr$style$pattern);
  124.  
  125.     }//end process()
  126.  
  127.  
  128.     /**
  129.      * Generates the error or warning for this sniff.
  130.      *
  131.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  132.      * @param int                         $stackPtr  The position of the forbidden style
  133.      *                                                in the token array.
  134.      * @param string                      $style     The name of the forbidden style.
  135.      * @param string                      $pattern   The pattern used for the match.
  136.      *
  137.      * @return void 
  138.      */
  139.     protected function addError($phpcsFile$stackPtr$style$pattern=null)
  140.     {
  141.         $data  = array($style);
  142.         $error 'The use of style %s is ';
  143.         if ($this->error === true{
  144.             $type   'Found';
  145.             $error .= 'forbidden';
  146.         else {
  147.             $type   'Discouraged';
  148.             $error .= 'discouraged';
  149.         }
  150.  
  151.         if ($pattern === null{
  152.             $pattern $style;
  153.         }
  154.  
  155.         if ($this->forbiddenStyles[$pattern!== null{
  156.             $data[$this->forbiddenStyles[$pattern];
  157.             if ($this->error === true{
  158.                 $fix $phpcsFile->addFixableError($error.'; use %s instead'$stackPtr$type.'WithAlternative'$data);
  159.             else {
  160.                 $fix $phpcsFile->addFixableWarning($error.'; use %s instead'$stackPtr$type.'WithAlternative'$data);
  161.             }
  162.  
  163.             if ($fix === true{
  164.                 $phpcsFile->fixer->replaceToken($stackPtr$this->forbiddenStyles[$pattern]);
  165.             }
  166.         else {
  167.             if ($this->error === true{
  168.                 $phpcsFile->addError($error$stackPtr$type$data);
  169.             else {
  170.                 $phpcsFile->addWarning($error$stackPtr$type$data);
  171.             }
  172.         }
  173.  
  174.     }//end addError()
  175.  
  176.  
  177. }//end class

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