Skip to content

Fix StaticCallOnNonStaticToInstanceCallRector for same-class calls - #7876

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
carlos-granados:static-call-on-same-class
Feb 3, 2026
Merged

Fix StaticCallOnNonStaticToInstanceCallRector for same-class calls#7876
TomasVotruba merged 1 commit into
rectorphp:mainfrom
carlos-granados:static-call-on-same-class

Conversation

@carlos-granados

Copy link
Copy Markdown
Contributor

The StaticCallOnNonStaticToInstanceCallRector rule was skipping the case where the static call was to a method that was implemented in the same class or a superclass. But it should actually only skip if it is a superclass. For the same class, it makes more sense to convert the call to a method call to the current instance.

Fixes rectorphp/rector#9613

When calling a non-static method statically from within the same class,
convert to $this->method() instead of skipping. This eliminates the
PHP deprecation warning for non-static method called statically.

Calls to ancestor methods (e.g., Grandpa::method() from a descendant)
are still skipped as they are valid non-static usage.
@TomasVotruba
TomasVotruba merged commit 5ce62c7 into rectorphp:main Feb 3, 2026
57 checks passed
@TomasVotruba

Copy link
Copy Markdown
Member

Looks good to me, thank you

@carlos-granados
carlos-granados deleted the static-call-on-same-class branch February 4, 2026 11:07
nikspyratos added a commit to nikspyratos/rector-src that referenced this pull request Jun 5, 2026
…c scope

StaticCallOnNonStaticToInstanceCallRector rewrites a same-class static call
`SameClass::nonStaticMethod()` to `$this->nonStaticMethod()` without checking
whether the enclosing function-like is `static`. Inside a static method, static
closure or static arrow function there is no `$this`, so the generated code
fatals at runtime with "Using $this when not in object context".

The same-class `$this->` branch was widened in rectorphp#7876 (fixing rectorphp/rector#9613)
so that same-class static calls always convert, but it lacks a static-scope
guard. This is the bug reported in rectorphp/rector#9647.

Guard the same-class branch with `$scope->hasVariableType('this')->yes()`, which
PHPStan resolves correctly for static methods, static closures and static arrow
functions (including those nested inside non-static scopes). When `$this` is not
available the rule now skips instead of producing invalid code; instance-scope
behavior from rectorphp#7876 is unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Not fixing statics within a class properly

2 participants