Skip to content

Conversation

@johnsaigle
Copy link
Contributor

Brief summary of changes

In an effort to take advantage of PHP's type system, we should add type declarations to our code, especially in the core classes.

The PDO documentation says that fetchAll returns the empty set when no data in the DB matches a given query and FALSE on failure. However we do not distinguish this in our codebase and so a FALSE value is allowed to bubble up through subsequent DB calls.

This makes it so that these functions must sometimes return the Bool type (in addition to String, Array, and Null) meaning we cannot use return type declarations.

Following a discussion in #3854, this PR adds an exception when the fetchAll class fails. This will allow us to remove Bool as one of the return types.

This is likely to break some things (I found it by debugging a failing test in #3878) so it is issued to major.

Related issues

@johnsaigle johnsaigle changed the title [Core: Database] Add exception on db failure case [Core: Database] Add exception on fetchAll failure case Aug 15, 2018
// The fetchAll function will return an empty set if no results are
// found and FALSE on "failure". We don't expect this case so an
// exception is thrown.
if (is_bool($rows)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't checking if it's false, it's checking if it's a boolean. It should be === false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that at first but phan gave me an error about comparing an array to a boolean (in the case where it was NOT false).

I'll change this statement to include both and see how phan feels about it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the docs pretty clearly say that the return type is array|false, so if phan is complaining about checking the false case it sounds like a bug in phan.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about !is_array() ?

@johnsaigle
Copy link
Contributor Author

@driusan @xlecours This is ready for re-review.

Copy link
Contributor

@xlecours xlecours left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this highlights the fact that the Database::execute function is doing more than just one thing.

@johnsaigle
Copy link
Contributor Author

@xlecours Yeah a lot of the DB functions return many many different types which makes validation and use of them kinda tricky. This is one small step in the process....

@driusan driusan merged commit 8d5a59a into aces:major Aug 27, 2018
@ridz1208 ridz1208 added this to the 21.0.0 milestone Sep 23, 2018
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.

4 participants