[Bug]: magic method warning on a field that definitely exists
Bug description
I'm running into an odd warning that shows that my attribute doesn't exist.
Here it is in the db:
Here it is in my migration file:
I pressed control shift period and everything. I tried invalidating caches. Nothing seems to make it recognize it. All other attributes work.
Even more strange, if I press control b for usages when I'm within the actual class file, it shows this:
Here it is in the helper file:
Plugin version
7.2.0.232
Operating system
Linux
Steps to reproduce
No response
Relevant log output
No response
That's weird. Could you share the first code? Maybe for some reason, PhpStorm can't understand the class for $event variable?
Thanks, yes here's the code:
<?php
namespace App\Helpers;
use App\Models\Event;
use App\Models\Registration;
use App\Models\Session;
use App\Models\WeightClass;
use Illuminate\Support\Collection;
class SessionsHelper
{
/**
* @param Event $event
* @return array
*/
public static function rebuildSessions(Event $event): array
{
$startTime = now();
self::removeOldSessions($event);
self::createSessionsFromRegistrations($event);
$lines = [];
$event->setMaxSessions();
$statsKey = 'Starting Out';
$count = $event->scheduleSessions()->count();
$sessionsBelowMin = $event->scheduleSessions()->where('competitor_count', '<', $event->min_competitors_per_session)->count();
$lines[$statsKey] = [];
$lines[$statsKey][] = ['br', "Started at: " . $startTime->toDayDateTimeString()];
$lines[$statsKey][] = ['br', "Max sessions: " . $event->max_sessions];
$lines[$statsKey][] = ['br', "Total sessions: " . $count];
$lines[$statsKey][] = ['br', "Min per session: " . $event->min_competitors_per_session];
$lines[$statsKey][] = ['br', "Ideal per session: " . $event->ideal_competitors_per_session];
$lines[$statsKey][] = ['br', "Max per session: " . $event->max_competitors_per_session];
$lines[$statsKey][] = ['br', "Sessions above minimum: " . $event->max_sessions - $sessionsBelowMin];
$lines[$statsKey][] = ['br', "Sessions below minimum count:: " . $sessionsBelowMin];
$sessionNamesHelper = new SessionNamesHelper($event);
$sessionNamesHelper->rebuildAll();
Super weird. I wanted to suggest invalidating caches, but you've already done it... Sorry, as you see, there is needed field in this class, so PhpStorm shouldn't complain.
I tried a bunch of things. I deleted the _ide_helper vendor folder, then commented out the column in the migration along with others, then redid everything. I can reproduce it 100% of the time and nothing seems to help. I tried changing the name from max_sessions to max_sessions2 and it works, changing back to max_sessions and it doesn't. I googled to see if this was like a reserved attribute or something, nothing came up. So, I'm going to go ahead and just rename my variable lol
Ok, so while changing the variable name, everything worked riiiiight up until I updated this code to use the new name. After updating it, it no longer recognizes max_session_count (the new name). So the name of the variable doesn't matter, it's actually this code that broke it:
/**
* Set the max sessions based on event configurations.
* @return void
*/
public function setMaxSessions(): void
{
$numberOfDays = ceil($this->first_session_starts_at->floatDiffInDays($this->ends_at));
$this->max_sessions = $this->max_sessions_per_day * $numberOfDays * $this->max_platforms;
$this->save();
}
I updated the code to this and now there's no problems:
/**
* Set the max sessions based on event configurations.
* @return void
*/
public function setMaxSessions(): void
{
$numberOfDays = ceil($this->first_session_starts_at->floatDiffInDays($this->ends_at));
$maxSessions = $this->max_sessions_per_day * $numberOfDays * $this->max_platforms;
$this->update(['max_sessions' => $maxSessions]);
}
Something about setting it within a model function is breaking the check. Is this a phpstorm thing or a laravel idea thing?
I can now reproduce this with 100% accuracy. To reproduce, simply do anyting in any model that sets $this->whatever_attribute to anything and you'll get warnigns of magic method access anywhere where $this->whatever_attribute is used elsewhere. For instance, this will create the issue:
public function setColors() {
$this->color = 'red';
$this->save();
}
And doing this will avoid the issue alltogether:
public function setColors() {
$model = $this;
$model->color = 'red';
$model->save();
}
It's a PhpStorm issue. Let's continue in their bug tracker.
https://youtrack.jetbrains.com/issue/WI-79801/Laravel-Model-having-a-method-using-one-of-its-properties-will-cause-Property-accessed-via-magic-method-Warning-even-though