Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle all hydration mode in QueryResultDynamicReturnTypeExtension #453

Merged
merged 11 commits into from
Jan 16, 2024

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented May 4, 2023

@VincentLanglet VincentLanglet marked this pull request as ready for review May 4, 2023 11:51
@VincentLanglet VincentLanglet changed the title Improve QueryResultDynamicReturnTypeExtension (Third try) Handle all hydration mode in QueryResultDynamicReturnTypeExtension Oct 4, 2023
@VincentLanglet
Copy link
Contributor Author

Hi @ondrejmirtes, would you have time to take a new look on this and tell me if you encounter any issue on your personnal projects with this PR with all the new fixes provided. Thanks :)

@VincentLanglet
Copy link
Contributor Author

Hi @ondrejmirtes, happy new year.

Any feedback about how I could help moving this PR forward ?
I tried the PR on my side and got:

  • no false positive
  • three real error found by phpstan

@ondrejmirtes
Copy link
Member

I can merge this, test 4 real world projects, collect errors and then revert it again.

@ondrejmirtes ondrejmirtes merged commit 95523ef into phpstan:1.3.x Jan 16, 2024
26 checks passed
@VincentLanglet
Copy link
Contributor Author

I can merge this, test 4 real world projects, collect errors and then revert it again.

Thanks, please ping me on any issue. I'll make myself available.

@ondrejmirtes
Copy link
Member

Real world project number 1: 48 new errors, most are "Casting to int something that's already int.". A few real bugs found. But the PR is not currently usable because COUNT(pr.id) AS count as inferred as int<0, max>|numeric-string which will be solved by #506.

Real world project number 2: 21 new errors, some queries use getScalarResult and getResult(AbstractQuery::HYDRATE_SCALAR). In these cases the inferred type is mixed which should be improved. Example query (anonymized):

 $qb = $this->createQueryBuilder('a');
 $result = $qb
            ->select('a.one, a.two, a.three')
            ->andWhere($qb->expr()->in('a.four', $four))
            ->orderBy('a.date', 'ASC')
            ->getQuery()
            ->getResult(AbstractQuery::HYDRATE_SCALAR);

Can you please look into this bug? Reverting the change for now.

@VincentLanglet
Copy link
Contributor Author

Real world project number 1: 48 new errors, most are "Casting to int something that's already int.". A few real bugs found. But the PR is not currently usable because COUNT(pr.id) AS count as inferred as int<0, max>|numeric-string which will be solved by #506.

Great.

Real world project number 2: 21 new errors, some queries use getScalarResult and getResult(AbstractQuery::HYDRATE_SCALAR). In these cases the inferred type is mixed which should be improved. Example query (anonymized):

 $qb = $this->createQueryBuilder('a');
 $result = $qb
            ->select('a.one, a.two, a.three')
            ->andWhere($qb->expr()->in('a.four', $four))
            ->orderBy('a.date', 'ASC')
            ->getQuery()
            ->getResult(AbstractQuery::HYDRATE_SCALAR);

Can you please look into this bug? Reverting the change for now.

Sure, what was the inferred type before mixed ?

@ondrejmirtes
Copy link
Member

I'm sorry to be wrong here. The type before was mixed, the type with your PR is list<array>. But it should be more precise. Because right now it moves the error from level 9 to a lower level.

@VincentLanglet
Copy link
Contributor Author

I'm sorry to be wrong here. The type before was mixed, the type with your PR is list<array>. But it should be more precise. Because right now it moves the error from level 9 to a lower level.

Ok I see.

I had a test for

$query = $em->createQuery('
			SELECT		m.intColumn, m.stringNullColumn
			FROM		QueryResult\Entities\Many m
		');

		assertType(
			'list<array{intColumn: int, stringNullColumn: string|null}>',
			$query->getResult(AbstractQuery::HYDRATE_SCALAR)
		);

So I assume that in your example, 'a.one, a.two, a.three' are not all scalar columns. (Maybe a datetime)

It's not easy in those case to infer the correct type.
I prefer to follow the same strategy than #444
If I cannot exactly infer the type, at least I return the same than before to avoid changing error levels.

opened #520 as draft since we need #506 first

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.

None yet

2 participants