Skip to content

feat: a plain PHP method with no return type implements a PHP# interface member - #185

Merged
heyJordanParker merged 5 commits into
masterfrom
feat/untyped-implementers
Oct 20, 2026
Merged

heyJordanParker merged 5 commits into
masterfrom
feat/untyped-implementers

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

A PHP# interface member keeps the type the user writes. A plain PHP class whose method declares no return type, its own or one a package gives it, now implements the member at mago compile and when the engine links the class. Each PHP# call of the member checks the result, so a wrong value is refused where PHP# code uses it, with an error that names the member and the type it states as PHP# writes them.

Before this, every Eloquent model behind a PHP# interface failed: string getMorphClass() over Model::getMorphClass() was refused by mago analyze and fataled when the class linked. The only way out was to state the member Any? and cast each read, which is writing around the PHP underneath.

How it works

The call checks the result. The bridge wraps each PHP# call of a PHP# interface's instance member, and each call of its method value, in one VERIFY_TYPE_ARGS named App.Taggable.morphToMany(). The check reads each List, Set, Map and Iterable in the result alone, a tuple's included, so it costs the same however many elements the result holds. A type that holds a float, an Any or an Object is still checked whole, as section 24 checks every value from plain PHP. A void or never member takes no check. Plain PHP callers stay unchecked.

The error names the type the code wrote. The check's text carries the type the error names after the type it checks, as List<Any?>, List<int>, and the engine names the second. A null-safe call's error names Relation, not Relation?.

A call on the plain class is a call of the member. When a class's method declares no return type and a PHP# interface the class implements declares the member, the analyzer types contact.morphToMany(…) and this.morphToMany(…) in a PHP# subclass by the member, and the bridge checks it as a call through the interface. A class whose interfaces declare different results for the method is refused at the link, as Kotlin refuses conflicting inherited members.

A generic method's caller checks the type it gives. A check inside a generic method, or a generic class's static method, reads the type parameter as any type. So a call that gives that type argument checks the result against it, as Java checks an erased generic method's result at its caller. A call that gives its own method's type parameter leaves the check to the call that gives that one, and a data enum's case factory takes none.

The engine links an untyped method under the member. The bridge marks the member's result ZEND_SHARP_RESULT_CALLER_CHECKED, and the compiler turns it into a type bit beside ZEND_PARAM_WIDENED. Inheritance takes an implementation without a return type under it. This retires #179's forwarder, so a PHP# class over an untyped plain parent declares nothing for the member.

What stays as it was:

  • A method that declares another type, as : string under MorphToMany, is refused at compile and at load.
  • A plain PHP method under a PHP# class's method still declares its return type, since a call of a class's method checks nothing.
  • A call of a PHP# class's own non-generic method, and of an interface's static operator, pays no check.

Verification

Row Observed
1 MorphToMany morphToMany(string related, string name) over a plain class inheriting an untyped package method: compile accepts it, the engine loads it, and the call returns the relation with no cast, through the interface, on the plain class and on this of a PHP# subclass.
2 The package method returns a string: TypeError: CallerChecked.Taggable.morphToMany(): Return value must be of type CallerChecked.Relation, string returned.
3 A : string method under the member: compile refuses it with incompatible-return-type, and the engine fatals at link.
4 A call through the interface pays one SHARP_VERIFY_TYPE_ARGS after DO_FCALL. A call of a PHP# class's own method pays none.
5 The probe interface-member-typed-against-untyped-php-implementer compiles, analyzes with no issue, and prints label:record for the PHP# and the plain PHP implementer.
6 A plain PHP project analyzes and compiles as on master.

Tests

Zend/tests/sharp/caller_checked.phpt runs in process, under every opcache mode, repeat and each optimizer pass. It covers a never member, a generic class's static method, a method's own type parameter, a null-safe call, a tuple, the plain class, a PHP# subclass and its this, and each collection kind with a wrong value, with every error's exact text. Each case failed on the previous head: the never member panicked mago compile, the plain class and the subclass were refused, the static method asserted in the engine, the method's type parameter let a string through, and the errors named List<Any?> and Relation?.

The bridge tests a_call_of_an_interface_member_checks_its_result, a_call_of_a_plain_class_method_under_an_interface_member_checks_its_result, a_call_of_a_generic_method_checks_its_result_against_the_type_it_gives and an_interface_member_marks_its_return_type_as_checked_by_each_call, and the analyzer test a_call_of_a_plain_class_method_under_an_interface_member_takes_the_members_type, each failed on the previous head.

Spec

Section 22 states the rule beside the plain-PHP-implementer bullet, with an example sharp/bin/spec runs, and a second bullet for the generic caller's check. The MockK and all-open line is corrected in section 22 and in question 6 of slice 22.

…ace member

A PHP# interface member keeps the type it states. Each PHP# call of the member,
and each call of its method value, checks the result in one VERIFY_TYPE_ARGS
named as PHP# writes the member, as `App.Taggable.morphToMany()`. The check
reads a collection alone, as `List<Any?>`, so it costs the same however many
elements the result holds.

So a plain PHP class implements the member with a method that declares no
return type, its own or one a package gives it, as Eloquent's Model gives
getMorphClass():

- the bridge marks the member's result ZEND_SHARP_RESULT_CALLER_CHECKED, and
  the engine links an untyped method under it, beside ZEND_PARAM_WIDENED;
- the analysis refuses none, by the one codex predicate
  ClassLikeMetadata::checks_results_at_call the bridge reads too.

A method that declares another type is refused at compile and at load, as
before. A call of a PHP# class's own method, or of an interface's static
operator, checks nothing.
… adds

patch-coverage needs every added line of the bridge run. The member's class and
metadata now come from the one lookup of its callee, so no branch is left that
no call reaches. A result PHP# writes always carries its type text, so marking
it is an unreachable!(…) for any other node, as the bridge writes impossible
arms. The call test checks a Set, a Map and an Iterable member too.
… the call is written

The review of #185 found six gaps in the check each PHP# call of a PHP# interface's member runs on its result.

- A `never` or `void` member no longer panics mago compile: the bridge marks the member's result
  ZEND_SHARP_RESULT_CALLER_CHECKED from the attr it gives the result, never from its node kind.
- A call of a plain PHP class's untyped method, on the class or on `this` of a PHP# subclass, is a call of
  the member of the class's PHP# interface that declares it: the analyzer types it by the member, and the
  bridge checks it as a call through the interface. A class whose interfaces declare different results for
  the method is refused at the link, as Kotlin refuses conflicting inherited members.
- The error names the type the member states: the check's text carries the type the error names after the
  type it checks, so a `List<int>` member's error says `List<int>`, and a null-safe call's says `Relation`.
- The check reads each collection alone at any depth, a tuple's elements included.
- A check's text in a static member of a generic class names the class's type parameter as the static
  member's `#i`, through the rewrite its own type texts take, so it no longer asserts in the engine.
- A call that gives a generic method, or a generic class's static method, the type argument its result
  names checks the result against that type, as Java checks an erased generic method's result at its
  caller, since a check in the method's body reads the type parameter as any type.

#179's forwarder goes: the engine links a plain PHP parent's untyped method under the member itself, and a
PHP# subclass's calls take the member's type and check, so Lowering::forwarders, Types::forwarded_methods and
the analyzer's forwarded exemption are deleted.

The in-process engine test caller_checked.phpt runs every case under opcache, repeat and each optimizer pass.
@heyJordanParker
heyJordanParker merged commit 6b246c8 into master Oct 20, 2026
26 checks passed
@heyJordanParker
heyJordanParker deleted the feat/untyped-implementers branch November 3, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant