GroupSingleCharacters   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 28
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSingleCharStrings() 0 3 1
A runPass() 0 12 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 (?: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
}