From a412308af15ce6b521a2d13b0a36333fb396b187 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Wed, 16 Nov 2022 09:10:00 +0100 Subject: [PATCH] Attribute methods can be protected --- .../Models/Actions/ResolveModelAttributesFromAttributes.php | 4 ++-- tests/expected/User.stub | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Domain/Models/Actions/ResolveModelAttributesFromAttributes.php b/src/Domain/Models/Actions/ResolveModelAttributesFromAttributes.php index ab1387e..bd969d6 100644 --- a/src/Domain/Models/Actions/ResolveModelAttributesFromAttributes.php +++ b/src/Domain/Models/Actions/ResolveModelAttributesFromAttributes.php @@ -67,7 +67,7 @@ public function execute(Model $model): void */ private function findAttributeMethods(Model $model): array { - return collect((new ReflectionClass($model->fqcn))->getMethods(ReflectionMethod::IS_PUBLIC)) + return collect((new ReflectionClass($model->fqcn))->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED)) ->filter(fn (ReflectionMethod $method): bool => !$method->isStatic()) ->filter(fn (ReflectionMethod $method): bool => $this->isAttributeMethod($method)) ->all(); @@ -86,7 +86,7 @@ private function isAttributeMethod(ReflectionMethod $method): bool */ private function getAttributeGetterAndSetter(Model $model, string $attributeMethod): array { - $attribute = $model->instance()->{$attributeMethod}(); + $attribute = (fn () => $this->{$attributeMethod}())->call($model->instance()); return [ $attribute->get !== null ? new ReflectionFunction($attribute->get) : null, diff --git a/tests/expected/User.stub b/tests/expected/User.stub index e5beb26..cd51414 100644 --- a/tests/expected/User.stub +++ b/tests/expected/User.stub @@ -47,7 +47,7 @@ class User extends Model return $this->hasMany(Post::class); } - public function city(): Attribute + protected function city(): Attribute { return new Attribute( get: fn (): ?string => $this->address->city()