Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.30% |
52 / 54 |
|
88.24% |
15 / 17 |
CRAP | |
0.00% |
0 / 1 |
WorkspaceCache | |
96.30% |
52 / 54 |
|
88.24% |
15 / 17 |
39 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
loadFiles | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
validate | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFiles | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
getDuplicateId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUnit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getBooklet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSysCheck | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addFile | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
markUnusedItems | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
9 | |||
setGlobalIds | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getGlobalIds | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
initializeFilesArray | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
addGlobalIdSource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** @noinspection PhpUnhandledExceptionInspection */ |
4 | declare(strict_types=1); |
5 | |
6 | class WorkspaceCache { |
7 | protected array $cachedFiles = []; |
8 | protected array $duplicates = []; |
9 | protected array $used = []; |
10 | protected Workspace $workspace; |
11 | protected array $globalIds = []; // type => [id => fileName] |
12 | |
13 | function __construct(Workspace $workspace) { |
14 | $this->workspace = $workspace; |
15 | $this->setGlobalIds(); |
16 | $this->initializeFilesArray(); |
17 | } |
18 | |
19 | public function loadFiles(): void { |
20 | foreach (Workspace::subFolders as $type) { |
21 | $pattern = ($type == 'Resource') ? "*.*" : "*.[xX][mM][lL]"; |
22 | $filePaths = Folder::glob($this->workspace->getOrCreateSubFolderPath($type), $pattern, true); |
23 | |
24 | foreach ($filePaths as $filePath) { |
25 | $file = File::get($filePath, $type); |
26 | $this->addFile($type, $file); |
27 | } |
28 | } |
29 | } |
30 | |
31 | public function validate(?string $onlyValidateThisType = null): void { |
32 | if ($onlyValidateThisType) { |
33 | foreach ($this->cachedFiles[$onlyValidateThisType] as $file) { |
34 | /* @var $file File */ |
35 | $file->crossValidate($this); |
36 | } |
37 | } else { |
38 | foreach ($this->cachedFiles as $fileSet) { |
39 | foreach ($fileSet as $file) { |
40 | /* @var $file File */ |
41 | $file->crossValidate($this); |
42 | } |
43 | } |
44 | } |
45 | |
46 | $this->markUnusedItems(); |
47 | } |
48 | |
49 | public function getId(): int { |
50 | return $this->workspace->getId(); |
51 | } |
52 | |
53 | public function getFiles(bool $flat = false): array { |
54 | if (!$flat) { |
55 | return $this->cachedFiles; |
56 | } |
57 | |
58 | $files = []; |
59 | |
60 | foreach ($this->cachedFiles as $fileSet) { |
61 | foreach ($fileSet as /** @var File */ $file) { |
62 | $files[$file->getPath()] = $file; |
63 | } |
64 | } |
65 | |
66 | return $files; |
67 | } |
68 | |
69 | public function getDuplicateId(File $file): ?string { |
70 | return $this->duplicates["{$file->getType()}/{$file->getName()}"] ?? null; |
71 | } |
72 | |
73 | public function getFile(string $type, string $fileId): ?File { |
74 | return $this->cachedFiles[$type][$fileId] ?? null; |
75 | } |
76 | |
77 | public function getResource(string $resourceId): ?ResourceFile { |
78 | return $this->cachedFiles['Resource'][$resourceId] ?? null; |
79 | } |
80 | |
81 | public function getUnit(string $unitId): ?XMLFileUnit { |
82 | return $this->cachedFiles['Unit'][$unitId] ?? null; |
83 | } |
84 | |
85 | public function getBooklet(string $bookletId): ?XMLFileBooklet { |
86 | return $this->cachedFiles['Booklet'][$bookletId] ?? null; |
87 | } |
88 | |
89 | public function getSysCheck(string $sysCheckId): ?XMLFileSysCheck { |
90 | return $this->cachedFiles['SysCheck'][$sysCheckId] ?? null; |
91 | } |
92 | |
93 | public function addFile(string $type, File $file, $overwriteAllowed = false): string { |
94 | $index = $file->getId(); |
95 | |
96 | if (isset($this->cachedFiles[$type][$index])) { |
97 | $duplicate = $this->cachedFiles[$type][$index]; |
98 | |
99 | if (!$overwriteAllowed or ($file->getName() !== $duplicate->getName())) { |
100 | $index = md5(microtime()); |
101 | $this->duplicates["{$file->getType()}/{$file->getName()}"] = $index; |
102 | } |
103 | } |
104 | |
105 | $this->cachedFiles[$type][$index] = $file; |
106 | |
107 | return "$type/{$file->getId()}"; |
108 | } |
109 | |
110 | public function markUnusedItems(): void { |
111 | $relationsMap = []; |
112 | |
113 | foreach (Workspace::subFolders as $type) { |
114 | foreach ($this->cachedFiles[$type] as $file) { |
115 | /* @var $file File */ |
116 | if ($file::canBeRelationSubject) { |
117 | $relations = $file->getRelations(); |
118 | foreach ($relations as $relation) { |
119 | /* @var FileRelation $relation */ |
120 | $relationsMap[$relation->getTargetType()][strtoupper($relation->getTargetName())] = $file->getName(); |
121 | } |
122 | } |
123 | } |
124 | } |
125 | |
126 | foreach (Workspace::subFolders as $type) { |
127 | foreach ($this->cachedFiles[$type] as $file) { |
128 | /* @var $file File */ |
129 | |
130 | if ($file::canBeRelationObject and !isset($relationsMap[$file->getType()][strtoupper($file->getId())])) { |
131 | $file->report('warning', "{$file->getType()} is never used"); |
132 | } |
133 | } |
134 | } |
135 | } |
136 | |
137 | public function setGlobalIds(): void { |
138 | $this->globalIds = $this->workspace->workspaceDAO->getGlobalIds(); |
139 | } |
140 | |
141 | public function getGlobalIds(): array { |
142 | return $this->globalIds; |
143 | } |
144 | |
145 | private function initializeFilesArray(): void { |
146 | foreach (Workspace::subFolders as $type) { |
147 | $this->cachedFiles[$type] = []; |
148 | } |
149 | } |
150 | |
151 | public function addGlobalIdSource(string $fileName, string $type, array $idList): void { |
152 | $this->globalIds[$this->getId()][$fileName][$type] = $idList; |
153 | } |
154 | } |