Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExecutableRegExSniff.php
Go to the documentation of this file.
1 <?php
7 
8 use PHP_CodeSniffer\Sniffs\Sniff;
9 use PHP_CodeSniffer\Files\File;
10 use PHP_CodeSniffer\Util\Tokens;
11 
17 class ExecutableRegExSniff implements Sniff
18 {
24  // @codingStandardsIgnoreLine
25  protected $errorMessage = "Possible executable regular expression in %s. Make sure that the pattern doesn't contain 'e' modifier";
26 
32  protected $errorCode = 'PossibleExecutableRegEx';
33 
39  protected $function = 'preg_replace';
40 
46  protected $ignoreTokens = [
47  T_DOUBLE_COLON,
48  T_OBJECT_OPERATOR,
49  T_FUNCTION,
50  T_CONST,
51  T_CLASS,
52  ];
53 
57  public function register()
58  {
59  return [T_STRING];
60  }
61 
65  public function process(File $phpcsFile, $stackPtr)
66  {
67  $tokens = $phpcsFile->getTokens();
68  if ($tokens[$stackPtr]['content'] !== $this->function) {
69  return;
70  }
71  $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
72  if (in_array($tokens[$prevToken]['code'], $this->ignoreTokens)) {
73  return;
74  }
75  $nextToken = $phpcsFile->findNext([T_WHITESPACE, T_OPEN_PARENTHESIS], $stackPtr + 1, null, true);
76  if (in_array($tokens[$nextToken]['code'], Tokens::$stringTokens)
77  && preg_match('/[#\/|~\}\)][imsxADSUXJu]*e[imsxADSUXJu]*.$/', $tokens[$nextToken]['content'])
78  ) {
79  $phpcsFile->addError(
80  $this->errorMessage,
81  $stackPtr,
82  $this->errorCode,
83  [$tokens[$stackPtr]['content']]
84  );
85  }
86  }
87 }
$tokens
Definition: cards_list.phtml:9