<commit_before><?php

namespace WP_CLI\Commands;

class Shell_Command extends \WP_CLI_Command {

	/**
	 * Interactive PHP console.
	 */
	public function __invoke() {
		\WP_CLI::line( 'Type "exit" to close session.' );

		while ( true ) {
			$line = self::prompt();

			if ( '' === $line )
				continue;

			$line = rtrim( $line, ';' ) . ';';

			if ( self::starts_with( self::non_expressions(), $line ) ) {
				eval( $line );
			} else {
				if ( self::starts_with( 'return', $line ) )
					$line = substr( $line, strlen( 'return' ) );

				$line = '$_ = ' . $line;

				eval( $line );

				\WP_CLI::line( var_export( $_, false ) );
			}
		}
	}

	private static function non_expressions() {
		return implode( '|', array(
			'echo', 'global', 'unset',
			'while', 'for', 'foreach', 'if', 'switch',
			'include', 'include\_once', 'require', 'require\_once'
		) );
	}

	private static function prompt() {
		static $cmd;

		if ( !$cmd ) {
			$cmd = self::create_prompt_cmd( 'wp> ', self::get_history_path() );
		}

		$fp = popen( $cmd, 'r' );

		$line = fgets( $fp );

		if ( !$line ) {
			$line = 'exit';
		}

		return trim( $line );
	}

	private static function create_prompt_cmd( $prompt, $history_path ) {
		$cmd = implode( '; ', array(
			'set -f',
			sprintf( 'history -r %s', escapeshellarg( $history_path ) ),
			'LINE=""',
			sprintf( 'read -re -p %s LINE', escapeshellarg( $prompt ) ),
			'[ $? -eq 0 ] || exit',
			'history -s "$LINE"',
			sprintf( 'history -w %s', escapeshellarg( $history_path ) ),
			'echo $LINE'
		) );

		return '/bin/bash -c ' . escapeshellarg( $cmd );
	}

	private static function get_history_path() {
		$data = getcwd() . get_current_user();

		return sys_get_temp_dir() . '/wp-cli-history-' . md5( $data );
	}

	private static function starts_with( $tokens, $line ) {
		return preg_match( "/^($tokens)[\(\s]+/", $line );
	}
}

\WP_CLI::add_command( 'shell', new Shell_Command );
<commit_msg>make shell readline script more readable, using a heredoc<commit_after><?php

namespace WP_CLI\Commands;

class Shell_Command extends \WP_CLI_Command {

	/**
	 * Interactive PHP console.
	 */
	public function __invoke() {
		\WP_CLI::line( 'Type "exit" to close session.' );

		while ( true ) {
			$line = self::prompt();

			if ( '' === $line )
				continue;

			$line = rtrim( $line, ';' ) . ';';

			if ( self::starts_with( self::non_expressions(), $line ) ) {
				eval( $line );
			} else {
				if ( self::starts_with( 'return', $line ) )
					$line = substr( $line, strlen( 'return' ) );

				$line = '$_ = ' . $line;

				eval( $line );

				\WP_CLI::line( var_export( $_, false ) );
			}
		}
	}

	private static function non_expressions() {
		return implode( '|', array(
			'echo', 'global', 'unset',
			'while', 'for', 'foreach', 'if', 'switch',
			'include', 'include\_once', 'require', 'require\_once'
		) );
	}

	private static function prompt() {
		static $cmd;

		if ( !$cmd ) {
			$cmd = self::create_prompt_cmd( 'wp> ', self::get_history_path() );
		}

		$fp = popen( $cmd, 'r' );

		$line = fgets( $fp );

		if ( !$line ) {
			$line = 'exit';
		}

		return trim( $line );
	}

	private static function create_prompt_cmd( $prompt, $history_path ) {
		$prompt = escapeshellarg( $prompt );
		$history_path = escapeshellarg( $history_path );

		$cmd = <<<BASH
set -f
history -r $history_path
LINE=""
read -re -p $prompt LINE
[ $? -eq 0 ] || exit
history -s "\$LINE"
history -w $history_path
echo \$LINE
BASH;

		$cmd = str_replace( "\n", '; ', $cmd );

		return '/bin/bash -c ' . escapeshellarg( $cmd );
	}

	private static function get_history_path() {
		$data = getcwd() . get_current_user();

		return sys_get_temp_dir() . '/wp-cli-history-' . md5( $data );
	}

	private static function starts_with( $tokens, $line ) {
		return preg_match( "/^($tokens)[\(\s]+/", $line );
	}
}

\WP_CLI::add_command( 'shell', new Shell_Command );

{'fraction_non_alphanumeric': 0.19731057667442461, 'fraction_numerical': 0.001034393586759762, 'mean_word_length': 3.154672395273899, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, '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': '7354223', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1425, 'n_words': 345}