mageekguy\atoum\tools\diff: lines coverage

92% of 348

OPs

100% of 67

Lines

81% of 54

Branches

29% of 55

Paths
Method OPs OPs % Lines Line % Branches Branches % Paths Path %
mageekguy\atoum\tools\diff::__construct() 32 100% 10 100% 7 86% 4 75%
mageekguy\atoum\tools\diff::__invoke() 14 100% 2 100% 1 100% 1 100%
mageekguy\atoum\tools\diff::__toString() 11 100% 1 100% 1 100% 1 100%
mageekguy\atoum\tools\diff::setDecorator() 16 100% 3 100% 1 100% 1 100%
mageekguy\atoum\tools\diff::getDecorator() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\tools\diff::setExpected() 14 100% 3 100% 1 100% 1 100%
mageekguy\atoum\tools\diff::getExpected() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\tools\diff::setActual() 14 100% 3 100% 1 100% 1 100%
mageekguy\atoum\tools\diff::getActual() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\tools\diff::make() 59 100% 13 100% 10 100% 8 38%
mageekguy\atoum\tools\diff::diff() 157 82% 28 100% 28 68% 34 3%
mageekguy\atoum\tools\diff::split() 13 100% 1 100% 1 100% 1 100%
#
1
<?php
2

                    
3
namespace mageekguy\atoum\tools;
4

                    
5
class diff
6
{
7
	protected $expected = null;
8
	protected $actual = null;
9
	protected $diff = null;
10
	protected $decorator = null;
11

                    
12
	public function __construct($expected = null, $actual = null)100%
13
	{
14
		$this->setDecorator();
15

                    
16
		if ($expected !== null)
17
		{
18
			$this->setExpected($expected);
19
		}
20

                    
21
		if ($actual !== null)
22
		{
23
			$this->setActual($actual);
24
		}
25
	}
26

                    
27
	public function __invoke($expected = null, $actual = null)100%
28
	{
29
		$this->make($expected, $actual);
30

                    
31
		return $this;
32
	}
33

                    
34
	public function __toString()100%
35
	{
36
		return $this->decorator->decorate($this);
37
	}
38

                    
39
	public function setDecorator(diff\decorator $decorator = null)100%
40
	{
41
		$this->decorator = $decorator ?: new diff\decorator();
42
		return $this;
43
	}
44

                    
45
	public function getDecorator()100%
46
	{
47
		return $this->decorator;
48
	}
49

                    
50
	public function setExpected($mixed)100%
51
	{
52
		$this->expected = (string) $mixed;
53
		$this->diff = null;
54

                    
55
		return $this;
56
	}
57

                    
58
	public function getExpected()100%
59
	{
60
		return $this->expected;
61
	}
62

                    
63
	public function setActual($mixed)100%
64
	{
65
		$this->actual = (string) $mixed;
66
		$this->diff = null;
67

                    
68
		return $this;
69
	}
70

                    
71
	public function getActual()100%
72
	{
73
		return $this->actual;
74
	}
75

                    
76
	public function make($expected = null, $actual = null)100%
77
	{
78
		if ($expected !== null)
79
		{
80
			$this->setExpected($expected);
81
		}
82

                    
83
		if ($expected !== null)
84
		{
85
			$this->setActual($actual);
86
		}
87

                    
88
		if ($this->diff === null)
89
		{
90
			$this->diff = $this->diff(self::split($this->expected), self::split($this->actual));
91
		}
92

                    
93
		return $this->diff;
94
	}
95

                    
96
	protected function diff($old, $new)100%
97
	{
98
		$diff = array();
99

                    
100
		if (sizeof($old) > 0 || sizeof($new) > 0)
101
		{
102
			$lengths = array();
103
			$maxLength = 0;
104

                    
105
			foreach ($old as $oldKey => $oldValue)
106
			{
107
				$newKeys = array_keys($new, $oldValue);
108

                    
109
				foreach ($newKeys as $newKey)
110
				{
111
					$lengths[$oldKey][$newKey] = isset($lengths[$oldKey - 1][$newKey - 1]) === false ? 1 : $lengths[$oldKey - 1][$newKey - 1] + 1;
112

                    
113
					if ($lengths[$oldKey][$newKey] > $maxLength)
114
					{
115
						$maxLength = $lengths[$oldKey][$newKey];
116
						$oldMaxLength = $oldKey + 1 - $maxLength;
117
						$newMaxLength = $newKey + 1 - $maxLength;
118
					}
119
				}
120
			}
121

                    
122
			if ($maxLength == 0)
123
			{
124
				$diff = array(array('-' => $old, '+' => $new));
125
			}
126
			else
127
			{
128
				$diff = array_merge(
129
					$this->diff(array_slice($old, 0, $oldMaxLength), array_slice($new, 0, $newMaxLength)),
130
					array_slice($new, $newMaxLength, $maxLength),
131
					$this->diff(array_slice($old, $oldMaxLength + $maxLength), array_slice($new, $newMaxLength + $maxLength))
132
				);
133
			}
134
		}
135

                    
136
		return $diff;
137
	}
138

                    
139
	protected static function split($value)100%
140
	{
141
		return explode(PHP_EOL, $value);
142
	}
143
}