Completed
Branch master (073708)
by Josh
01:59
created

AbstractPass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 11 2
A afterRun() 0 9 3
A beforeRun() 0 10 3
A canRun() 0 4 1
runPass() 0 1 ?
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
abstract class AbstractPass implements PassInterface
11
{
12
	/**
13
	* @var bool Whether the current set of strings is optional
14
	*/
15
	protected $isOptional;
16
17
	/**
18
	* {@inheritdoc}
19
	*/
20 27
	public function run(array $strings)
21
	{
22 27
		$strings = $this->beforeRun($strings);
23 27
		if ($this->canRun($strings))
24
		{
25 24
			$strings = $this->runPass($strings);
26
		}
27 27
		$strings = $this->afterRun($strings);
28
29 27
		return $strings;
30
	}
31
32
	/**
33
	* Process the list of strings after the pass is run
34
	*
35
	* @param  array[] $strings
36
	* @return array[]
37
	*/
38 27
	protected function afterRun(array $strings)
39
	{
40 27
		if ($this->isOptional && $strings[0] !== [])
41
		{
42 2
			array_unshift($strings, []);
43
		}
44
45 27
		return $strings;
46
	}
47
48
	/**
49
	* Prepare the list of strings before the pass is run
50
	*
51
	* @param  array[] $strings
52
	* @return array[]
53
	*/
54 27
	protected function beforeRun(array $strings)
55
	{
56 27
		$this->isOptional = (isset($strings[0]) && $strings[0] === []);
57 27
		if ($this->isOptional)
58
		{
59 2
			array_shift($strings);
60
		}
61
62 27
		return $strings;
63
	}
64
65
	/**
66
	* Test whether this pass can be run on a given list of strings
67
	*
68
	* @param  array[] $strings
69
	* @return bool
70
	*/
71 19
	protected function canRun(array $strings)
72
	{
73 19
		return true;
74
	}
75
76
	/**
77
	* Run this pass on a list of strings
78
	*
79
	* @param  array[] $strings
80
	* @return array[]
81
	*/
82
	abstract protected function runPass(array $strings);
83
}