Completed
Push — master ( 0ac2b6...073708 )
by Josh
02:05
created

GroupSingleCharacters::runPass()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 1
crap 3
1
<?php
2
3
/**
4
* @package   s9e\RegexpBuilder
5
* @copyright Copyright (c) 2016 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
* Enables other passes to replace (?:[xy]|a[xy]) with a?[xy]
12
*/
13
class GroupSingleCharacters extends AbstractPass
14
{
15
	/**
16
	* {@inheritdoc}
17
	*/
18 4
	protected function runPass(array $strings)
19
	{
20 4
		$singles = $this->getSingleCharStrings($strings);
21 4
		$cnt     = count($singles);
22 4
		if ($cnt > 1 && $cnt < count($strings))
23
		{
24
			// Remove the singles from the input, then prepend them as a new string
25 1
			$strings = array_diff_key($strings, $singles);
26 1
			array_unshift($strings, [array_values($singles)]);
27
		}
28
29 4
		return $strings;
30
	}
31
32
	/**
33
	* Return an array of every single-char string in given list of strings
34
	*
35
	* @param  array[] $strings
36
	* @return array[]
37
	*/
38 4 View Code Duplication
	protected function getSingleCharStrings(array $strings)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
	{
40 4
		$singles = [];
41 4
		foreach ($strings as $k => $string)
42
		{
43 4
			if (count($string) === 1 && !is_array($string[0]))
44
			{
45 4
				$singles[$k] = $string;
46
			}
47
		}
48
49 4
		return $singles;
50
	}
51
}