GroupSingleCharacters::runPass()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 3
rs 10
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 (?:aa|b|cc|d) with (?:[bd]|aa|cc)
12
* Enables other passes to replace (?:[xy]|a[xy]) with a?[xy]
13
*/
14
class GroupSingleCharacters extends AbstractPass
15
{
16
	/**
17
	* {@inheritdoc}
18 4
	*/
19
	protected function runPass(array $strings): array
20 4
	{
21 4
		$singles = $this->getSingleCharStrings($strings);
22 4
		$cnt     = count($singles);
23
		if ($cnt > 1 && $cnt < count($strings))
24
		{
25 1
			// Remove the singles from the input, then prepend them as a new string
26 1
			$strings = array_diff_key($strings, $singles);
27
			array_unshift($strings, [array_values($singles)]);
28
		}
29 4
30
		return $strings;
31
	}
32
33
	/**
34
	* Return an array of every single-char string in given list of strings
35
	*
36
	* @param  array[] $strings
37
	* @return array[]
38 4
	*/
39
	protected function getSingleCharStrings(array $strings): array
40 4
	{
41
		return array_filter($strings, [$this, 'isSingleCharString']);
42
	}
43
}