Skip to content

Commit 1329ccd

Browse files
committed
Add support for protected Attribute accessors
1 parent 2e996b6 commit 1329ccd

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

src/Console/ModelsCommand.php

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,7 @@ public function getPropertiesFromMethods($model)
576576
// methods that resemble mutators but aren't.
577577
$reflections = array_filter($reflections, function (\ReflectionMethod $methodReflection) {
578578
return !$methodReflection->isPrivate() && !(
579-
in_array(
580-
\Illuminate\Database\Eloquent\Concerns\HasAttributes::class,
581-
$methodReflection->getDeclaringClass()->getTraitNames()
582-
) && (
579+
$methodReflection->getDeclaringClass()->getName() === \Illuminate\Database\Eloquent\Model::class && (
583580
$methodReflection->getName() === 'setClassCastableAttribute' ||
584581
$methodReflection->getName() === 'setEnumCastableAttribute'
585582
)
@@ -605,18 +602,15 @@ public function getPropertiesFromMethods($model)
605602
$this->setProperty($name, $type, true, null, $comment);
606603
}
607604
} elseif ($isAttribute) {
608-
$name = Str::snake($method);
609-
$types = $this->getAttributeReturnType($model, $reflection);
610-
$comment = $this->getCommentFromDocBlock($reflection);
611-
612-
if ($types->has('get')) {
613-
$type = $this->getTypeInModel($model, $types['get']);
614-
$this->setProperty($name, $type, true, null, $comment);
615-
}
616-
617-
if ($types->has('set')) {
618-
$this->setProperty($name, null, null, true, $comment);
619-
}
605+
$types = $this->getAttributeTypes($model, $reflection);
606+
$type = $this->getTypeInModel($model, $types->get('get') ?: $types->get('set')) ?: null;
607+
$this->setProperty(
608+
Str::snake($method),
609+
$type,
610+
$types->has('get'),
611+
$types->has('set'),
612+
$this->getCommentFromDocBlock($reflection)
613+
);
620614
} elseif (
621615
Str::startsWith($method, 'set') && Str::endsWith(
622616
$method,
@@ -1136,21 +1130,33 @@ protected function hasCamelCaseModelProperties()
11361130
return $this->laravel['config']->get('ide-helper.model_camel_case_properties', false);
11371131
}
11381132

1139-
protected function getAttributeReturnType(Model $model, \ReflectionMethod $reflectionMethod): Collection
1133+
protected function getAttributeTypes(Model $model, \ReflectionMethod $reflectionMethod): Collection
11401134
{
11411135
// Private/protected ReflectionMethods require setAccessible prior to PHP 8.1
11421136
$reflectionMethod->setAccessible(true);
11431137

11441138
/** @var Attribute $attribute */
11451139
$attribute = $reflectionMethod->invoke($model);
11461140

1147-
return collect([
1148-
'get' => $attribute->get ? optional(new \ReflectionFunction($attribute->get))->getReturnType() : null,
1149-
'set' => $attribute->set ? optional(new \ReflectionFunction($attribute->set))->getReturnType() : null,
1150-
])
1151-
->filter()
1141+
$methods = new Collection();
1142+
1143+
if ($attribute->get) {
1144+
$methods['get'] = optional(new \ReflectionFunction($attribute->get))->getReturnType();
1145+
}
1146+
if ($attribute->set) {
1147+
$function = optional(new \ReflectionFunction($attribute->set));
1148+
if ($function->getNumberOfParameters() === 0) {
1149+
$methods['set'] = null;
1150+
} else {
1151+
$methods['set'] = $function->getParameters()[0]->getType();
1152+
}
1153+
}
1154+
1155+
return $methods
11521156
->map(function ($type) {
1153-
if ($type instanceof \ReflectionUnionType) {
1157+
if ($type === null) {
1158+
$types = collect([]);
1159+
} elseif ($type instanceof \ReflectionUnionType) {
11541160
$types = collect($type->getTypes())
11551161
/** @var ReflectionType $reflectionType */
11561162
->map(function ($reflectionType) {
@@ -1161,7 +1167,7 @@ protected function getAttributeReturnType(Model $model, \ReflectionMethod $refle
11611167
$types = collect($this->extractReflectionTypes($type));
11621168
}
11631169

1164-
if ($type->allowsNull()) {
1170+
if ($type && $type->allowsNull()) {
11651171
$types->push('null');
11661172
}
11671173

@@ -1415,8 +1421,7 @@ protected function getClassNameInDestinationFile(object $model, string $classNam
14151421
{
14161422
$reflection = $model instanceof ReflectionClass
14171423
? $model
1418-
: new ReflectionObject($model)
1419-
;
1424+
: new ReflectionObject($model);
14201425

14211426
$className = trim($className, '\\');
14221427
$writingToExternalFile = !$this->write || $this->write_mixin;

0 commit comments

Comments
 (0)