<commit_before><?php

namespace s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP;

use s9e\TextFormatter\Configurator\RendererGenerators\PHP\Optimizer;
use s9e\TextFormatter\Tests\Test;

/**
* @requires extension tokenizer
* @covers s9e\TextFormatter\Configurator\RendererGenerators\PHP\Optimizer
*/
class OptimizerTest extends Test
{
	/**
	* @dataProvider getOptimizationTests
	* @testdox optimize() tests
	*/
	public function testOptimizations($original, $expected)
	{
		$optimizer = new Optimizer;

		$this->assertSame($expected, $optimizer->optimize($original));
	}

	public function getOptimizationTests()
	{
		return [
			[
				"\$this->out.='<br';\$this->out.='>';",
				"\$this->out.='<br>';"
			],
			[
				"\$this->out.='a';\$this->out.='b';\$this->out.='c';",
				"\$this->out.='abc';"
			],
			[
				"\$foo.='foo';\$bar.='foo';\$this->out.='b';",
				"\$foo.='foo';\$bar.='foo';\$this->out.='b';",
			],
			[
				"\$this->out.='foo';if(1){}",
				"\$this->out.='foo';if(1){}"
			],
			[
				"\$this->out.='foo'.'bar';",
				"\$this->out.='foobar';"
			],
			[
				"\$this->out.='foo'.\"bar\";",
				"\$this->out.='foo'.\"bar\";"
			],
			[
				"htmlspecialchars(\$node->getAttribute('foo'),2).htmlspecialchars(\$node->getAttribute('bar'),2)",
				"htmlspecialchars(\$node->getAttribute('foo').\$node->getAttribute('bar'),2)"
			],
			[
				"htmlspecialchars(\$node->getAttribute('foo'),1).htmlspecialchars(\$node->getAttribute('bar'),2)",
				"htmlspecialchars(\$node->getAttribute('foo'),1).htmlspecialchars(\$node->getAttribute('bar'),2)"
			],
			[
				"htmlspecialchars(\$node->getAttribute('foo'),2).'bar'",
				"htmlspecialchars(\$node->getAttribute('foo'),2).'bar'"
			],
			[
				"\$this->out.=htmlspecialchars('<>\"',2);",
				"\$this->out.='&lt;&gt;&quot;';"
			],
			[
				"\$this->out.=htmlspecialchars('<>\"',0);",
				"\$this->out.='&lt;&gt;\"';"
			],
			[
				'$this->out.=htmlspecialchars($node->localName,0);',
				'$this->out.=$node->localName;',
			],
			[
				'$this->out.=htmlspecialchars($node->nodeName,0);',
				'$this->out.=$node->nodeName;',
			],
		];
	}

	/**
	* @dataProvider getControlStructureOptimizationTests
	* @testdox optimize() tests
	*/
	public function testControlStructureOptimization($original, $expected)
	{
		$optimizer = new Optimizer;

		$this->assertSame($expected, $optimizer->optimizeControlStructures($original));
	}

	public function getControlStructureOptimizationTests()
	{
		return [
			[
				'if ($foo) { bar(); }',
				'if ($foo) bar();'
			],
			[
				'if(($foo)){bar();}',
				'if(($foo))bar();'
			],
			[
				'if ($foo) { bar(); } else { baz(); }',
				'if ($foo) bar(); else baz();'
			],
			[
				'if($foo){bar();}else{baz();}',
				'if($foo)bar();else baz();'
			],
			[
				'if($foo){bar();}else{$baz=1;}',
				'if($foo)bar();else$baz=1;'
			],
			[
				'if ($foo) { bar(); baz(); }',
				'if ($foo) { bar(); baz(); }'
			],
			[
				'while (--$i) { }',
				'while (--$i);'
			],
			[<commit_msg>Added tests<commit_after><?php

namespace s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP;

use s9e\TextFormatter\Configurator\RendererGenerators\PHP\Optimizer;
use s9e\TextFormatter\Tests\Test;

/**
* @requires extension tokenizer
* @covers s9e\TextFormatter\Configurator\RendererGenerators\PHP\Optimizer
*/
class OptimizerTest extends Test
{
	/**
	* @dataProvider getOptimizationTests
	* @testdox optimize() tests
	*/
	public function testOptimizations($original, $expected)
	{
		$optimizer = new Optimizer;

		$this->assertSame($expected, $optimizer->optimize($original));
	}

	public function getOptimizationTests()
	{
		return [
			[
				"\$this->out.='<br';\$this->out.='>';",
				"\$this->out.='<br>';"
			],
			[
				"\$this->out.='a';\$this->out.='b';\$this->out.='c';",
				"\$this->out.='abc';"
			],
			[
				"\$foo.='foo';\$bar.='foo';\$this->out.='b';",
				"\$foo.='foo';\$bar.='foo';\$this->out.='b';",
			],
			[
				"\$this->out.='foo';if(1){}",
				"\$this->out.='foo';if(1){}"
			],
			[
				"\$this->out.='foo'.'bar';",
				"\$this->out.='foobar';"
			],
			[
				"\$this->out.='foo'.\"bar\";",
				"\$this->out.='foo'.\"bar\";"
			],
			[
				"htmlspecialchars(\$node->getAttribute('foo'),2).htmlspecialchars(\$node->getAttribute('bar'),2)",
				"htmlspecialchars(\$node->getAttribute('foo').\$node->getAttribute('bar'),2)"
			],
			[
				"htmlspecialchars(\$node->getAttribute('foo'),1).htmlspecialchars(\$node->getAttribute('bar'),2)",
				"htmlspecialchars(\$node->getAttribute('foo'),1).htmlspecialchars(\$node->getAttribute('bar'),2)"
			],
			[
				"htmlspecialchars(\$node->getAttribute('foo'),2).'bar'",
				"htmlspecialchars(\$node->getAttribute('foo'),2).'bar'"
			],
			[
				"\$this->out.=htmlspecialchars('<>\"',2);",
				"\$this->out.='&lt;&gt;&quot;';"
			],
			[
				"\$this->out.=htmlspecialchars('<>\"',0);",
				"\$this->out.='&lt;&gt;\"';"
			],
			[
				'$this->out.=htmlspecialchars($node->localName,0);',
				'$this->out.=$node->localName;',
			],
			[
				'$this->out.=htmlspecialchars($node->nodeName,0);',
				'$this->out.=$node->nodeName;',
			],
		];
	}

	/**
	* @dataProvider getControlStructureOptimizationTests
	* @testdox optimize() tests
	*/
	public function testControlStructureOptimization($original, $expected)
	{
		$optimizer = new Optimizer;

		$this->assertSame($expected, $optimizer->optimizeControlStructures($original));
	}

	public function getControlStructureOptimizationTests()
	{
		return [
			[
				'if ($foo) { bar(); }',
				'if ($foo) bar();'
			],
			[
				'if(($foo)){bar();}',
				'if(($foo))bar();'
			],
			[
				'if ($foo) { bar(); } else { baz(); }',
				'if ($foo) bar(); else baz();'
			],
			[
				'if($foo){bar();}else{ baz();}',
				'if($foo)bar();else baz();'
			],
			[
				'if($foo){bar();}else {baz();}',
				'if($foo)bar();else baz();'
			],
			[
				'if($foo){bar();}else{baz();}',
				'if($foo)bar();else baz();'
			],
			[
				'if($foo){bar();}else{$baz=1;}',
				'if($foo)bar();else$baz=1;'
			],
			[
				'if ($foo) { bar(); baz(); }',
				'if ($foo) { bar(); baz(); }'
			],
			[
				'while (--$i) { }',
				'while (--$i);'
			],
			[
{'fraction_non_alphanumeric': 0.27857609405617245, 'fraction_numerical': 0.006858262573481384, 'mean_word_length': 3.5036764705882355, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 93, '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': '7318987', 'n_tokens_mistral': 2811, 'n_tokens_neox': 2344, 'n_words': 235}