Failed Conditions
Push — master ( dd969d...42da22 )
by Alexander
10:30
created

PathCollisionDetector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 22
c 1
b 0
f 0
dl 0
loc 72
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isCollision() 0 9 3
A addPaths() 0 22 6
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Repository\RevisionLog;
12
13
14
class PathCollisionDetector
15
{
16
17
	/**
18
	 * Known paths.
19
	 *
20
	 * @var array
21
	 */
22
	private $_knownPaths = array();
23
24
	/**
25
	 * Expanded array of paths.
26
	 *
27
	 * @var array
28
	 */
29
	private $_expandedPaths = array('/' => 1);
30
31
	/**
32
	 * Longest path length.
33
	 *
34
	 * @var integer
35
	 */
36
	private $_longestPathLength = 1;
37
38
	/**
39
	 * Adds paths.
40
	 *
41
	 * @param array $paths Paths.
42
	 *
43
	 * @return void
44
	 */
45 53
	public function addPaths(array $paths)
46
	{
47 53
		foreach ( $paths as $path ) {
48 14
			$parent_path = rtrim($path, '/');
49 14
			$this->_knownPaths[$path] = true;
50
51
			do {
52 14
				$this->_expandedPaths[$parent_path . '/'] = strlen($parent_path . '/');
53 14
				$parent_path = dirname($parent_path);
54 14
			} while ( $parent_path !== '/' && $parent_path !== '' );
55
		}
56
57 53
		$path_count = count($this->_expandedPaths);
58
59 53
		if ( $path_count > 1 ) {
60 12
			$this->_longestPathLength = call_user_func_array('max', \array_values($this->_expandedPaths));
61
		}
62 48
		elseif ( $path_count === 1 ) {
63 48
			$this->_longestPathLength = current($this->_expandedPaths);
64
		}
65
		else {
66
			$this->_longestPathLength = 0; // @codeCoverageIgnore
67
		}
68 53
	}
69
70
	/**
71
	 * Checks path collision.
72
	 *
73
	 * @param string $path Path.
74
	 *
75
	 * @return boolean
76
	 */
77 16
	public function isCollision($path)
78
	{
79 16
		if ( isset($this->_knownPaths[$path]) || !$this->_knownPaths ) {
80 10
			return false;
81
		}
82
83 8
		$path = substr($path, 0, $this->_longestPathLength);
84
85 8
		return isset($this->_expandedPaths[$path]);
86
	}
87
88
}
89