Skip to content

Return type for integers should be numeric-string  #222

Closed

Description

@Seldaek

This code here:

        $result = $this->getEntityManager()->getConnection()->fetchOne('SELECT verified FROM vendor WHERE name = :vendor', ['vendor' => $vendor]);

        return $result === '1';

Triggered:

Strict comparison using === between int<-128, 127> and '1' will always evaluate to false.

At least with doctrine connection like this it appears to really return '1'. I am not sure if there are cases where PDO will return the proper integers, or perhaps with Doctrine when using query builder it may have smarter return values. Would need to all be verified.

Activity

  1. added
    bug
    enhancement
    on Feb 1, 2022
  2. Seldaek commented on Feb 1, 2022

    @Seldaek
    ContributorAuthor

    Now the sad thing with numeric-string is we lose the integer ranges :)

  3. staabm commented on Feb 1, 2022

    @staabm
    Owner

    yeah, I guess this would only work as a big union of all possible values (and this is limited to at max 256 possible unique values by phpstan)

  4. Seldaek commented on Feb 1, 2022

    @Seldaek
    ContributorAuthor

    Yeah that or you'd need numeric-string<x,y> syntax support in phpstan. That would be better but not sure it's worth it.

  5. staabm commented on Feb 1, 2022

    @staabm
    Owner
  6. Seldaek commented on Feb 1, 2022

    @Seldaek
    ContributorAuthor

    Hmm ok.. need to check the real runtime output I get, maybe my code is broken :D

  7. Seldaek commented on Feb 2, 2022

    @Seldaek
    ContributorAuthor

    OK confirming I get '1' (string) back from a tinyint(1) column with pdo_mysql (mysqlnd 8.0.13).

    Now if I do $this->getEntityManager()->getConnection()->getNativeConnection()->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false) before it does indeed return 1, but I am not sure what other impact there is of doing this. doctrine/dbal#1270 scares me a bit :)

    It does seem like PHP 8.1 however returns correct types by default as per php/php-src@c18b1ae - so that's some good news. I'm not sure if the code here should only assume int returns for 8.1, or if it should be an option in the phpstan config (as emulated prepare statements is the default AFAIK, it should probably default to numeric-string on php<8.1 and ints on php8.1+).

  8. staabm commented on Feb 2, 2022

    @staabm
    Owner

    Thanks for investigating. Thats great news.

    We will have a new RuntimeConfiguration switch then, with which you can tell phpstan-dba how types should be handled

    https://github.com/staabm/phpstan-dba/blob/main/src/QueryReflection/RuntimeConfiguration.php

  9. Seldaek commented on Feb 2, 2022

    @Seldaek
    ContributorAuthor

    Also note that I just noticed this only says PDO MySQL in the PHP changelog. It may not apply to other DBs.

  10. staabm commented on Feb 2, 2022

    @staabm
    Owner

    php/php-src@c18b1ae also mentions a ATTR_STRINGIFY_FETCHES option.. did you try that? maybe that works without the problems of ATTR_EMULATE_PREPARES

  11. Seldaek commented on Feb 2, 2022

    @Seldaek
    ContributorAuthor

    I did try, it said it is not available for mysqlnd.. Maybe in PHP 8.1 it becomes available to revert to the old mode, that's my understanding from changelog anyway.

  12. removed
    bug
    on Feb 2, 2022
  13. staabm commented on Feb 3, 2022

    @staabm
    Owner
  14. Seldaek commented on Feb 3, 2022

    @Seldaek
    ContributorAuthor

    Great thanks, seems to work fine :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions