Gets the index of a PHPDoc tag.

@param File   $file
@param int    $commentStart
@param string $tagName

@return bool|int
499120
protected function findSingleCommentTagIndex(File $file, $commentStart, $tagName)
    {
        $indexes = $this->findCommentTagIndexes($file, $commentStart, $tagName);
        return count($indexes)
            ? array_shift($indexes)
            : false;
    }
545201
public function _processInlineTags(&$tag, $first = FALSE)
    {
        $description = '';
        if (is_array($tag)) $tag = $tag[0];
        if (is_object($tag)) {
            if ($first) {
                $tags =& $tag->firstSentenceTags($this->_doclet);
            } else {
                $tags =& $tag->inlineTags($this->_doclet);
            }
            if ($tags) {
                foreach ($tags as $aTag) {
                    if ($aTag) {
                        $description .= $aTag->text($this->_doclet);
                    }
                }
            }

            return $this->_doclet->formatter->toFormattedText($description);
        }

        return NULL;
    }
1055271
protected function getOpeningTagPos()
    {
        $startPositionInBlob = false;
        if (preg_match("/<" . preg_quote($this->options["uniqueNode"]) . "(>| )/", $this->workingBlob, $matches, PREG_OFFSET_CAPTURE) === 1) {
            $startPositionInBlob = $matches[0][1];
        }


        if ($startPositionInBlob === false) {
            $this->hasSearchedUntilPos = strlen($this->workingBlob) - 1;
        }

        return $startPositionInBlob;
    }
9037
protected function processTag(DOMDocument $xmlDoc, $tagName)
    {
        /** @var $embed \DOMElement */
        foreach ($xmlDoc->getElementsByTagName($tagName) as $embed) {
            if (!$view = $embed->getAttribute('view')) {
                $view = $tagName;
            }

            $embedContent = null;
            $parameters = $this->getParameters($embed);

            if ($contentId = $embed->getAttribute('object_id')) {
                try {
                    /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
                    $content = $this->repository->sudo(
                        static function (Repository $repository) use ($contentId) {
                            return $repository->getContentService()->loadContent($contentId);
                        }
                    );

                    if (
                        !$this->repository->canUser('content', 'read', $content)
                        && !$this->repository->canUser('content', 'view_embed', $content)
                    ) {
                        throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]);
                    }

                    // Check published status of the Content
                    if (
                        $content->getVersionInfo()->status !== APIVersionInfo::STATUS_PUBLISHED
                        && !$this->repository->canUser('content', 'versionread', $content)
                    ) {
                        throw new UnauthorizedException('content', 'versionread', ['contentId' => $contentId]);
                    }

                    $embedContent = $this->fragmentHandler->render(
                        new ControllerReference(
                            'ez_content:embedAction',
                            [
                                'contentId' => $contentId,
                                'viewType' => $view,
                                'layout' => false,
                                'params' => $parameters,
                            ]
                        )
                    );
                } catch (APINotFoundException $e) {
                    if ($this->logger) {
                        $this->logger->error(
                            'While generating embed for xmltext, could not locate ' .
                            'Content object with ID ' . $contentId
                        );
                    }
                }
            } elseif ($locationId = $embed->getAttribute('node_id')) {
                try {
                    /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
                    $location = $this->repository->sudo(
                        static function (Repository $repository) use ($locationId) {
                            return $repository->getLocationService()->loadLocation($locationId);
                        }
                    );

                    if (
                        !$this->repository->canUser('content', 'read', $location->getContentInfo(), $location)
                        && !$this->repository->canUser('content', 'view_embed', $location->getContentInfo(), $location)
                    ) {
                        throw new UnauthorizedException('content', 'read', ['locationId' => $location->id]);
                    }

                    $embedContent = $this->fragmentHandler->render(
                        new ControllerReference(
                            'ez_content:embedAction',
                            [
                                'contentId' => $location->getContentInfo()->id,
                                'locationId' => $location->id,
                                'viewType' => $view,
                                'layout' => false,
                                'params' => $parameters,
                            ]
                        )
                    );
                } catch (APINotFoundException $e) {
                    if ($this->logger) {
                        $this->logger->error(
                            'While generating embed for xmltext, could not locate ' .
                            'Location with ID ' . $locationId
                        );
                    }
                }
            }

            if ($embedContent === null) {
                // Remove empty embed
                $embed->parentNode->removeChild($embed);
            } else {
                while ($embed->hasChildNodes()) {
                    $embed->removeChild($embed->firstChild);
                }
                $embed->appendChild($xmlDoc->createCDATASection($embedContent));
            }
        }
    }
547880
public function getFirstTag($name)
    {
        foreach ($this->tags as $i => $tag) {
            if (strtolower($tag->getName()) == $name) {
                return $this->tags[$i];
            }
        }

        return null;
    }
63042
public TagFileInfo getTagFile(String shortname) {
        TagFileInfo tagFiles[] = getTagFiles();

        if (tagFiles == null || tagFiles.length == 0) {
            return null;
        }

        for (int i=0; i < tagFiles.length; i++) {
            if (tagFiles[i].getName().equals(shortname)) {
                return tagFiles[i];
            }
        }
        return null;
    }
179605
public function check(\SplFileInfo $file, Tokens $tokens)
    {
        $classes = array_keys($tokens->findGivenKind(T_CLASS));
        $numClasses = count($classes);

        for ($i = 0; $i < $numClasses; ++$i) {
            $index = $classes[$i];

            // figure out where the classy starts
            $classStart = $tokens->getNextTokenOfKind($index, array('{'));

            // figure out where the classy ends
            $classEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $classStart);

            $this->checkMethods($tokens, $classStart, $classEnd);
        }
    }
420195
private function loadPrevDocBlockContent(int $startPosition): int
    {
        return $this->file->findPrevious(
            [
                T_DOC_COMMENT_OPEN_TAG,
                T_DOC_COMMENT_STAR,
                T_DOC_COMMENT_WHITESPACE,
            ],
            $startPosition - 1,
            $this->getDocCommentPos(),
            true
        );
    }
1366573
protected function addReturnAnnotation(File $phpcsFile, int $docBlockStartIndex, int $docBlockEndIndex, string $returnType = 'void'): void
    {
        $indentation = $this->getIndentationWhitespace($phpcsFile, $docBlockEndIndex);

        $lastLineEndIndex = $phpcsFile->findPrevious([T_DOC_COMMENT_WHITESPACE], $docBlockEndIndex - 1, null, true);

        $phpcsFile->fixer->beginChangeset();
        $phpcsFile->fixer->addNewline($lastLineEndIndex);
        $phpcsFile->fixer->addContent($lastLineEndIndex, $indentation . '* @return ' . $returnType);
        $phpcsFile->fixer->endChangeset();
    }
252495
protected function processReturn($commentStart, $commentEnd)
    {
        $tokens = $this->currentFile->getTokens();
        $funcPtr = $this->currentFile->findNext(T_FUNCTION, $commentEnd);

        // Only check for a return comment if a non-void return statement exists
        if (isset($tokens[$funcPtr]['scope_opener'])) {
            $start = $tokens[$funcPtr]['scope_opener'];

            // iterate over all return statements of this function,
            // run the check on the first which is not only 'return;'
            while ($returnToken = $this->currentFile->findNext(T_RETURN, $start, $tokens[$funcPtr]['scope_closer'])) {
                if ($this->isMatchingReturn($tokens, $returnToken)) {
                    parent::processReturn($commentStart, $commentEnd);
                    break;
                }
                $start = $returnToken + 1;
            }
        }

    }
863241
protected function _parse(\WikiRenderer\InlineTag $tag, $posstart)
    {
        $checkNextTag = true;

        // we analyse each part of the string,
        for ($i = $posstart + 1; $i < $this->end; ++$i) {
            $t = &$this->str[$i];

            // is it the escape char ?
            if ($this->escapeChar != '' && $t === $this->escapeChar) {
                if ($checkNextTag) {
                    $t = ''; // yes -> let's ignore the tag
                    $checkNextTag = false;
                } else {
                    // if we are here, this is because the previous part was the escape char
                    $tag->addContentString($this->escapeChar);
                    if ($this->config->outputDoubleEscapeChar) {
                        $tag->addContentString($this->escapeChar);
                    }
                    $checkNextTag = true;
                }
            // no escape char, and previous token allowed us to
            // take care of the current token, so let's processing it
            } elseif ($checkNextTag) {
                $result = $tag->isSupportedToken($t);
                if ($result === $tag::END_TOKEN) {
                    return $i;
                }
                else if ($result !== false) {
                    // nothing
                }
                else if (!$tag->isOtherTagAllowed()) {
                    $tag->addContentString($t);
                }
                // is there a tag which begin something ?
                elseif (isset($this->currentTextLineContainer->allowedTags[$t])) {
                    $newtag = clone $this->currentTextLineContainer->allowedTags[$t];
                    $i = $this->_parse($newtag, $i);
                    if ($i !== false) {
                        $tag->addContentGenerator($newtag->getWikiContent(), $newtag->getContent());
                    } else {
                        $i = $this->end;
                        $tag->addContentGenerator($newtag->getWikiContent(), $newtag->getBogusContent());
                    }
                }
                // is there a simple tag ?
                elseif (isset($this->allSimpleTags[$t])) {
                    $tag->addContentGenerator($t, $this->allSimpleTags[$t]->getContent($this->documentGenerator, $t));
                } else {
                    $tag->addContentString($t);
                }
            // previous token prevents us to process the current token, and
            // indicated to ignore it, so let's ignore it.
            } else {
                if (isset($this->currentTextLineContainer->allowedTags[$t]) ||
                    isset($this->allSimpleTags[$t])
                ) {
                    if ($this->config->outputEscapeCharForTags) {
                        $tag->addContentString($this->escapeChar . $t);
                    } else {
                        $tag->addContentString($t);
                    }
                } else {
                    if ($this->config->outputEscapeChar) {
                        $tag->addContentString($this->escapeChar . $t);
                    } else {
                        $tag->addContentString($t);
                    }
                }
                $checkNextTag = true;
            }
        }

        if (!$checkNextTag &&
            ($this->config->outputEscapeChar || 
            $this->config->outputEscapeCharAtEOL)
        ) {
            $tag->addContentString($this->escapeChar);
        }
        if (!$tag->isLineContainer()) {
            //we didn't find the ended tag, error
            $this->error = true;

            return false;
        } else {
            return $this->end;
        }
    }
826320
public function findFirstOnLine(File $phpcsFile, $start)
    {
        $tokens = $phpcsFile->getTokens();

        for ($i = $start; $i >= 0; $i--) {
            if ($tokens[$i]['line'] === $tokens[$start]['line']) {
                continue;
            }

            return ($i + 1);
        }

        return false;
    }
766128
protected function readTags(File $phpCsFile, int $docBlockStartIndex, int $docBlockEndIndex): array
    {
        $tokens = $phpCsFile->getTokens();

        $tags = [];

        for ($i = $docBlockStartIndex; $i < $docBlockEndIndex; $i++) {
            if ($tokens[$i]['code'] !== T_DOC_COMMENT_TAG) {
                continue;
            }

            $start = $this->getFirstTokenOfLine($tokens, $i);
            $end = $this->getEndIndex($tokens, $i);
            $tagEnd = $this->getTagEndIndex($tokens, $start, $end);

            $tag = [
                'index' => $i,
                'tag' => $tokens[$i]['content'],
                'tagEnd' => $tagEnd,
                'start' => $start,
                'end' => $end,
                'content' => $this->getContent($tokens, $i, $tagEnd),
            ];
            $tags[] = $tag;
            $i = $end;
        }

        return $tags;
    }
753731
protected function checkInlineComments(File $phpCsFile, $startIndex, $endIndex) {
		$tokens = $phpCsFile->getTokens();

		for ($i = $startIndex + 1; $i < $endIndex; $i++) {
			if ($tokens[$i]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
				continue;
			}

			$commentEndTag = $tokens[$i]['comment_closer'];

			$this->fixDocCommentCloseTags($phpCsFile, $commentEndTag);

			// We skip for multiline for now
			if ($tokens[$i]['line'] !== $tokens[$commentEndTag]['line']) {
				continue;
			}

			$typeTag = $this->_findTagIndex($tokens, $i, $commentEndTag, T_DOC_COMMENT_TAG);
			$contentTag = $this->_findTagIndex($tokens, $i, $commentEndTag, T_DOC_COMMENT_STRING);

			if ($typeTag === null || $contentTag === null) {
				$phpCsFile->addError('Invalid Inline Doc Block', $startIndex, 'DocBlockInvalid');
				return;
			}

			if ($tokens[$typeTag]['content'] !== '@var') {
				// We ignore those
				return;
			}

			$errors = $this->findErrors($phpCsFile, $contentTag);

			if (!$errors) {
				continue;
			}

			$fix = $phpCsFile->addFixableError('Invalid Inline Doc Block content: ' . implode(', ', $errors), $i, 'DocBlockContentInvalid');
			if (!$fix) {
				continue;
			}

			$phpCsFile->fixer->beginChangeset();

			$comment = $tokens[$contentTag]['content'];

			if (isset($errors['space-before-end']) || isset($errors['end'])) {
				$comment .= ' ';
			}

			if (isset($errors['order'])) {
				$comment = preg_replace('|^(.+?)\s+(.+?)\s*$|', '\2 \1 ', $comment);
			}

			$phpCsFile->fixer->replaceToken($contentTag, $comment);

			$phpCsFile->fixer->endChangeset();
		}
	}
471943
private function getDocCommentFromFile($fileName)
    {
        $lines = file($fileName, FILE_IGNORE_NEW_LINES);

        $declaringClassStartLine = $this->getDeclaringClass()->getStartLine() - 1;
        $declaringClassLength = $this->getDeclaringClass()->getEndLine() - $declaringClassStartLine + 1;

        $currentClassLines = array_slice($lines, $declaringClassStartLine, $declaringClassLength, true);

        // Need the php open tag to tokenize the class.
        $tokens = token_get_all("<?php\n" . implode("\n", $currentClassLines));

        $return = false;
        $constDeclarationKey = $this->getCurrentConstantKeyFromClassTokens($tokens);

        // Now we have the key value of the constant declaration, we have to pick up the comment before the declaration (if it exists).
        for ($constDeclarationKey--; $constDeclarationKey > 0; $constDeclarationKey--) {
            if ($tokens[$constDeclarationKey][0] !== T_WHITESPACE && $tokens[$constDeclarationKey][0] !== T_DOC_COMMENT) {
                break;
            } elseif ($tokens[$constDeclarationKey][0] === T_DOC_COMMENT) {
                $return = $tokens[$constDeclarationKey][1];
                break;
            }
        }

        return $return;
    }
1309567
protected function validateInheritdoc(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart, $commentEnd)
    {
        $classHelper = new ClassHelper($phpcsFile);

        $commentString = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart + 1);
        if (preg_match('/\{\@inheritdoc\}/', $commentString)) {
            $classes = $classHelper->getClassParentsAndInterfaces($stackPtr, 'validate {@inheritdoc}');
            if (false !== $classes) {
                $method = $phpcsFile->getDeclarationName($stackPtr);
                foreach ($classes as $class) {
                    if (method_exists($class, $method)) {
                        return true;
                    }
                }
                $error = 'No overrided method found for {@inheritdoc} annotation';
                $phpcsFile->addError($error, $commentStart, 'InvalidInheritdoc');
            } else {
                return true;
            }
        }
        return false;
    }
1308559
private function getFieldIndex($prevTag, $tag, $fieldIndex)
    {
        if ($prevTag == $tag or '' == $prevTag) {
            return $fieldIndex; // iteration of field index will continue
        }
        $specTag = $this->currentSpec['field']->getTag();
        if (preg_match('/'.$specTag.'/', $tag)) {
            // not same field tag, but field spec tag matches
            return $fieldIndex; // iteration of field index will continue
        }
        // not same field tag, iteration gets reset
        return $this->spec['field']->getIndexStart();
    }