Skip to content

Exception when virtual attribute needs to get authenticated user or fetch some query result and calculate it #1619

Open
@ma2783930

Description

@ma2783930

Versions:

  • ide-helper Version: 2.15.1
  • Laravel Version: 10.48.14
  • PHP Version: 8.2.19

Description:

There is exception when an virtual attribute created and it has some calculations based on queries or authentcated user.

Example:

protected function hasValidTiming(): Attribute
{
  $user = auth()->user();
  $timing = AnnouncementTiming::whereRelation('user', 'department_id', $user->department_id)->first();

  return new Attribute(
      get: fn() => $timing ? $timing->isFuture() : false
  );
}

And this causes an exception becase $user object is null in console environment while ide-helper tries to access and read it.

I have to add this custom logic in start of method:

if (app()->runningInConsole()) {
   return new Attribute( get: fn() => false );
}

But I think this custom logic is a very bad way and it needs to fix on ide-helper.

I think may be an option is needed in ide-helper config to tell it to do not process Attribute body and just generate its name for some Attributes to prevent calculation and developer logic exceptions.

Thank you much

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions