Skip to content

[Bug]: Conditional return type on a private method always takes the false branch #97

Closed

Description

@fdaviddpt

Bug report

On a private method, a conditional return type always takes the false branch. The same method as protected or public works.

<?php

namespace App;

interface WithCount {}
final class Counted implements WithCount {}

final class Finder
{
    /** @return ($command is WithCount ? list<object> : list<int>) */
    private function result(object $command): array
    {
        return [new \stdClass()];
    }

    public function run(): array
    {
        return $this->result(new Counted());
    }
}

(new Finder())->run();
echo "OK\n";
Fatal error: Uncaught TypePHP\Exception\TypeError: App\Finder::result(): Return value[0] must be of type int, stdClass returned in playground.php on line 13

The same method declared protected or public prints OK. A value condition on a private method fails the same way: @return ($limit is 1 ? object : list<object>) called with 1 gives Return value must be a list, stdClass returned.
Tested on v0.10.26 and on main (2ec0426).

Code snippet that reproduces the problem

https://typephp-php.github.io/docs/playground#code=N4IgLglmA2CmIC4QCUCuA7SBbWACZsEAZhLACa4Dis6sAThAMYDOIANCIwPZnxIA8AfgAOAC2EAddFPQBDHM2GzGeAILDhAbilSImekWV4A6lFEBhLhjC5gAXykk50XI2izmzXJevlcELGE4HEwvUzALK0xbB2l0J1kXNw8vADE9XjopYClcPNwAegAqItwAATpYMFQ6dFwACgASbiwsWXQKCDCzH2jBXGgusH4uACMAK1hGMAA+XAQBof49WYBKXCKC3PzhBgA3WTA8IgxpiC46yuZUaDB6scnp3GauVvayVYXZOjpZAE9tnkcnV8qDKtVargANq0ADuuAkEmYYDI5ncnnqqwAutoQXlYoDcMJUKNBoxcCd0GcLrg6BhMV8fv9CcDQWCqjU6o0Il0ALQzK43O5w7xRI5kTGrXGg2IE9D1EXpDr0SX8unyqVSKaiLgIkAAeQA0ojpCBcexOBcSABzRCgGiyUnkRBgOmwDgQa3oLiVAAqvxUABFYMIIogAEwAVg431+fwAaokIGRDud0IgQCdoNALciGNMCBD0NRaPmAJLoA4MdoqF1ujhXYRTMAAOVTe1gLZu7lGEEGYD+ddQ7pAjebZa9Ptgvtk1tYCFdw44bWtTAACnQuE26AOCLIyPPDNBmCO9INaIm6PPQLst-RILB54uR9aaPQmE-65xEj24J+lyAzCMIk3z-iOsb-GBHAPM2YF2PBQA

Expected output

OK: Counted implements WithCount, so the true branch list<object> applies, as it does for the same method declared protected.

Context: our test suite hits this on a private helper used by our list endpoints, and we have other private methods with conditional return types.

Activity

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

    @rcalicdan
    Member

    Good fine, this is cause by a checking lapses inside Typephp RuntimeCheckers engine, will be fixing this on next release..

  4. added
    next minor release
    on Oct 3, 2026
  5. linked a pull request that will close this issueNext minor released #100on Oct 3, 2026
  6. fdaviddpt commented on Oct 3, 2026

    @fdaviddpt
    Author

    A data point before the release: this still reproduces on next-minor-released (PR #100) at f617372, PHP 8.2, cache cleared.

    /**
     * @return ($asString is true ? string : int)
     */
    private function pick(bool $asString)
    {
        return $asString ? "s" : 1;
    }

    $this->pick(true) gives Return value must be of type int, string 's' returned. The same method as protected passes.

    In our codebase it hits a private getInternalMetadataByKey(string $key, bool $outputValue = false) with @return ($outputValue is true ? mixed[]|string|null : IMetadata), which every entity's metadata accessors go through, so the run stops at the first metadata read in four modules.

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

Metadata

Metadata

Assignees

Labels

bugnext minor release

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions