plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Bug]: Model scopes shown as unused

Open DevquasarX9 opened this issue 6 months ago • 5 comments

Bug description

Environment

  • PhpStorm version: 2025.1.2 (Build #PS-251.26094.133, built on June 5, 2025)
  • Laravel Idea plugin version: 10.2.2.251
  • Laravel version: v12.18.0
  • PHP version: 8.4

Description

Model scopes are not recognized as “used” by Laravel Idea when they are called through the query builder. Example:

use App\Models\HardwareMaintenanceLog;

HardwareMaintenanceLog::byStatus('completed')->get();

The scope byStatus is correctly defined inside the HardwareMaintenanceLog model, but Laravel Idea does not:

  • mark the scope as “used” in the gutter/code lens;
  • offer “Go to declaration” or show a reference count;
  • include the scope in “Find usages” results.

Expected behaviour

  1. The scope should be detected as used when called via Model::scopeName() or its camel-cased alias (Model::name()).
  2. IDE gutter/lens should show reference counters.
  3. “Go to declaration” (⌘/Ctrl + B) should jump to the scope definition.

Actual behaviour

  • No reference counter is shown.
  • Scope appears as unused in IDE inspections.

Additional info

  • Tried Invalidate Caches / Restart – no change.
  • Scopes are located in the same namespace/file as the model.
  • Autocomplete for the scope works; only reference tracking is missing.

Plugin version

10.2.2.251

Operating system

Linux

Steps to reproduce

  1. Create a Laravel model:

    class HardwareMaintenanceLog extends Model
    {
        public function scopeByStatus($query, string $status): void
        {
            $query->where('status', $status);
        }
    }
    
  2. Call the scope somewhere in the code:

    HardwareMaintenanceLog::byStatus('completed')->get();
    
  3. Observe that Laravel Idea does not detect any usages of scopeByStatus.

Relevant log output


DevquasarX9 avatar Jun 18 '25 13:06 DevquasarX9

Thanks for your report! I've added this to internal task tracker.

Can you please check, if toggling this option in plugin will help?

Image

It may not fix code lens counter, but we will work on that as well.

gorbunov avatar Jun 20 '25 05:06 gorbunov

I have the same situation. I have tried enabling the "Implicit eloquent method usages" check box and it did not help.

    /**
     * scope to fetch only published products
     *
     * @param  Builder<self>  $query
     * @return Builder<self>
     */
    protected function scopePublished(Builder $query): Builder
    {
        return $query->where('status', ProductStatusType::PUBLISHED->value);
    }

    /**
     * @param  Builder<self>  $query
     * @return Builder<self>
     */
    protected function scopeAdEligibleProducts(Builder $query): Builder
    {
        return $query->published()
            ->whereActive(true)
            ->whereHas('featuredImage')
            ->where('quantity', '>', 0);
    }

ElvisIsKing666 avatar Jul 29 '25 18:07 ElvisIsKing666

@ElvisIsKing666 I've checked your case. Everything works in the 11.0.1 version. It shows scopePublished as "used" and "scopeAdEligibleProducts" as "notUsed" But if I use this scope anywhere, it immediately becomes normal, "used".

adelf avatar Aug 09 '25 17:08 adelf

@gorbunov The "Implicit Eloquent method usages searcher" option changes behaviour to be as expected, but I am hesitant to leave it on because of the CPU usage warning.

It would be awesome if you could optimise the algorithm somehow!

enduity avatar Nov 26 '25 11:11 enduity

I am hesitant to leave it on because of the CPU usage warning.

It's just that phpStorm's ImplicitUsageSearcher internal interface is marked as resource intensive, since usages will be actively updated on document edit instead of using reference cache, and it needed for usage search of things that linked by "magic strings" (strings with class names, configs strings, etc.). Search itself is optimized, but we decided still keep warning for user discretion.

In reality, unless you on old laptop, or low on battery power (in power saving mode this feature may be disabled by IDE itself), or working on giant monorepo you will likely won't notice any higher CPU usage

gorbunov avatar Nov 27 '25 05:11 gorbunov