Recurse   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 2
b 0
f 0
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A runPass() 0 3 1
A __construct() 0 3 1
A recurseString() 0 13 3
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\RegexpBuilder
5
* @copyright Copyright (c) 2016-2022 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): array
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): array
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
}