-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstubs.php
104 lines (86 loc) · 2.95 KB
/
stubs.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
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
<?php
function modifiersOf($thing) {
$result = [];
$modifiers =
$thing->getModifiers();
if ($modifiers & ReflectionMethod::IS_FINAL) $result[] = "final";
if ($modifiers & ReflectionMethod::IS_ABSTRACT) $result[] = "abstract";
if ($modifiers & ReflectionMethod::IS_PUBLIC) $result[] = "public";
if ($modifiers & ReflectionMethod::IS_PROTECTED) $result[] = "protected";
if ($modifiers & ReflectionMethod::IS_PRIVATE) $result[] = "private";
if ($modifiers & ReflectionMethod::IS_STATIC) $result[] = "static";
return implode(" ", $result);
}
function argumentOf($parameter) {
$argument = [];
if ($parameter->isPassedByReference()) {
$argument[] = "&";
}
if ($parameter->isArray()) {
$argument[] = "array";
}
if ($parameter->isCallable()) {
$argument[] = "callable";
}
if ($parameter->getClass()) {
$argument[] =
classOf($parameter->getClass());
}
$argument[] = sprintf("$%s", $parameter->getName());
if ($parameter->isOptional()) {
$default = $parameter->isDefaultValueAvailable() ?
$parameter->getDefaultValue() : "null";
$argument[] = sprintf("= %s", $default);
}
return implode(" ", $argument);
}
function argumentsOf($thing) {
$arguments = [];
foreach ($thing->getParameters() as $parameter) {
$arguments[] = argumentOf($parameter);
}
return implode(", ", $arguments);
}
function namespaceOf($thing) { return $thing->getNamespaceName(); }
function nameOf($thing) { return $thing->getName(); }
function classOf($thing) {
$namespace = namespaceOf($thing);
$name = nameOf($thing);
if (strpos($name, $namespace) === 0) {
return substr($name, strlen($namespace)+1);
} else return $name;
}
function implementationOf($class, $method, $stubs) {
if (isset($stubs[$class->getName()][$method->getName()])) {
return $stubs[$class->getName()][$method->getName()];
} else return "{}";
}
$stubs = [
"JITFU\Type" => [
"of" => "{ return new Type(\$type); }"
]
];
$extension = new ReflectionExtension("jitfu");
foreach ($extension->getClasses() as $class) {
$handle = fopen(sprintf(
"stubs/%s.php", classOf($class)), "w+");
fprintf($handle, "<?php\n");
fprintf($handle, "namespace %s {\n", namespaceOf($class));
fprintf($handle, "\tclass %s {\n", classOf($class));
foreach ($class->getConstants() as $name => $value) {
fprintf(
$handle, "\t\tconst %s = %s;\n", $name, $value);
}
foreach ($class->getMethods() as $method) {
fprintf(
$handle,
"\t\t%s function %s (%s) %s\n",
modifiersOf($method),
nameOf($method),
argumentsOf($method),
implementationOf($class, $method, $stubs));
}
fprintf($handle, "\t}\n");
fprintf($handle, "}\n");
}
?>