Completed
Push — master ( d1c60d...2373c3 )
by Josh
03:29
created

AbstractPass::afterRun()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
cc 3
eloc 4
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
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
		$strings = $this->processStrings($strings);
24 27
		$strings = $this->afterRun($strings);
25
26 27
		return $strings;
27
	}
28
29
	/**
30
	* Process the list of strings after the pass is run
31
	*
32
	* @param  array[] $strings
33
	* @return array[]
34
	*/
35 27
	protected function afterRun(array $strings)
36
	{
37 27
		if ($this->isOptional && $strings[0] !== [])
38
		{
39 2
			array_unshift($strings, []);
40
		}
41
42 27
		return $strings;
43
	}
44
45
	/**
46
	* Prepare the list of strings before the pass is run
47
	*
48
	* @param  array[] $strings
49
	* @return array[]
50
	*/
51 27
	protected function beforeRun(array $strings)
52
	{
53 27
		$this->isOptional = (isset($strings[0]) && $strings[0] === []);
54 27
		if ($this->isOptional)
55
		{
56 2
			array_shift($strings);
57
		}
58
59 27
		return $strings;
60
	}
61
62
	/**
63
	* Process a given list of strings
64
	*
65
	* @param  array[] $strings
66
	* @return array[]
67
	*/
68
	abstract protected function processStrings(array $strings);
69
}