Skip to content

fix: skip non-native methods - #7747

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
calebdw:calebdw/push-owqotkpsoqlk
Dec 25, 2025
Merged

fix: skip non-native methods#7747
TomasVotruba merged 1 commit into
rectorphp:mainfrom
calebdw:calebdw/push-owqotkpsoqlk

Conversation

@calebdw

@calebdw calebdw commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

Hello!

This prevents ALL non-native methods (not just those from annotations) from being converted to first class callables:

This change resulted in the following phpstan error:

image image

@TomasVotruba

Copy link
Copy Markdown
Member

Thanks 👍

Can you also add test fixture that verifies this change? Just so we don't accidentally change it in the future.

@calebdw

calebdw commented Dec 9, 2025

Copy link
Copy Markdown
Contributor Author

It's already testing by the existing annotation fixture---I didn't want to write a whole PHPStan extension just to test something other than a AnnotationMethodReflection (e.g., Laravel's macro system)

@calebdw
calebdw force-pushed the calebdw/push-owqotkpsoqlk branch from 27475fc to e818fd2 Compare December 9, 2025 21:27
@calebdw
calebdw force-pushed the calebdw/push-owqotkpsoqlk branch from e818fd2 to cb12687 Compare December 9, 2025 21:27
@TomasVotruba

Copy link
Copy Markdown
Member

No need for extension. What is "non-native method" apart the @method?

@calebdw

calebdw commented Dec 10, 2025

Copy link
Copy Markdown
Contributor Author

This is a Macro:

MenuItem::macro('reportable', static function (string $class): MenuItem {
    if (! class_exists($class) || ! is_a($class, Reportable::class, true)) {
        throw new RuntimeException("Invalid reportable class [{$class}].");
    }

    $instance = resolve($class);

    return MenuItem::make($instance->name())
        ->canSeeWith($instance::permissions())
        ->path($instance->route());
});

See: https://github.com/larastan/larastan/blob/3.x/src/Methods/MacroMethodsClassReflectionExtension.php

@TomasVotruba

Copy link
Copy Markdown
Member

So in short, the reportable() does not exist?

@calebdw

calebdw commented Dec 10, 2025

Copy link
Copy Markdown
Contributor Author

Yes, it's not an actual method, it's a closure that is called through __call: https://github.com/laravel/framework/blob/12.x/src/Illuminate/Macroable/Traits/Macroable.php

Note, this does not actually result in a php error, so I'm not sure why phpstan added a check for it. I'm thinking about just globally ignoring callable.nonNativeMethod, but I figured that rector should not make changes that result in phpstan errors

@TomasVotruba

Copy link
Copy Markdown
Member

I see. How would you implement it if no Rector nor PHPStan would exists?

@calebdw

calebdw commented Dec 10, 2025

Copy link
Copy Markdown
Contributor Author

I'm not sure what you're asking? Implement what?

@TomasVotruba

TomasVotruba commented Dec 10, 2025

Copy link
Copy Markdown
Member

The change this rule is doing. How would you upgrade code yourself manually, without any tooling.

@calebdw

calebdw commented Dec 10, 2025

Copy link
Copy Markdown
Contributor Author

I would be fine with this upgrade in favor of the first class callable, however, other people might not like it since it throws phpstan errors.

This PR just ensures that an actual method exists on the declaring class and that the method in not a macro or annotation

@TomasVotruba

Copy link
Copy Markdown
Member

Allright, lets go this way then.

We still need a test fixture to backup this change.

@TomasVotruba

Copy link
Copy Markdown
Member

Just checking, as there is no feedback in past 2 weeks. Any plan to finish this?
I want to avoid staling failing PRs here.

@calebdw

calebdw commented Dec 25, 2025

Copy link
Copy Markdown
Contributor Author

Yes, I plan to, but it's Christmas and I'm sick---I haven't had time yet to write a custom phpstan extension just to test this.

If you'd like to merge now then feel free, there's an existing passing test that covers this.

@calebdw

calebdw commented Dec 25, 2025

Copy link
Copy Markdown
Contributor Author

To be transparent, I've just globally ignored these phpstan errors in my config as I don't care to enforce them and it doesn't cause any sort of runtime error

Not sure these phpstan rules were added in the first place...

@TomasVotruba

Copy link
Copy Markdown
Member

I see, let's give this a go then 👍

@TomasVotruba
TomasVotruba merged commit 3f2b48a into rectorphp:main Dec 25, 2025
66 of 75 checks passed
@calebdw
calebdw deleted the calebdw/push-owqotkpsoqlk branch December 25, 2025 22:55
@calebdw

calebdw commented Dec 25, 2025

Copy link
Copy Markdown
Contributor Author

Sounds good, thank you sir and have a Merry Christmas!!

@TomasVotruba

Copy link
Copy Markdown
Member

Thank you 👍 Merry Christmas to you and your family 🎄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants