Skip to content

Commit 30651ec

Browse files
committed
Refactoring
1 parent 2f0ebf5 commit 30651ec

File tree

3 files changed

+92
-93
lines changed

3 files changed

+92
-93
lines changed

bin/gen_base_callmap.sh

-13
This file was deleted.

bin/gen_callmap.php

+92-16
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,106 @@
22

33
declare(strict_types=1);
44

5+
use Webmozart\Assert\Assert;
6+
57
require __DIR__ . '/gen_callmap_utils.php';
68

7-
$callMap = require "dictionaries/CallMap.php";
8-
$orig = $callMap;
9+
// Load+normalize autogenerated maps
10+
$baseMaps = [];
11+
foreach (glob(__DIR__."/../dictionaries/autogen/CallMap_*.php") as $file) {
12+
Assert::eq(preg_match('/_(\d+)\.php/', $file, $matches), 1);
13+
$version = $matches[1];
14+
15+
$baseMaps[$version] = normalizeCallMap(require $file);
16+
writeCallMap($file, $baseMaps[$version]);
17+
}
18+
19+
ksort($baseMaps);
20+
$last = array_key_last($baseMaps);
21+
22+
// Load+normalize hand-written diff maps
23+
$customMaps = [
24+
$last => normalizeCallMap(require __DIR__."/../dictionaries/override/CallMap.php")
25+
];
26+
27+
$customDiffs = [];
28+
foreach (glob(__DIR__."/../dictionaries/override/CallMap_*.php") as $file) {
29+
Assert::eq(preg_match('/_(\d+)_delta\.php/', $file, $matches), 1);
30+
$version = $matches[1];
31+
$customDiffs[$version] = normalizeCallMap(require $file);
32+
}
33+
krsort($customDiffs);
34+
35+
$versions = array_reverse(array_keys($customDiffs));
36+
37+
// Merge hand-written diff maps to generate hand-written full maps
38+
foreach ($customDiffs as $version => $customDiff) {
39+
$customMap = $customMaps[$version];
40+
41+
foreach ($customDiff['removed'] as $func => $descr) {
42+
$customMap[$func] = $descr;
43+
}
44+
foreach ($customDiff['added'] as $func => $descr) {
45+
unset($customMap[$func]);
46+
}
47+
foreach ($customDiff['changed'] as $func => $sub) {
48+
$customMap[$func] = $sub['old'];
49+
}
950

10-
foreach ($callMap as $functionName => &$entry) {
11-
$refl = getReflectionFunction($functionName);
12-
if (!$refl) {
51+
$prevVersion = array_search($version, $versions)-1;
52+
if ($prevVersion < 0) {
1353
continue;
1454
}
15-
assertEntryParameters($refl, $entry);
16-
} unset($entry);
55+
$customMaps[$versions[$prevVersion]] = $customMap;
56+
}
57+
krsort($customMaps);
58+
59+
// Merge hand-written full maps into autogenerated full maps, write to files
60+
foreach ($customMaps as $version => $data) {
61+
writeCallMap("dictionaries/CallMap_$version.php", array_replace($baseMaps[$version] ?? [], $data));
62+
}
63+
64+
// Cleanup hand-written full maps, removing data that is the same in the autogenerated full maps
65+
foreach ($customMaps as $version => &$data) {
66+
foreach ($data as $name => $func) {
67+
if (($baseMaps[$version][$name] ?? null) === $func) {
68+
unset($data[$name]);
69+
}
70+
}
71+
$data = normalizeCallMap($data);
72+
} unset($data);
1773

18-
writeCallMap("dictionaries/CallMap.php", $callMap);
74+
// Write base custom map
75+
writeCallMap(__DIR__."/../dictionaries/override/CallMap.php", $customMaps[$last]);
1976

20-
$diffFile = "dictionaries/CallMap_84_delta.php";
77+
$customDiffs = [];
2178

22-
$diff = require $diffFile;
79+
foreach ($customMaps as $version => $cur) {
80+
$prevVersion = array_search($version, $versions)-1;
81+
if ($prevVersion < 0) {
82+
continue;
83+
}
84+
$prevVersion = $versions[$prevVersion];
85+
86+
$prev = $customMaps[$prevVersion];
2387

24-
foreach ($callMap as $functionName => $entry) {
25-
if ($orig[$functionName] !== $entry) {
26-
$diff['changed'][$functionName]['old'] = $orig[$functionName];
27-
$diff['changed'][$functionName]['new'] = $entry;
88+
$diff = ['changed' => [], 'added' => [], 'removed' => []];
89+
foreach ($prev as $name => $func) {
90+
if (!isset($cur[$name])) {
91+
$diff['removed'][$name] = $func;
92+
} else if ($cur[$name] !== $func) {
93+
$diff['changed'][$name] = [
94+
'old' => $func,
95+
'new' => $cur[$name]
96+
];
97+
}
98+
}
99+
100+
foreach ($cur as $name => $func) {
101+
if (!isset($prev[$name])) {
102+
$diff['added'][$name] = $func;
103+
}
28104
}
29-
}
30105

31-
writeCallMap($diffFile, $diff);
106+
writeCallMap("dictionaries/override/CallMap_$version.php", $diff);
107+
}

bin/normalize-callmap.php

-64
This file was deleted.

0 commit comments

Comments
 (0)