Skip to content

[Bug]: Methods returning by reference no longer return a reference #98

Closed

Description

@fdaviddpt

Bug report

A method that returns by reference (function &name()) no longer returns a reference once TypePHP is active, even without any docblock. Writes through the reference are lost and PHP emits a notice.

<?php

final class Bag
{
    public array $items = [1];

    public function &items(): array
    {
        return $this->items;
    }
}

$bag = new Bag();
$ref = &$bag->items();
$ref[] = 2;

echo json_encode($bag->items), "\n";

With TypePHP:

[1]
PHP Notice:  Only variable references should be returned by reference in playground.php on line 9

Without TypePHP (TYPEPHP_DISABLE=true): [1,2].
Tested on v0.10.26 and on main (2ec0426).

Code snippet that reproduces the problem

https://typephp-php.github.io/docs/playground#code=N4IgLglmA2CmIC4QCUCuA7SBbWACZsEAZhLACa4Dis6sAThAMYDOIANCIwPZnxIA8AfgAOAC2EAddFJLoAhtFyNoc5s1wAhOQHMpwKbkO5hqAEbQmuOXTpyAnrgAkUWFnUBeXAG0AjAF0AbikDIxNzSyIMRkgudFwAMhc3AAoASgQrG3sQw304owK6WDBUOjjHMFEIZgBaAD4k5iD8wwBfKXbpdEdTHVxPWgB3TR005scion6Enp16xrGpCdgiLz9pgCZmqVhGUS5cACtmWIB9Gm5eZNntebBXZlS2XAkQCWCQZvZOWJJtRFANDk5nIiDAdFQsA4EG06C4RQAKrZGLAACKwYSVRAbACsHGstjsADUFBAyHIYuhECBItBoN9mOCmGACCUytRaAxGABJdAAN2sEDk6BRYIhUJARWYwl2YAAchSIHzYHLUHTgRALGA7GLIRwpTLotzYfDYAidKwEOC9SAsDomAAFOhcGV0bUEORkS1EBTMCUQdAWWgkuiW0DCZ2uyCwS3WiXaGj0Jix8UcRgKFQglM25jplSh3USgn2bMSrimQ6y0utGtAA

Expected output

[1,2], no notice: the reference returned by items() points to $bag->items, as in plain PHP.

Context: our test suite hits the notice on &getCommandOptions() in our list endpoint base classes. Code that relies on writing through such a reference would silently change behaviour.

Activity

  1. self-assigned this
    on Oct 3, 2026
  2. rcalicdan commented on Oct 3, 2026

    @rcalicdan
    Member

    This is certainly to be an ast bug in typephp ast transformation parser.. Will look into it..

  3. linked a pull request that will close this issueNext minor released #100on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingnext minor releasefeature for next minor release

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions