Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit fad508a

Browse files
cs_matheus_feolablakeembrey
cs_matheus_feola
authored andcommitted
Adding transitive dependencies problem
1 parent 05fc3da commit fad508a

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Transitive Dependencies
2+
3+
A class that calculates how dependencies propagate between things such as classes in a program. One of the insidious things about dependencies is that they are transitive—if A depends on B and B depends on C, then A also depends on C. So, this code that calculates the full set of dependencies for a group of items. Example class for test:
4+
5+
```php
6+
7+
<?php
8+
9+
require_once 'transitive-dependencies.php';
10+
11+
class Dependencies_Test {
12+
13+
public $dependencies;
14+
15+
public function __construct() {
16+
$this->dependencies = new DependenciesList();
17+
}
18+
19+
public function add_direct_test() {
20+
$this->dependencies->add_direct('A', 'B,C');
21+
$this->dependencies->add_direct('B', 'C,E');
22+
$this->dependencies->add_direct('C', 'G');
23+
$this->dependencies->add_direct('D', 'A,F');
24+
$this->dependencies->add_direct('E', 'F');
25+
$this->dependencies->add_direct('F', 'H');
26+
}
27+
28+
}
29+
30+
$test = new Dependencies_Test();
31+
$test->add_direct_test();
32+
$resultado = $test->dependencies->dependencies_for("A");
33+
34+
//print dependencies array in screen
35+
var_dump($resultado);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
class DependenciesList
4+
{
5+
//where the dependencies values are stored
6+
public $dependencies_matrix;
7+
8+
public function __construct()
9+
{
10+
$this->dependencies_matrix = array();
11+
}
12+
13+
public function add_direct($index, $dependencies)
14+
{
15+
$this->dependencies_matrix[$index] = explode(",", $dependencies);
16+
}
17+
18+
public function dependencies_for($element)
19+
{
20+
//array where dependencies of the element parameter will be stored
21+
$dependents = array();
22+
//process get element dependencies
23+
$process = $this->dependencies_matrix[$element];
24+
25+
while (!empty($process)) {
26+
27+
//get an item from process
28+
$x = array_shift($process);
29+
30+
//if x is not element itself, and is not in dependents array
31+
if ($x != $element && !in_array($x, $dependents)) {
32+
33+
//put in dependents array
34+
$dependents[] = $x;
35+
36+
//if x exists in dependencies_matrix
37+
if (array_key_exists($x, $this->dependencies_matrix)) {
38+
39+
//break the next dependencies list in strings
40+
if (gettype($this->dependencies_matrix[$x]) == "array") {
41+
foreach ($this->dependencies_matrix[$x] as $key => $value) {
42+
array_push($process, $value);
43+
}
44+
}
45+
46+
else {
47+
//put the x line in process
48+
array_push($process, array_values($this->dependencies_matrix[$x]));
49+
}
50+
}
51+
}
52+
}
53+
return $dependents;
54+
}
55+
} //endclass

0 commit comments

Comments
 (0)