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

Public Member Functions

 processMemberVar (File $phpcsFile, $stackPtr)
 

Protected Member Functions

 processVariable (File $phpcsFile, $stackPtr)
 
 processVariableInString (File $phpcsFile, $stackPtr)
 

Detailed Description

Definition at line 16 of file VariableCommentSniff.php.

Member Function Documentation

◆ processMemberVar()

processMemberVar ( File  $phpcsFile,
  $stackPtr 
)

Called to process class member vars.

Parameters
\PHP_CodeSniffer\Files\File$phpcsFileThe file being scanned.
int$stackPtrThe position of the current token in the stack passed in $tokens.
Returns
void

Definition at line 29 of file VariableCommentSniff.php.

30  {
31  $tokens = $phpcsFile->getTokens();
32  $ignore = array(
33  T_PUBLIC,
34  T_PRIVATE,
35  T_PROTECTED,
36  T_VAR,
37  T_STATIC,
38  T_WHITESPACE,
39  );
40 
41  $commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
42  if ($commentEnd === false
43  || ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
44  && $tokens[$commentEnd]['code'] !== T_COMMENT)
45  ) {
46  $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
47  return;
48  }
49 
50  if ($tokens[$commentEnd]['code'] === T_COMMENT) {
51  $phpcsFile->addError('You must use "/**" style comments for a member variable comment', $stackPtr, 'WrongStyle');
52  return;
53  }
54 
55  $commentStart = $tokens[$commentEnd]['comment_opener'];
56 
57  $foundVar = null;
58  foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
59  if ($tokens[$tag]['content'] === '@var') {
60  if ($foundVar !== null) {
61  $error = 'Only one @var tag is allowed in a member variable comment';
62  $phpcsFile->addError($error, $tag, 'DuplicateVar');
63  } else {
64  $foundVar = $tag;
65  }
66  } else if ($tokens[$tag]['content'] === '@see') {
67  // Make sure the tag isn't empty.
68  $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
69  if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
70  $error = 'Content missing for @see tag in member variable comment';
71  $phpcsFile->addError($error, $tag, 'EmptySees');
72  }
73  } else {
74  $error = '%s tag is not allowed in member variable comment';
75  $data = array($tokens[$tag]['content']);
76  $phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
77  }//end if
78  }//end foreach
79 
80  // The @var tag is the only one we require.
81  if ($foundVar === null) {
82  $error = 'Missing @var tag in member variable comment';
83  $phpcsFile->addError($error, $commentEnd, 'MissingVar');
84  return;
85  }
86 
87  $firstTag = $tokens[$commentStart]['comment_tags'][0];
88  if ($foundVar !== null && $tokens[$firstTag]['content'] !== '@var') {
89  $error = 'The @var tag must be the first tag in a member variable comment';
90  $phpcsFile->addError($error, $foundVar, 'VarOrder');
91  }
92 
93  // Make sure the tag isn't empty and has the correct padding.
94  $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
95  if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
96  $error = 'Content missing for @var tag in member variable comment';
97  $phpcsFile->addError($error, $foundVar, 'EmptyVar');
98  return;
99  }
100 
101  $varType = $tokens[($foundVar + 2)]['content'];
102  $suggestedType = Common::suggestType($varType);
103  if ($varType !== $suggestedType) {
104  $error = 'Expected "%s" but found "%s" for @var tag in member variable comment';
105  $data = array(
106  $suggestedType,
107  $varType,
108  );
109 
110  $fix = $phpcsFile->addFixableError($error, ($foundVar + 2), 'IncorrectVarType', $data);
111  if ($fix === true) {
112  $phpcsFile->fixer->replaceToken(($foundVar + 2), $suggestedType);
113  }
114  }
115 
116  }//end processMemberVar()
$tokens
Definition: cards_list.phtml:9

◆ processVariable()

processVariable ( File  $phpcsFile,
  $stackPtr 
)
protected

Called to process a normal variable.

Not required for this sniff.

Parameters
\PHP_CodeSniffer\Files\File$phpcsFileThe PHP_CodeSniffer file where this token was found.
int$stackPtrThe position where the double quoted string was found.
Returns
void

Definition at line 130 of file VariableCommentSniff.php.

131  {
132 
133  }//end processVariable()

◆ processVariableInString()

processVariableInString ( File  $phpcsFile,
  $stackPtr 
)
protected

Called to process variables found in double quoted strings.

Not required for this sniff.

Parameters
\PHP_CodeSniffer\Files\File$phpcsFileThe PHP_CodeSniffer file where this token was found.
int$stackPtrThe position where the double quoted string was found.
Returns
void

Definition at line 147 of file VariableCommentSniff.php.

148  {
149 
150  }//end processVariableInString()

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