Repository navigation
Expand file tree
/
Copy pathPathFilter.php
More file actions
112 lines (93 loc) · 2.78 KB
/
Copy pathPathFilter.php
File metadata and controls
112 lines (93 loc) · 2.78 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace Pharaonic\Diff\Support;
use Pharaonic\Diff\DiffOptions;
/**
* Decides which paths are compared and reported, from DiffOptions::ignore() and only()
*
* A path is ignored when it is at or below an ignore() pattern, which wins over only(). With
* only() patterns, a path is reported when it is at or below one of them, and only traversed
* when it leads to one of them; every other path is skipped without being traversed.
*
* @internal
*/
final class PathFilter
{
/**
* Not compared at all
*/
public const EXCLUDED = 0;
/**
* Compared to reach a path below it, but its own changes are not reported
*/
public const TRAVERSED = 1;
/**
* Compared and reported
*/
public const INCLUDED = 2;
/**
* @var list<list<string|null>>
*/
private array $ignore;
/**
* @var list<list<string|null>>
*/
private array $only;
public function __construct(DiffOptions $options)
{
$this->ignore = $options->ignoredPatterns();
$this->only = $options->onlyPatterns();
}
/**
* @param list<int|string> $path
*
* @return self::EXCLUDED|self::TRAVERSED|self::INCLUDED
*/
public function state(array $path): int
{
foreach ($this->ignore as $pattern) {
if (Pattern::covers($pattern, $path)) {
return self::EXCLUDED;
}
}
if ($this->only === []) {
return self::INCLUDED;
}
$state = self::EXCLUDED;
foreach ($this->only as $pattern) {
if (Pattern::covers($pattern, $path)) {
return self::INCLUDED;
}
if (Pattern::leadsTo($pattern, $path)) {
$state = self::TRAVERSED;
}
}
return $state;
}
/**
* Whether a value at these paths is skipped without being traversed
*
* @param list<int|string>|null $oldPath
* @param list<int|string>|null $newPath
*/
public function skips(?array $oldPath, ?array $newPath): bool
{
return ($oldPath !== null && $this->state($oldPath) === self::EXCLUDED)
|| ($newPath !== null && $this->state($newPath) === self::EXCLUDED);
}
/**
* Whether a change at either path may be reported
*
* @param list<int|string>|null $oldPath
* @param list<int|string>|null $newPath
*/
public function reports(?array $oldPath, ?array $newPath): bool
{
$old = $oldPath === null ? null : $this->state($oldPath);
$new = $newPath === null ? null : $this->state($newPath);
if ($old === self::EXCLUDED || $new === self::EXCLUDED) {
return false;
}
return $old === self::INCLUDED || $new === self::INCLUDED;
}
}