Skip to content

Commit dc3b65c

Browse files
committed
add cast return type from get method
1 parent b1efadb commit dc3b65c

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

php-templates/models.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,29 @@ function getBuilderMethod($method, $factory)
5151
"parameters" => $params,
5252
"return" => $return,
5353
];
54-
};
54+
}
55+
56+
function getCastReturnType($classname)
57+
{
58+
if ($classname === null) {
59+
return null;
60+
}
61+
62+
$returnType = $classname;
63+
64+
try {
65+
$class = new \ReflectionClass($classname);
66+
$method = $class->getMethod('get');
67+
68+
if ($method->hasReturnType()) {
69+
$returnType = $method->getReturnType()->getName();
70+
}
71+
72+
return $returnType;
73+
} catch (\Exception | \Throwable $e) {
74+
return $returnType;
75+
}
76+
}
5577

5678
function getModelInfo($className, $factory)
5779
{
@@ -91,6 +113,7 @@ function getModelInfo($className, $factory)
91113
->map(fn($attrs) => array_merge($attrs, [
92114
'title_case' => str_replace('_', '', \Illuminate\Support\Str::title($attrs['name'])),
93115
'documented' => $existingProperties->contains($attrs['name']),
116+
'cast' => getCastReturnType($attrs['cast'])
94117
]))
95118
->toArray();
96119

src/templates/models.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,29 @@ function getBuilderMethod($method, $factory)
5151
"parameters" => $params,
5252
"return" => $return,
5353
];
54-
};
54+
}
55+
56+
function getCastReturnType($classname)
57+
{
58+
if ($classname === null) {
59+
return null;
60+
}
61+
62+
$returnType = $classname;
63+
64+
try {
65+
$class = new \\ReflectionClass($classname);
66+
$method = $class->getMethod('get');
67+
68+
if ($method->hasReturnType()) {
69+
$returnType = $method->getReturnType()->getName();
70+
}
71+
72+
return $returnType;
73+
} catch (\\Exception | \\Throwable $e) {
74+
return $returnType;
75+
}
76+
}
5577
5678
function getModelInfo($className, $factory)
5779
{
@@ -91,6 +113,7 @@ function getModelInfo($className, $factory)
91113
->map(fn($attrs) => array_merge($attrs, [
92114
'title_case' => str_replace('_', '', \\Illuminate\\Support\\Str::title($attrs['name'])),
93115
'documented' => $existingProperties->contains($attrs['name']),
116+
'cast' => getCastReturnType($attrs['cast'])
94117
]))
95118
->toArray();
96119

0 commit comments

Comments
 (0)