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

Recurse::runPass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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
use s9e\RegexpBuilder\Runner;
11
12
/**
13
* Enables passes to be run recursively into alternations to replace a(?:x0|x1|y0|y1) with a[xy][01]
14
*/
15
class Recurse extends AbstractPass
16
{
17
	/**
18
	* @var Runner
19
	*/
20
	protected $runner;
21
22
	/**
23
	* @param Runner $runner
24
	*/
25 1
	public function __construct(Runner $runner)
26
	{
27 1
		$this->runner = $runner;
28 1
	}
29
30
	/**
31
	* {@inheritdoc}
32
	*/
33 1
	protected function runPass(array $strings)
34
	{
35 1
		return array_map([$this, 'recurseString'], $strings);
36
	}
37
38
	/**
39
	* Recurse into given string and run all passes on each element
40
	*
41
	* @param  array $string
42
	* @return array
43
	*/
44 1
	protected function recurseString(array $string)
45
	{
46 1
		$isOptional = $this->isOptional;
47 1
		foreach ($string as $k => $element)
48
		{
49 1
			if (is_array($element))
50
			{
51 1
				$string[$k] = $this->runner->run($element);
52
			}
53
		}
54 1
		$this->isOptional = $isOptional;
55
56 1
		return $string;
57
	}
58
}