本文整理汇总了PHP中PhpCsFixer\Tokenizer\Tokens::getNonWhitespaceSibling方法的典型用法代码示例。如果您正苦于以下问题:PHP Tokens::getNonWhitespaceSibling方法的具体用法?PHP Tokens::getNonWhitespaceSibling怎么用?PHP Tokens::getNonWhitespaceSibling使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpCsFixer\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::getNonWhitespaceSibling方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fixSpaceAboveMethod
/**
* Fix spacing above a method signature.
*
* Deals with comments, PHPDocs and spaces above the method with respect to the position of the method in the class.
*
* @param Tokens $tokens
* @param int $classStart index of the class Token the method is in
* @param int $methodIndex index of the method to fix
*/
private function fixSpaceAboveMethod(Tokens $tokens, $classStart, $methodIndex)
{
static $methodAttr = array(T_PRIVATE, T_PROTECTED, T_PUBLIC, T_ABSTRACT, T_FINAL, T_STATIC);
// find out where the method signature starts
$firstMethodAttrIndex = $methodIndex;
for ($i = $methodIndex; $i > $classStart; --$i) {
$nonWhiteAbove = $tokens->getNonWhitespaceSibling($i, -1);
if (null !== $nonWhiteAbove && $tokens[$nonWhiteAbove]->isGivenKind($methodAttr)) {
$firstMethodAttrIndex = $nonWhiteAbove;
} else {
break;
}
}
// deal with comments above a method
if ($tokens[$nonWhiteAbove]->isGivenKind(T_COMMENT)) {
if (1 === $firstMethodAttrIndex - $nonWhiteAbove) {
// no white space found between comment and method start
$this->correctLineBreaks($tokens, $nonWhiteAbove, $firstMethodAttrIndex, 1);
return;
}
// $tokens[$nonWhiteAbove+1] is always a white space token here
if (substr_count($tokens[$nonWhiteAbove + 1]->getContent(), "\n") > 1) {
// more than one line break, always bring it back to 2 line breaks between the method start and what is above it
$this->correctLineBreaks($tokens, $nonWhiteAbove, $firstMethodAttrIndex, 2);
return;
}
// there are 2 cases:
if ($tokens[$nonWhiteAbove - 1]->isWhitespace() && substr_count($tokens[$nonWhiteAbove - 1]->getContent(), "\n") > 0) {
// 1. The comment is meant for the method (although not a PHPDoc),
// make sure there is one line break between the method and the comment...
$this->correctLineBreaks($tokens, $nonWhiteAbove, $firstMethodAttrIndex, 1);
// ... and make sure there is blank line above the comment (with the exception when it is directly after a class opening)
$nonWhiteAbove = $this->findCommentBlockStart($tokens, $nonWhiteAbove);
$nonWhiteAboveComment = $tokens->getNonWhitespaceSibling($nonWhiteAbove, -1);
$this->correctLineBreaks($tokens, $nonWhiteAboveComment, $nonWhiteAbove, $nonWhiteAboveComment === $classStart ? 1 : 2);
} else {
// 2. The comment belongs to the code above the method,
// make sure there is a blank line above the method (i.e. 2 line breaks)
$this->correctLineBreaks($tokens, $nonWhiteAbove, $firstMethodAttrIndex, 2);
}
return;
}
// deal with method without a PHPDoc above it
if (false === $tokens[$nonWhiteAbove]->isGivenKind(T_DOC_COMMENT)) {
$this->correctLineBreaks($tokens, $nonWhiteAbove, $firstMethodAttrIndex, $nonWhiteAbove === $classStart ? 1 : 2);
return;
}
// there should be one linebreak between the method signature and the PHPDoc above it
$this->correctLineBreaks($tokens, $nonWhiteAbove, $firstMethodAttrIndex, 1);
// there should be one blank line between the PHPDoc and whatever is above (with the exception when it is directly after a class opening)
$nonWhiteAbovePHPDoc = $tokens->getNonWhitespaceSibling($nonWhiteAbove, -1);
$this->correctLineBreaks($tokens, $nonWhiteAbovePHPDoc, $nonWhiteAbove, $nonWhiteAbovePHPDoc === $classStart ? 1 : 2);
}