<commit_before><?php
/**
 * Restricts array assignment of certain keys
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @author   <NAME> <<EMAIL>>
 */
class WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff implements PHP_CodeSniffer_Sniff
{

	/**
	 * Exclude groups
	 *
	 * Example: 'foo,bar'
	 * 
	 * @var string Comma-delimited group list 
	 */
	public $exclude = '';

	/**
	 * Groups of variable data to check against.
	 * Don't use this in extended classes, override getGroups() instead.
	 * This is only used for Unit tests.
	 * 
	 * @var array
	 */
	public static $groups = array();


	/**
	 * Returns an array of tokens this test wants to listen for.
	 *
	 * @return array
	 */
	public function register()
	{
		return array(
				T_DOUBLE_ARROW,
				T_CLOSE_SQUARE_BRACKET,
				T_CONSTANT_ENCAPSED_STRING,
				T_DOUBLE_QUOTED_STRING,
			   );

	}//end register()

	/**
	 * Groups of variables to restrict
	 * This should be overridden in extending classes.
	 *
	 * Example: groups => array(
	 * 	'wpdb' => array(
	 * 		'type' => 'error' | 'warning',
	 * 		'message' => 'Dont use this one please!',
	 * 		'variables' => array( '$val', '$var' ),
	 * 		'object_vars' => array( '$foo->bar', .. ),
	 * 		'array_members' => array( '$foo['bar']', .. ),
	 * 	)
	 * )
	 *
	 * @return array
	 */
	public function getGroups() {
		return self::$groups;
	}


	/**
	 * Processes this test, when one of its tokens is encountered.
	 *
	 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
	 * @param int                  $stackPtr  The position of the current token
	 *                                        in the stack passed in $tokens.
	 *
	 * @return void
	 */
	public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr )
	{
		$tokens = $phpcsFile->getTokens();

		$token = $tokens[$stackPtr];

		$exclude = explode( ',', $this->exclude );

		$groups = $this->getGroups();

		if ( empty( $groups ) ) {
			return;
		}

		if ( in_array( $token['code'], array( T_CLOSE_SQUARE_BRACKET ) ) ) {
			$equal = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true );
			if ( $tokens[$equal]['code'] !== T_EQUAL ) {
				return; // This is not an assignment!
			}
		}

		// Instances: Multi-dimentional array, keyed by line
		$inst = array();


		// $foo = array( 'bar' => 'taz' );
		// $foo['bar'] = $taz;
		if ( in_array( $token['code'], array( T_CLOSE_SQUARE_BRACKET, T_DOUBLE_ARROW ) ) ) {
			if ( $token['code'] == T_CLOSE_SQUARE_BRACKET ) {
				$operator = $phpcsFile->findNext( array( T_EQUAL ), $stackPtr + 1 );
			} elseif ( $token['code'] == T_DOUBLE_ARROW ) {
				$operator = $stackPtr;
			}
			$keyIdx = $phpcsFile->findPrevious( array( T_WHITESPACE, T_CLOSE_SQUARE_BRACKET ), $operator - 1, null, true );
			$key = trim( $tokens[$keyIdx]['content'], '\'"' );
			$valIdx = $phpcsFile->findNext( T_WHITESPACE, $operator + 1, null, true );
			$val = trim( $tokens[$valIdx]['content'], '\'"' );
			$inst[ $key ][] = array( $val, $token['line'] );
		}
		// $foo = 'bar=taz&other=thing';
		elseif ( in_array( $token['code'], array( T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_QUOTED_STRING ) ) ) {
			if ( preg_match_all( '#[\'"&]([a-z_]+)=([^\'"&]*)#i', $token['content'], $matches ) <= 0 ) {
				return; // No assignments here, nothing to check
			}
			foreach ( $matches[1] as $i => $_k ) {
				$inst[ $_k ][] = array( $matches[2][$i], $token['line'] );
			}
		}

		if ( empty( $inst ) ) {
			return;
		}


		foreach ( $groups as $groupName => $group ) {
			
			if ( in_array( $groupName, $exclude ) ) {
				continue;
			}
<commit_msg>Fix assigned value extraction<commit_after><?php
/**
 * Restricts array assignment of certain keys
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @author   <NAME> <<EMAIL>>
 */
class WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff implements PHP_CodeSniffer_Sniff
{

	/**
	 * Exclude groups
	 *
	 * Example: 'foo,bar'
	 * 
	 * @var string Comma-delimited group list 
	 */
	public $exclude = '';

	/**
	 * Groups of variable data to check against.
	 * Don't use this in extended classes, override getGroups() instead.
	 * This is only used for Unit tests.
	 * 
	 * @var array
	 */
	public static $groups = array();


	/**
	 * Returns an array of tokens this test wants to listen for.
	 *
	 * @return array
	 */
	public function register()
	{
		return array(
				T_DOUBLE_ARROW,
				T_CLOSE_SQUARE_BRACKET,
				T_CONSTANT_ENCAPSED_STRING,
				T_DOUBLE_QUOTED_STRING,
			   );

	}//end register()

	/**
	 * Groups of variables to restrict
	 * This should be overridden in extending classes.
	 *
	 * Example: groups => array(
	 * 	'wpdb' => array(
	 * 		'type' => 'error' | 'warning',
	 * 		'message' => 'Dont use this one please!',
	 * 		'variables' => array( '$val', '$var' ),
	 * 		'object_vars' => array( '$foo->bar', .. ),
	 * 		'array_members' => array( '$foo['bar']', .. ),
	 * 	)
	 * )
	 *
	 * @return array
	 */
	public function getGroups() {
		return self::$groups;
	}


	/**
	 * Processes this test, when one of its tokens is encountered.
	 *
	 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
	 * @param int                  $stackPtr  The position of the current token
	 *                                        in the stack passed in $tokens.
	 *
	 * @return void
	 */
	public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr )
	{
		$tokens = $phpcsFile->getTokens();

		$token = $tokens[$stackPtr];

		$exclude = explode( ',', $this->exclude );

		$groups = $this->getGroups();

		if ( empty( $groups ) ) {
			return;
		}

		if ( in_array( $token['code'], array( T_CLOSE_SQUARE_BRACKET ) ) ) {
			$equal = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true );
			if ( $tokens[$equal]['code'] !== T_EQUAL ) {
				return; // This is not an assignment!
			}
		}

		// Instances: Multi-dimentional array, keyed by line
		$inst = array();


		// $foo = array( 'bar' => 'taz' );
		// $foo['bar'] = $taz;
		if ( in_array( $token['code'], array( T_CLOSE_SQUARE_BRACKET, T_DOUBLE_ARROW ) ) ) {
			if ( $token['code'] == T_CLOSE_SQUARE_BRACKET ) {
				$operator = $phpcsFile->findNext( array( T_EQUAL ), $stackPtr + 1 );
			} elseif ( $token['code'] == T_DOUBLE_ARROW ) {
				$operator = $stackPtr;
			}
			$keyIdx = $phpcsFile->findPrevious( array( T_WHITESPACE, T_CLOSE_SQUARE_BRACKET ), $operator - 1, null, true );
			$key = trim( $tokens[$keyIdx]['content'], '\'"' );
			$valStart = $phpcsFile->findNext( array( T_WHITESPACE ), $operator + 1, null, true );
			$valEnd = $phpcsFile->findNext( array( T_COMMA, T_SEMICOLON ), $valStart + 1, null, false, null, true );
			$val = $phpcsFile->getTokensAsString( $valStart, $valEnd - $valStart );
			$val = trim( $val, '\'"' );
			$inst[ $key ][] = array( $val, $token['line'] );
		}
		// $foo = 'bar=taz&other=thing';
		elseif ( in_array( $token['code'], array( T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_QUOTED_STRING ) ) ) {
			if ( preg_match_all( '#[\'"&]([a-z_]+)=([^\'"&]*)#i', $token['content'], $matches ) <= 0 ) {
				return; // No assignments here, nothing to check
			}
			foreach ( $matches[1] as $i => $_k ) {
				$inst[ $_k ][] = array( $matches[2][$i], $token['line'] );
			}
		}

		if ( empty( $inst ) ) {
			return;
		}


		foreach ( $groups as $groupName => $group ) {
			
			if ( in_array( $groupName, $exclude ) ) {
				continue;
			}

{'fraction_non_alphanumeric': 0.17220172201722017, 'fraction_numerical': 0.002050020500205002, 'mean_word_length': 3.043093922651934, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3864756', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2570, 'n_words': 630}