PromoteSingleStrings::runPass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\RegexpBuilder
5
* @copyright Copyright (c) 2016-2022 The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\RegexpBuilder\Passes;
9
10
/**
11
* Replaces alternations that only contain one string to allow other passes to replace
12
* (?:a0?x|bx) with (?:a0?|b)x
13
*/
14
class PromoteSingleStrings extends AbstractPass
15
{
16
	/**
17
	* {@inheritdoc}
18
	*/
19 4
	protected function runPass(array $strings): array
20
	{
21 4
		return array_map([$this, 'promoteSingleStrings'], $strings);
22
	}
23
24
	/**
25
	* Promote single strings found inside given string
26
	*
27
	* @param  array $string Original string
28
	* @return array         Modified string
29
	*/
30 3
	protected function promoteSingleStrings(array $string): array
31
	{
32 3
		$newString = [];
33 3
		foreach ($string as $element)
34
		{
35 3
			if (is_array($element) && count($element) === 1)
36
			{
37 2
				$newString = array_merge($newString, $element[0]);
38
			}
39
			else
40
			{
41 2
				$newString[] = $element;
42
			}
43
		}
44
45 3
		return $newString;
46
	}
47
}