Skip to content

Commit 9915db7

Browse files
authored
Merge pull request #65 from neclimdul/type_hints
Add @var docblock type hinting to exported classes
2 parents 34093c8 + 98f5577 commit 9915db7

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

utilities/includes/ClassSeparator.php

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,67 @@ private function writeClassesToFiles(array $classes)
151151
$date = $this->generated_at;
152152

153153
return array_walk($classes, function ($class) use ($date) {
154-
preg_match('~([^ ]+)~', $class, $name);
155-
$filename = base_path() . '/src/Classes/' . $name[0] . '.php';
156-
$template = include utilities_path() . "/includes/templates/class.template.php";
154+
$tokens = array_filter(token_get_all('<?php class ' . $class), function ($token) {
155+
return !(!is_array($token) || $token[0] == T_WHITESPACE);
156+
});
157+
$types = array();
158+
while ($token = current($tokens)) {
159+
// If this is the class definition, grab the name.
160+
if ($token[0] === T_CLASS) {
161+
$token = next($tokens);
162+
$name = $token[1];
163+
}
164+
// If we see the variable definition for the special
165+
// "paramtypesmap" property that defines all the other property
166+
// types then start parsing out those types.
167+
if ($token[0] === T_VARIABLE && $token[1] == '$paramtypesmap') {
168+
while ($type_token = next($tokens)) {
169+
// Throw out the opening array tag.
170+
if ($type_token[0] === T_ARRAY) {
171+
continue;
172+
}
173+
// This looks like a type definition.
174+
if ($type_token[0] === T_CONSTANT_ENCAPSED_STRING) {
175+
// Store the key as the property name.
176+
$property_name = trim($type_token[1], '"');
177+
// Throw away the => separator.
178+
next($tokens);
179+
// Store the value as the type.
180+
$type_token = next($tokens);
181+
// Store the values as the property type.
182+
$property_type = trim($type_token[1], '"');
183+
// If this is describing a scalar value, just use it.
184+
if (FALSE !== array_search(
185+
str_replace(array('[',']'), '', $property_type),
186+
array('string', 'integer', 'boolean', 'float')
187+
)) {
188+
$types[$property_name] = $property_type;
189+
}
190+
// Date time is really a string containing a date.
191+
elseif ($property_type == 'dateTime' || $property_type == 'dateTime[]') {
192+
$types[$property_name] = str_replace('dateTime', 'string', $property_type);
193+
}
194+
// This is a NetSuite value so map it to a class.
195+
else {
196+
$types[$property_name] = '\\NetSuite\\Classes\\' . $property_type;
197+
}
198+
}
199+
// We've fallen out of the array definition so continue.
200+
else {
201+
$token = $type_token;
202+
break;
203+
}
204+
}
205+
}
206+
next($tokens);
207+
}
208+
// Add a doc comment for each property.
209+
foreach ($types as $variable_name => $property_type) {
210+
$class = preg_replace('/public \$' . $variable_name . ';/', "/**\n * @var $property_type\n */\n public \$$variable_name;", $class);
211+
}
212+
// Template write the class.
213+
$filename = base_path() . '/src/Classes/' . $name . '.php';
214+
$template = include utilities_path() . '/includes/templates/class.template.php';
157215
file_put_contents($filename, $template);
158216
});
159217
}

0 commit comments

Comments
 (0)