CoalesceSingleCharacterPrefix::runPass()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 3
nop 1
crap 3
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 (?:ab|bb|c) with (?:[ab]b|c)
12
*/
13
class CoalesceSingleCharacterPrefix extends AbstractPass
14
{
15
	/**
16
	* {@inheritdoc}
17
	*/
18 5
	protected function runPass(array $strings): array
19
	{
20 5
		$newStrings = [];
21 5
		foreach ($this->getEligibleKeys($strings) as $keys)
22
		{
23
			// Create a new string to hold the merged strings and replace the first element with
24
			// an empty character class
25 2
			$newString    = $strings[$keys[0]];
26 2
			$newString[0] = [];
27
28
			// Fill the character class with the prefix of each string in this group before removing
29
			// the original string
30 2
			foreach ($keys as $key)
31
			{
32 2
				$newString[0][] = [$strings[$key][0]];
33 2
				unset($strings[$key]);
34
			}
35 2
			$newStrings[] = $newString;
36
		}
37
38 5
		return array_merge($newStrings, $strings);
39
	}
40
41
	/**
42
	* Filter the list of eligible keys and keep those that have at least two matches
43
	*
44
	* @param  array[] $eligibleKeys List of lists of keys
45
	* @return array[]
46
	*/
47 5
	protected function filterEligibleKeys(array $eligibleKeys): array
48
	{
49 5
		$filteredKeys = [];
50 5
		foreach ($eligibleKeys as $k => $keys)
51
		{
52 4
			if (count($keys) > 1)
53
			{
54 2
				$filteredKeys[] = $keys;
55
			}
56
		}
57
58 5
		return $filteredKeys;
59
	}
60
61
	/**
62
	* Get a list of keys of strings eligible to be merged together, grouped by suffix
63
	*
64
	* @param  array[] $strings
65
	* @return array[]
66
	*/
67 5
	protected function getEligibleKeys(array $strings): array
68
	{
69 5
		$eligibleKeys = [];
70 5
		foreach ($strings as $k => $string)
71
		{
72 5
			if (!is_array($string[0]) && isset($string[1]))
73
			{
74 4
				$suffix = serialize(array_slice($string, 1));
75 4
				$eligibleKeys[$suffix][] = $k;
76
			}
77
		}
78
79 5
		return $this->filterEligibleKeys($eligibleKeys);
80
	}
81
}