Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ma2783930 opened this issue Nov 28, 2024 · 0 comments
Labels

Comments

@ma2783930
Copy link

ma2783930 commented Nov 28, 2024

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

@ma2783930 ma2783930 added the bug label Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant