Skip to content

Entity::hasProperty checking is not as intuitive as the docblock suggests #197

@mfutselaar

Description

@mfutselaar

The function name suggests that it checks if the property exists on the entity. However, it checks if the property's value is null, which can cause unexpected results.

For example, in the case when you get the following result from the api:

{
     "SomeValue": null
}

You'd expect $entity->hasProperty('SomeValue') to return true, since it actually exists in the body. However, it returns false because it looks at the value instead of the property.

Instead of

public function hasProperty($key)
{
        return !is_null($this->getProperty($key));
}

You should consider using

public function hasProperty($key)
{
        return Arr::has($this->toArray(), $key);
}

Which does the actual check for the key and makes using the function useful for validation of property existence.

Changing the behavior of this function might be a bit tricky because it's probably already in use. So instead of a PR I'll leave this issue here and have you decide on what to do with it, or leave it for future Google searches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions