Skip to content

Add SimpleScope PHPStan-free type resolution proof of concept - #8463

Open
TomasVotruba wants to merge 12 commits into
mainfrom
simple-scope-tier3-poc
Open

Add SimpleScope PHPStan-free type resolution proof of concept#8463
TomasVotruba wants to merge 12 commits into
mainfrom
simple-scope-tier3-poc

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Why

First concrete step toward reducing PHPStan coupling in the codebase. PHPStan is currently a runtime dependency used across ~545 source files (type system, scope, reflection, phpdoc parser). This PR does not remove any of that - it proves a model for a dependency-free type layer on an isolated island, so we can evaluate the approach before touching real rules.

What

A standalone, PHPStan-free type-resolution layer:

  • Rector\SimpleType\Contract\SimpleTypeInterface + concrete StringType, IntegerType, BooleanType, NullType, ArrayType, ObjectType, MixedType
  • Rector\SimpleScope\SimpleScope - resolves an expression to a SimpleType (literals, new X, local variables)
  • Rector\SimpleScope\SimpleScopeResolver - builds a scope from parameters and local assignments, in source order
  • DemoObjectMethodCallRenameRector - a Tier-3 name-matcher rule that does its own type resolution with zero PHPStan usage in its logic

Scope and limits

  • Nothing existing is changed; this is a self-contained island.
  • Handles the local, syntactic cases (literals, new, typed params, local vars). Cross-file inference (method return types, property types, flow-sensitive unions) still needs real reflection and is intentionally out of scope - those return MixedType (safe no-op).
  • Covered by a unit test for the resolver and fixture tests for the demo rule.

Passes composer complete-check and composer rector locally.

@TomasVotruba
TomasVotruba force-pushed the simple-scope-tier3-poc branch 2 times, most recently from a6773d3 to ede5d41 Compare September 5, 2026 12:56
@TomasVotruba
TomasVotruba marked this pull request as draft September 6, 2026 06:39
@TomasVotruba
TomasVotruba force-pushed the simple-scope-tier3-poc branch 2 times, most recently from 516f073 to 4d03fc7 Compare September 6, 2026 17:18
@TomasVotruba
TomasVotruba marked this pull request as ready for review September 8, 2026 08:31
TomasVotruba and others added 10 commits September 8, 2026 10:32
Introduce a standalone, dependency-free type layer as a first step toward
reducing PHPStan coupling:

- SimpleTypeInterface + concrete scalar/array/object/null/mixed types
- SimpleScope resolves an expression to a SimpleType
- SimpleScopeResolver builds a scope from params and local assigns
- DemoObjectMethodCallRenameRector shows a Tier-3 name-matcher rule doing
  its own type resolution with zero PHPStan usage

Nothing existing is changed; the island proves the model before any real
rule migration.
…ependent reflection

Renamed past the *.php.inc glob so the suite is green while the underlying
order-dependent PHPStan reflection issue (stale single-file source locators
across fastunit workers) is investigated. Reproduce with:
  vendor/bin/fastunit -p 4 tests rules-tests utils/phpstan/tests
Restore by renaming back to fixture.php.inc once the root cause is fixed.
Group the PHPStan-free scope and type resolution under src/Analyzer:
- Rector\Analyzer\SimpleScope\{SimpleScope,SimpleScopeResolver}
- Rector\Analyzer\SimpleType\* (+ Contract\SimpleTypeInterface)
Tests mirror the layout under tests/Analyzer.
…essibleCallsRector

First real rule backed by the PHPStan-free SimpleScope: the Reflection caller
type (ReflectionProperty/ReflectionMethod) is resolved from local new/assign
and param typehints instead of PHPStan's isObjectType.

Now subscribes to function-likes and resolves their scope; setAccessible calls
outside any function body are no longer targeted (rare, and safe to leave as a
no-op on PHP 8.1+). Unresolved callers stay MixedType, so nothing is removed
unless the type is certain - no false positives.
…ionSetAccessibleCallsRector

Positive (type resolved -> removed): typed param, standalone function, closure.
Skip (unresolved/non-matching -> untouched): untyped param (mixed), non-Reflection
object, method-call caller (SimpleScope cannot infer return type - safe no-op).
…), add variadic isInstanceOf()

- SimpleTypeInterface is now a marker (no describe())
- ObjectType exposes only isInstanceOf(string ...$classNames), exact match
- rules use isInstanceOf() instead of getClassName() comparisons
- unit test asserts by concrete type instead of describe()
… it at Expression level

- SimpleScopeNodeVisitor (DecoratingNodeVisitorInterface) resolves a SimpleScope
  per function-like and attaches it to inner statements via AttributeKey::SIMPLE_SCOPE
- RemoveReflectionSetAccessibleCallsRector subscribes to Expression again and reads
  the attached scope (no in-rule attribute writing, per rector-rules)
- SimpleScope::isObjectType(Expr, string ...) folds getType + instanceof + isInstanceOf
- add fixture covering setAccessible() nested in an if () {} block
… unit test

The demo rule was only a scaffold; the real RemoveReflectionSetAccessibleCallsRector
now exercises SimpleScope. Add an isObjectType() test so the method stays covered.
@TomasVotruba
TomasVotruba force-pushed the simple-scope-tier3-poc branch from 8e365b1 to 364dd06 Compare September 8, 2026 08:32
…via native is_a

Exact match still wins first (works for non-autoloadable fixture classes); for
autoloadable classes, native is_a($class, $expected, true) resolves parent and
interface relationships - PHPStan-free. Unlocks Tier-3 rules that match an
interface or base class, not just leaf classes.
…sInstanceOf

is_a($class, $parent, true) autoloads the class, unwanted in a static tool.
The autoload-free alternatives (class_parents/class_implements) are banned by
rector-rules in favor of ReflectionProvider - i.e. PHPStan, which is what this
layer avoids. Static parent/interface resolution therefore needs its own source
reflection; until then isInstanceOf stays exact-match only.
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