forked from phpmetrics/PhpMetrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackageSieve.php
51 lines (43 loc) · 1.21 KB
/
PackageSieve.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace Hal\Metric\Package;
class PackageSieve
{
public function __construct(
private readonly int $packageLevelLimit = 0,
private readonly array $ignorePrefixes = [],
)
{
}
public function getPackageFromClassName(string $className): string
{
if (strpos($className, '\\') === false) {
return '\\';
}
$parts = explode('\\', $className);
array_pop($parts);
if ($this->packageLevelLimit > 0) {
$parts = array_slice($parts, 0, $this->packageLevelLimit);
}
return implode('\\', $parts) . '\\';
}
public function getPackageFromNamespace(string $namespace): string
{
$package = $namespace;
if ($this->packageLevelLimit > 1) {
$package = implode('\\', array_slice(
explode('\\', $namespace),
0, $this->packageLevelLimit
));
}
return $package . '\\';
}
public function excludePackage(string $namespace): bool
{
foreach ($this->ignorePrefixes as $prefix) {
if (str_starts_with($namespace, $prefix)) {
return true;
}
}
return false;
}
}