plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Bug]: magic method warning on a field that definitely exists

Open RyanPaiva56 opened this issue 2 years ago • 6 comments

Bug description

I'm running into an odd warning that shows that my attribute doesn't exist.

Screenshot from 2023-08-26 22-28-18

Here it is in the db:

Screenshot from 2023-08-26 22-28-02

Here it is in my migration file:

Screenshot from 2023-08-26 22-30-30

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:

Screenshot from 2023-08-26 22-43-05

Here it is in the helper file:

Screenshot from 2023-08-26 22-46-31

Plugin version

7.2.0.232

Operating system

Linux

Steps to reproduce

No response

Relevant log output

No response

RyanPaiva56 avatar Aug 27 '23 02:08 RyanPaiva56

That's weird. Could you share the first code? Maybe for some reason, PhpStorm can't understand the class for $event variable?

adelf avatar Aug 27 '23 18:08 adelf

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();

RyanPaiva56 avatar Aug 28 '23 13:08 RyanPaiva56

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.

adelf avatar Aug 28 '23 13:08 adelf

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

RyanPaiva56 avatar Aug 28 '23 13:08 RyanPaiva56

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?

RyanPaiva56 avatar Aug 28 '23 13:08 RyanPaiva56

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();
}

RyanPaiva56 avatar Sep 16 '23 23:09 RyanPaiva56

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

adelf avatar Mar 21 '25 13:03 adelf