Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
StringPositionSniff Class Reference
Inheritance diagram for StringPositionSniff:

Public Member Functions

 register ()
 
 process (File $phpcsFile, $stackPtr)
 

Protected Member Functions

 findIdentical ($leftCurrentPosition, $rightCurrentPosition)
 
 findFunctionParenthesisCloser ($currentPosition)
 

Protected Attributes

 $errorMessage = 'Identical operator === is not used for testing the return value of %s function'
 
 $errorCode = 'ImproperValueTesting'
 
 $functions
 
 $tokens = []
 
 $file
 
 $leftLimit
 
 $rightLimit
 
 $leftRangeTokens
 
 $rightRangeTokens
 
 $identical
 

Detailed Description

Detects misusing of IS_IDENTICAL operators when strpos and stripos functions are used in a condition.

Examples: if (!strpos($haystack, $needle)) {} if (stripos($haystack, $needle) == false) {}

Definition at line 18 of file StringPositionSniff.php.

Member Function Documentation

◆ findFunctionParenthesisCloser()

findFunctionParenthesisCloser (   $currentPosition)
protected

Finds the position of close parenthesis of detected function.

Parameters
int$currentPosition
Returns
mixed

Definition at line 163 of file StringPositionSniff.php.

164  {
165  $nextOpenParenthesis = $this->file->findNext(T_OPEN_PARENTHESIS, $currentPosition, $this->rightLimit);
166  return $nextOpenParenthesis ? $this->tokens[$nextOpenParenthesis]['parenthesis_closer'] : false;
167  }

◆ findIdentical()

findIdentical (   $leftCurrentPosition,
  $rightCurrentPosition 
)
protected

Recursively finds identical operators in current scope.

Parameters
int$leftCurrentPosition
int$rightCurrentPosition
Returns
bool

Definition at line 142 of file StringPositionSniff.php.

143  {
144  $leftBound = $this->file->findPrevious($this->leftRangeTokens, $leftCurrentPosition, $this->leftLimit - 1);
145  $rightBound = $this->file->findNext($this->rightRangeTokens, $rightCurrentPosition, $this->rightLimit + 1);
146  $leftToken = $this->tokens[$leftBound];
147  $rightToken = $this->tokens[$rightBound];
148  if ($leftToken['code'] === T_OPEN_PARENTHESIS && $rightToken['code'] === T_CLOSE_PARENTHESIS) {
149  return $this->findIdentical($leftBound - 1, $rightBound + 1);
150  } else {
151  return (
152  in_array($leftToken['code'], $this->identical) || in_array($rightToken['code'], $this->identical)
153  ) ?: false;
154  }
155  }
findIdentical($leftCurrentPosition, $rightCurrentPosition)

◆ process()

process ( File  $phpcsFile,
  $stackPtr 
)

{}

Definition at line 119 of file StringPositionSniff.php.

120  {
121  $this->tokens = $phpcsFile->getTokens();
122  $this->file = $phpcsFile;
123  $this->leftLimit = $open = $this->tokens[$stackPtr]['parenthesis_opener'];
124  $this->rightLimit = $close = $this->tokens[$stackPtr]['parenthesis_closer'];
125  for ($i = ($open + 1); $i < $close; $i++) {
126  if (($this->tokens[$i]['code'] === T_STRING && in_array($this->tokens[$i]['content'], $this->functions))
127  && (!$this->findIdentical($i - 1, $this->findFunctionParenthesisCloser($i) + 1))
128  ) {
129  $foundFunctionName = $this->tokens[$i]['content'];
130  $phpcsFile->addError($this->errorMessage, $i, $this->errorCode, [$foundFunctionName]);
131  }
132  }
133  }
findIdentical($leftCurrentPosition, $rightCurrentPosition)
$i
Definition: gallery.phtml:31

◆ register()

register ( )

{}

Definition at line 111 of file StringPositionSniff.php.

112  {
113  return [T_IF, T_ELSEIF];
114  }

Field Documentation

◆ $errorCode

$errorCode = 'ImproperValueTesting'
protected

Definition at line 32 of file StringPositionSniff.php.

◆ $errorMessage

$errorMessage = 'Identical operator === is not used for testing the return value of %s function'
protected

Definition at line 25 of file StringPositionSniff.php.

◆ $file

$file
protected

Definition at line 56 of file StringPositionSniff.php.

◆ $functions

$functions
protected
Initial value:
= [
'strpos',
'stripos',
]

Definition at line 39 of file StringPositionSniff.php.

◆ $identical

$identical
protected
Initial value:
= [
T_IS_IDENTICAL,
T_IS_NOT_IDENTICAL,
]

Definition at line 103 of file StringPositionSniff.php.

◆ $leftLimit

$leftLimit
protected

Definition at line 63 of file StringPositionSniff.php.

◆ $leftRangeTokens

$leftRangeTokens
protected
Initial value:
= [
T_IS_IDENTICAL,
T_IS_NOT_IDENTICAL,
T_OPEN_PARENTHESIS,
T_BOOLEAN_AND,
T_BOOLEAN_OR,
]

Definition at line 77 of file StringPositionSniff.php.

◆ $rightLimit

$rightLimit
protected

Definition at line 70 of file StringPositionSniff.php.

◆ $rightRangeTokens

$rightRangeTokens
protected
Initial value:
= [
T_IS_IDENTICAL,
T_IS_NOT_IDENTICAL,
T_CLOSE_PARENTHESIS,
T_BOOLEAN_AND,
T_BOOLEAN_OR,
]

Definition at line 90 of file StringPositionSniff.php.

◆ $tokens

$tokens = []
protected

Definition at line 49 of file StringPositionSniff.php.


The documentation for this class was generated from the following file: