|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Hal\Component\Aggregator; |
|
11
|
|
|
use Hal\Component\Result\ResultCollection; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Agregates by directory |
|
15
|
|
|
* |
|
16
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
|
17
|
|
|
*/ |
|
18
|
|
|
class DirectoryRecursiveAggregator implements Aggregator { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Max depth |
|
22
|
|
|
* |
|
23
|
|
|
* @var int |
|
24
|
|
|
*/ |
|
25
|
|
|
private $depth; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor |
|
29
|
|
|
* |
|
30
|
|
|
* @param $depth |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($depth) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->depth = $depth; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
* @throws \RuntimeException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function aggregates(ResultCollection $results) { |
|
43
|
|
|
$array = new ResultCollection(); |
|
44
|
|
|
foreach($results as $result) { |
|
45
|
|
|
$basename = dirname($result->getFilename()); |
|
46
|
|
|
|
|
47
|
|
|
// from 'folder1/folder2/file.php', we want an array with ('folder1', 'folder2') |
|
48
|
|
|
$namespaces = explode(DIRECTORY_SEPARATOR, $basename); |
|
49
|
|
|
|
|
50
|
|
|
if($this->depth) { |
|
51
|
|
|
array_splice($namespaces, $this->depth); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// merge infos for each namespace in the DirectoryResultCollection |
|
55
|
|
|
$len = sizeof($namespaces, COUNT_NORMAL); |
|
56
|
|
|
|
|
57
|
|
|
for($i = 0; $i < $len; $i++) { |
|
58
|
|
|
|
|
59
|
|
|
$namespace = $namespaces[$i]; |
|
60
|
|
|
if(0 === strlen($namespace)) { |
|
61
|
|
|
$namespace = '.'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if(0 === $i) { |
|
65
|
|
|
// root |
|
66
|
|
|
if(!isset($array[$namespace])) { |
|
67
|
|
|
$array[$namespace] = new ResultCollection(); |
|
68
|
|
|
} |
|
69
|
|
|
$parent = &$array[$namespace]; |
|
70
|
|
|
} else { |
|
71
|
|
|
// namespace |
|
72
|
|
|
if(!isset($parent[$namespace])) { |
|
73
|
|
|
$parent[$namespace] = new ResultCollection(); // ResultRecursiveCollection -> has getOOP(), etc. |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
$parent = &$parent[$namespace]; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
$parent->push($result); |
|
79
|
|
|
} |
|
80
|
|
|
return $array; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: