phpactor icon indicating copy to clipboard operation
phpactor copied to clipboard

Laravel: Method not found false positive (on Eloquent Model)

Open calebdw opened this issue 2 years ago • 12 comments

Hello!

I'm in a Laravel Model and phpactor is reporting that a method in the same class does not exist---however, the method clearly exists and jump-to-definition works so I'm not sure what's going on...

image

calebdw avatar Nov 06 '23 14:11 calebdw

What sort class is it? an enum perhaps? otherwise can you perhaps provide a failing test? https://github.com/phpactor/phpactor/blob/2fa7ab2bdd411356fecf05dc228890681b4dc93c/lib/WorseReflection/Bridge/TolerantParser/Diagnostics/MissingMethodProvider.php#L151

dantleech avatar Nov 06 '23 16:11 dantleech

After further inspection it looks to be a conflict with ide-helper helper file...apparently the helper file stub definition is replacing the real class definition instead of complimenting it

calebdw avatar Nov 06 '23 17:11 calebdw

i'm not sure what you mean by that, there is a Eagle\...\WorkOrder stub? or your class extends a class which has a stub, or there is a mixin annotation?

dantleech avatar Nov 06 '23 19:11 dantleech

@dantleech

One way the Laravel ide-helper works is by creating an _ide_helper_models.php file which contains stubs for all the models with auto-generated docblocks that look like the following:

namespace Eagle\Maintenance\Models{
/**
 * Eagle\Maintenance\Models\WorkOrder
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 *  ...
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Data\Models\Audit> $audits
 * ...
 * @method static \Eagle\Maintenance\Builders\WorkOrderBuilder|WorkOrder canceled()
 * ...
 */
	class WorkOrder extends \Eloquent {}
}

The problem is that the stub and the class have the same name/namespace and so phpactor seems to replace its definition with the stub. Instead what should happen is the stub should compliment the class without replacing it. I tested intelephense and it prompts which definition you would like to jump to whereas phpactor jumps straight to the stub

calebdw avatar Nov 09 '23 14:11 calebdw

I see, I guess perhaps Phpactor should allow multiple files to define classes and use the combination of any definitions... although that would be potentially quite a large undertaking (while "jumping to either file" is probably not very hard, squeezing both definitions into the same Reflection class could be an issue and relates to this problem)

dantleech avatar Nov 09 '23 15:11 dantleech

hmm, also get this when including rector which has:

<?php

declare(strict_types=1);

namespace PHPUnit\Framework;

if (! class_exists('PHPUnit\Framework\TestCase')) {
    abstract class TestCase
    {
    }
}

which prevents the real PHPUnit TestCase from being found.

dantleech avatar Nov 23 '23 13:11 dantleech

I am facing similar issue.

1712483680_grim

hind-sagar-biswas avatar Apr 07 '24 09:04 hind-sagar-biswas

I am also facing this issue

caliguIa avatar Apr 16 '24 18:04 caliguIa