<commit_before><?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace PSR2R\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PSR2R\Tools\AbstractSniff;

/**
 * Doc blocks for class attributes should not have the variable name duplicated.
 * Type suffices: `@var <type>`.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME>
 * @license MIT
 */
class DocBlockVarWithoutNameSniff extends AbstractSniff {

	/**
	 * @inheritDoc
	 */
	public function process(File $phpcsFile, $stackPtr) {
		$tokens = $phpcsFile->getTokens();
		$docBlockStartIndex = $stackPtr;
		$docBlockEndIndex = $tokens[$stackPtr]['comment_closer'];

		$indentationLevel = $this->getIndentationLevel($phpcsFile, $stackPtr);

		// Skip for inline comments
		if ($indentationLevel > 1) {
			return;
		}

		for ($i = $docBlockStartIndex + 1; $i < $docBlockEndIndex; $i++) {
			if ($tokens[$i]['type'] !== 'T_DOC_COMMENT_TAG') {
				continue;
			}
			if ($tokens[$i]['content'] !== '@var') {
				continue;
			}

			//$nextIndex = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $i + 1, $docBlockEndIndex, true);
			$nextIndex = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $i + 1, $docBlockEndIndex);
			if (!$nextIndex || strpos($tokens[$nextIndex]['content'], ' ') === false) {
				continue;
			}

			$content = $tokens[$nextIndex]['content'];
			preg_match_all('/ \$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $content, $matches);

			if (isset($matches[0][0]) && $matches[0][0] !== '$this') {
				$fix = $phpcsFile->addFixableError('@var annotations should not contain the variable name.', $i,
					'RemoveVarName');
				if ($fix) {
					$phpcsFile->fixer->replaceToken($nextIndex, str_replace($matches[0][0], '', $content));
				}
			}
		}
	}

	/**
	 * @inheritDoc
	 */
	public function register() {
		return [T_DOC_COMMENT_OPEN_TAG];
	}

}<commit_msg>Fix template false positive<commit_after><?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace PSR2R\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PSR2R\Tools\AbstractSniff;

/**
 * Doc blocks for class attributes should not have the variable name duplicated.
 * Type suffices: `@var <type>`.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME>
 * @license MIT
 */
class DocBlockVarWithoutNameSniff extends AbstractSniff {

	/**
	 * @inheritDoc
	 */
	public function process(File $phpcsFile, $stackPtr) {
		$tokens = $phpcsFile->getTokens();
		$docBlockStartIndex = $stackPtr;
		$docBlockEndIndex = $tokens[$stackPtr]['comment_closer'];

		$indentationLevel = $this->getIndentationLevel($phpcsFile, $stackPtr);

		// Skip for inline comments
		if ($indentationLevel > 1) {
			return;
		}

		for ($i = $docBlockStartIndex + 1; $i < $docBlockEndIndex; $i++) {
			if ($tokens[$i]['type'] !== 'T_DOC_COMMENT_TAG') {
				continue;
			}
			if ($tokens[$i]['content'] !== '@var') {
				continue;
			}

			//$nextIndex = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $i + 1, $docBlockEndIndex, true);
			$nextIndex = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $i + 1, $docBlockEndIndex);
			if (!$nextIndex || strpos($tokens[$nextIndex]['content'], ' ') === false) {
				continue;
			}

			$content = $tokens[$nextIndex]['content'];
			preg_match_all('/ \$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $content, $matches);

			if (isset($matches[0][0]) && trim($matches[0][0]) !== '$this') {var_dump($matches);die();
				$fix = $phpcsFile->addFixableError('@var annotations should not contain the variable name.', $i,
					'RemoveVarName');
				if ($fix) {
					$phpcsFile->fixer->replaceToken($nextIndex, str_replace($matches[0][0], '', $content));
				}
			}
		}
	}

	/**
	 * @inheritDoc
	 */
	public function register() {
		return [T_DOC_COMMENT_OPEN_TAG];
	}

}
{'fraction_non_alphanumeric': 0.1807644882860666, 'fraction_numerical': 0.007891491985203452, 'mean_word_length': 4.01980198019802, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2365034', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1413, 'n_words': 310}