Closed ruudk closed 3 years ago
The best solution would probably be to make a helper function that traverses the existing class and calls all addUse() itself. And then it will be up to user whether to use such a function or not.
foreach ($file->getNamespaces() as $namespace) {
foreach ($namespace->getClasses() as $class) {
$types = [];
foreach ($class->getMethods() as $method) {
$types[] = $method->getReturnType(true);
array_map(function ($param) use (&$types) { $types[] = $param->getType(true); }, $method->getParameters());
}
array_map(function ($param) use (&$types) { $types[] = $param->getType(true); }, $class->getProperties());
foreach (array_filter($types) as $type) {
foreach ($type->getTypes() as $subtype) {
if ($subtype->isClass() && !$subtype->isClassKeyword()) {
$namespace->addUse((string) $subtype);
}
}
}
}
}
@dg Will this be implemented in the library, or it just a sample for users to implement themselves?
@matronator the sample for users :)
I'm using this library to generate a lot of PHP classes.
For every
returnType
,propertyType
,argumentType
I have to remember to call$namespace->addUse()
.It would be really nice if these types could be automatically imported.
For example like this:
Would such a change be accepted? Or is there a better way to deal with this?