Completed
Push — master ( 390f8f...6f977d )
by Josh
02:25
created

MergeSuffix   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 58
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processStrings() 0 21 4
A hasMatchingSuffix() 0 13 3
A isEligible() 0 4 2
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 MergeSuffix extends AbstractPass
11
{
12
	/**
13
	* {@inheritdoc}
14
	*/
15 8
	protected function processStrings(array $strings)
16
	{
17 8
		if (!$this->isEligible($strings))
18
		{
19 3
			return $strings;
20
		}
21
22 5
		$newString = [];
23 5
		while ($this->hasMatchingSuffix($strings))
24
		{
25 5
			array_unshift($newString, end($strings[0]));
26 5
			$i = count($strings);
27 5
			while (--$i >= 0)
28
			{
29 5
				array_pop($strings[$i]);
30
			}
31
		}
32 5
		array_unshift($newString, $strings);
33
34 5
		return [$newString];
35
	}
36
37
	/**
38
	* Test whether all given strings have the same last element
39
	*
40
	* @param  array[] $strings
41
	* @return bool
42
	*/
43 7
	protected function hasMatchingSuffix(array $strings)
44
	{
45 7
		$suffix = end($strings[1]);
46 7
		foreach ($strings as $string)
47
		{
48 7
			if (end($string) !== $suffix)
49
			{
50 7
				return false;
51
			}
52
		}
53
54 5
		return ($suffix !== false);
55
	}
56
57
	/**
58
	* Test whether this pass can be run on given list of strings
59
	*
60
	* @param  array[] $strings
61
	* @return bool
62
	*/
63 8
	protected function isEligible(array $strings)
64
	{
65 8
		return (count($strings) > 1 && $this->hasMatchingSuffix($strings));
66
	}
67
}