Description
Laravel Version
10
PHP Version
8.2
Database Driver & Version
MySql
Description
HasAttributes.php has the method isRelation.
This method only checks if there is no mutator with the same name and if there is a method with this name.
But if you have some helper methods which are not accessors ("getXyzAttribute" or "xyz(): Attribute"), it will still say it is an relation, which is not correct.
In my oppinion there should be a check for the return type like it is used in "hasAttributeMutator":
$returnType = (new ReflectionMethod($this, $method))->getReturnType();
return static::$attributeMutatorCache[get_class($this)][$key] =
$returnType instanceof ReflectionNamedType &&
$returnType->getName() === Attribute::class;
If there are some helper methods which are not relations but return the same type, they could be defined in a property and excluded by this check to support third party packages and custom code.
"Illuminate\Database\Eloquent\Relations\Relation" could be the type instead of Attribute::class in this case.
But with is_a instead of a basic check to include classes which extend Relation::class.
Or is there any other reason why this was not implemented this way?
Steps To Reproduce
- Create an model
- Add a helper method e.g. pizza() this model
- isRelation('pizza') on a object of this model would result in true, but it is not an relation