-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathModelFunctions.php
More file actions
85 lines (73 loc) · 2.73 KB
/
Copy pathModelFunctions.php
File metadata and controls
85 lines (73 loc) · 2.73 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
namespace Qameta\Allure\Codeception\Internal;
use function array_filter;
use function array_shift;
use function explode;
use function getcwd;
use function is_string;
use function rtrim;
use const DIRECTORY_SEPARATOR;
/**
* @internal
*/
final class ModelFunctions
{
/**
* @return list<string>
*/
public static function getTitlePathByFile(string $base, string $path, bool $final = false): array
{
if (!$path) {
return [];
}
$baseParts = explode(DIRECTORY_SEPARATOR, rtrim($base, DIRECTORY_SEPARATOR));
$pathParts = explode(DIRECTORY_SEPARATOR, $path);
if (!$base || $baseParts[0] !== $pathParts[0]) {
// The base is not provided or is on another disk (on Windows)
// or is not an absolute path.
// Fallback to CWD if not yet in fallback mode
if (!$final) {
$cwd = getcwd();
if ($cwd !== false) {
return self::getTitlePathByFile($cwd, $path, true);
}
}
// CWD didn't work too. Turn the absolute path into titlePath.
// Add leading '/' node on Linux/MAC to avoid confusion with
// well-formed titlePath values of other tests.
return $pathParts[0]
? $pathParts
: [DIRECTORY_SEPARATOR, ...$pathParts];
}
do {
// Skipping identical parts of both paths.
array_shift($baseParts);
array_shift($pathParts);
} while ($baseParts && $pathParts && $baseParts[0] === $pathParts[0]);
if (!$pathParts) {
// If the path contains less parts than the base (is a parent of the base)
// or is equal to the base, return empty titlePath.
return [];
}
// At this point we have three cases:
// - $pathParts is empty: the path contains less parts than the base (is a
// parent of the base) or is equal to the base, return empty titlePath.
// - $baseParts is empty: the path is inside the base.
// The titlePath consists entirely of the remaining path parts.
// - $baseParts is not empty: the path is not in the base but they share a
// common ancestor. We consider this ancestor the proper root directory
// and return the remaining parts of the path as titlePath.
// Essentially, all three cases are treated identically.
return $pathParts;
}
/**
* @return list<string>
*/
public static function getTitlePathByClass(?string $class): array
{
return is_string($class)
? [...array_filter(explode("\\", $class))]
: [];
}
}