TSNode   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 12
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
1
<?php
2
3
namespace ParamProcessor;
4
5
/**
6
 * Sorts a series of dependency pairs in linear order.
7
 *
8
 * Based on http://blog.metafoundry.com/2007/09/topological-sort-in-php.html
9
 *
10
 * @deprecated since 1.7
11
 * @codingStandardsIgnoreFile
12
 */
13
class TopologicalSort {
14
15
	private $mNodes = [];
16
	private $mNodeNames = [];
17
18
	/**
19
	 * Dependency pairs are a list of arrays in the form
20
	 * $name => $val where $key must come before $val in load order.
21
	 */
22 49
	public function __construct( array $dependencies = [], $parse = true ) {
23 49
		$this->mNodeNames = array_keys( $dependencies );
24
25 49
		if ( $parse ) {
26 49
			$dependencies = $this->parseDependencyList( $dependencies );
27
		}
28
29
		// turn pairs into double-linked node tree
30 49
		foreach ( $dependencies as $dpair ) {
31
			$module = key( $dpair );
32
			$dependency = current( $dpair );
33
			if ( !isset( $this->mNodes[$module] ) ) $this->mNodes[$module] = new TSNode( $module );
0 ignored issues
show
Deprecated Code introduced by
The class ParamProcessor\TSNode has been deprecated with message: since 1.7

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
34
			if ( !isset( $this->mNodes[$dependency] ) ) $this->mNodes[$dependency] = new TSNode( $dependency );
0 ignored issues
show
Deprecated Code introduced by
The class ParamProcessor\TSNode has been deprecated with message: since 1.7

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
35
			if ( !in_array( $dependency, $this->mNodes[$module]->children ) ) $this->mNodes[$module]->children[] = $dependency;
36
			if ( !in_array( $module, $this->mNodes[$dependency]->parents ) ) $this->mNodes[$dependency]->parents[] = $module;
37
		}
38 49
	}
39
40
	/**
41
	 * Perform Topological Sort.
42
	 *
43
	 * @return array Sorted array
44
	 */
45 49
	public function doSort() {
46 49
		$nodes = $this->mNodes;
47
48
		// get nodes without parents
49 49
		$root_nodes = array_values( $this->getRootNodes( $nodes ) );
50
51
		// begin algorithm
52 49
		$sorted = [];
53 49
		while ( count( $nodes ) > 0 ) {
54
			// check for circular reference
55
			if ( $root_nodes === [] ) {
56
				return [];
57
			}
58
59
60
			// remove this node from root_nodes
61
			// and add it to the output
62
			$n = array_pop( $root_nodes );
63
			$sorted[] = $n->name;
64
65
			// for each of its  children
66
			// queue the new node finally remove the original
67
			for ( $i = count( $n->children ) - 1; $i >= 0; $i -- ) {
68
				$childnode = $n->children[$i];
69
				// remove the link from this node to its
70
				// children ($nodes[$n->name]->children[$i]) AND
71
				// remove the link from each child to this
72
				// parent ($nodes[$childnode]->parents[?]) THEN
73
				// remove this child from this node
74
				unset( $nodes[$n->name]->children[$i] );
75
				$parent_position = array_search ( $n->name, $nodes[$childnode]->parents );
76
				unset( $nodes[$childnode]->parents[$parent_position] );
77
				// check if this child has other parents
78
				// if not, add it to the root nodes list
79
				if ( !count( $nodes[$childnode]->parents ) ) {
80
					array_push( $root_nodes, $nodes [$childnode] );
81
				}
82
			}
83
84
			// nodes.Remove(n);
85
			unset( $nodes[$n->name] );
86
		}
87
88 49
		$looseNodes = [];
89
90
		// Return the result with the loose nodes (items with no dependencies) appended.
91 49
		foreach( $this->mNodeNames as $name ) {
92 49
			if ( !in_array( $name, $sorted ) ) {
93 49
				$looseNodes[] = $name;
94
			}
95
		}
96
97 49
		return array_merge( $sorted, $looseNodes );
98
	}
99
100
	/**
101
	 * Returns a list of node objects that do not have parents
102
	 *
103
	 * @param array $nodes array of node objects
104
	 *
105
	 * @return array of node objects
106
	 */
107 49
	private function getRootNodes( array $nodes ) {
108 49
		$output =  [];
109
110 49
		foreach ( $nodes as $name => $node ) {
111
			if ( !count ( $node->parents ) ) {
112
				$output[$name] = $node;
113
			}
114
		}
115
116 49
		return $output;
117
	}
118
119
	/**
120
	 * Parses an array of dependencies into an array of dependency pairs
121
	 *
122
	 * The array of dependencies would be in the form:
123
	 * $dependency_list = array(
124
	 *  "name" => array("dependency1","dependency2","dependency3"),
125
	 *  "name2" => array("dependencyA","dependencyB","dependencyC"),
126
	 *  ...etc
127
	 * );
128
	 *
129
	 * @param array $dlist Array of dependency pairs for use as parameter in doSort method
130
	 *
131
	 * @return array
132
	 */
133 49
	private function parseDependencyList( array $dlist = [] ) {
134 49
		$output = [];
135
136 49
		foreach ( $dlist as $name => $dependencies ) {
137 49
			foreach ( $dependencies as $d ) {
138
				$output[] = [ $d => $name ];
139
			}
140
		}
141
142 49
		return $output;
143
	}
144
}
145
146
/**
147
 * @deprecated since 1.7
148
 */
149
class TSNode {
150
	public $name;
151
	public $children = [];
152
	public $parents = [];
153
154
	public function __construct( $name = '' ) {
155
		if ( !is_string( $name ) ) {
156
			throw new \InvalidArgumentException( 'Name needs to be a string' );
157
		}
158
		$this->name = $name;
159
	}
160
}