Skip to content

Stubs use bool where (likely) only false is possible #2692

@InvisibleSmiley

Description

@InvisibleSmiley

Situation

The stubs provided by this project are used as the basis for tools like PhpStorm and static analyzers like PHPStan.

More precise types often directly affect tool results. Example:

<?php

/** @return array<string, array<int, string>> */
function foo(RedisArray $ra): array {
  $keys = $ra->keys('*');
  assert($keys !== false);
  return $keys;
}

Currently, according to redis_array.stub.php and the API documentation, RedisArray::keys has method return type declaration bool|array and no associated PHPDoc that could be used to define a more precise return type. PhpStorm stubs and PHPStan have been updated recently to also use that signature.

Since bool is true|false, the assertion only removed false from that union type and leaves array|true, which is broader than array which is the return type of function foo.

Request

Define more precise return types in stubs, either using pure PHP (type declarations) or PHPDoc (type hints).

Requirements

  • Understanding which methods can actually only return false instead of true or false

Out of scope

  • Stubs could/should also feature more precise types in general, e.g. iterable key/value types instead of just array.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions