Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ZeroUnitsSniff.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Sniffs\Less;
7 
8 use PHP_CodeSniffer\Sniffs\Sniff;
9 use PHP_CodeSniffer\Files\File;
10 
21 class ZeroUnitsSniff implements Sniff
22 {
23  const CSS_PROPERTY_UNIT_PX = 'px';
24  const CSS_PROPERTY_UNIT_EM = 'em';
25  const CSS_PROPERTY_UNIT_REM = 'rem';
26 
32  private $units = [
36  ];
37 
44 
48  public function register()
49  {
50  return [T_LNUMBER, T_DNUMBER];
51  }
52 
56  public function process(File $phpcsFile, $stackPtr)
57  {
58  $tokens = $phpcsFile->getTokens();
59  $tokenCode = $tokens[$stackPtr]['code'];
60  $tokenContent = $tokens[$stackPtr]['content'];
61 
62  $nextToken = $tokens[$stackPtr + 1];
63 
64  if (T_LNUMBER === $tokenCode
65  && "0" === $tokenContent
66  && T_STRING === $nextToken['code']
67  && in_array($nextToken['content'], $this->units)
68  ) {
69  $phpcsFile->addError('Units specified for "0" value', $stackPtr, 'ZeroUnitFound');
70  }
71 
72  if ((T_DNUMBER === $tokenCode)
73  && 0 === strpos($tokenContent, "0")
74  && ((float)$tokenContent < 1)
75  ) {
76  $phpcsFile->addError('Values starts from "0"', $stackPtr, 'ZeroUnitFound');
77  }
78  }
79 }
process(File $phpcsFile, $stackPtr)
$tokens
Definition: cards_list.phtml:9