Completed
Push — master ( 95e794...553f1a )
by Josh
04:13
created

src/Passes/GroupSingleCharacters.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
class GroupSingleCharacters extends AbstractPass
11
{
12
	/**
13
	* {@inheritdoc}
14
	*/
15 4
	protected function processStrings(array $strings)
16
	{
17 4
		$singles = $this->getSingleCharStrings($strings);
18 4
		$cnt     = count($singles);
19 4
		if ($cnt > 1 && $cnt < count($strings))
20
		{
21
			// Remove the singles from the input, then prepend them as a new string
22 1
			$strings = array_diff_key($strings, $singles);
23 1
			array_unshift($strings, [array_values($singles)]);
24
		}
25
26 4
		return $strings;
27
	}
28
29
	/**
30
	* Return an array of every single-char string in given list of strings
31
	*
32
	* @param  array[] $strings
33
	* @return array[]
34
	*/
35 4 View Code Duplication
	protected function getSingleCharStrings(array $strings)
0 ignored issues
show
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...
36
	{
37 4
		$singles = [];
38 4
		foreach ($strings as $k => $string)
39
		{
40 4
			if (count($string) === 1 && !is_array($string[0]))
41
			{
42 4
				$singles[$k] = $string;
43
			}
44
		}
45
46 4
		return $singles;
47
	}
48
}