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

GroupSingleCharacters   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 13
loc 39
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSingleCharStrings() 13 13 4
A runPass() 0 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}