-
Notifications
You must be signed in to change notification settings - Fork 188
[Core: Database] Add type declarations to function signatures #3878
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
[Core: Database] Add type declarations to function signatures #3878
Conversation
|
(Added Needs Work because it's failing Travis, haven't looked into why.) |
PapillonMcGill
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
@driusan I believe it's failing in the cases in LORIS where we have baked-in type coercion. It's likely that several smaller PRs normalizing the use of function calls and return values from this function will need to be merged before this can be merged. Perhaps |
|
Sure, I changed the tag. |
| $trackChanges = true | ||
| ?string $username, | ||
| ?string $password, | ||
| ?string $host, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one seems weird.. what does it mean to pass those as null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly not totally sure. It requires further research. However when these are set to just strings (null not allowed) Travis throws up a ton of errors during the execution of unit tests. Something to the effect of "MockDatabase::connect() requires a string parameter for param X but null given".
I don't know enough about our automated testing to know what to do about that, so I changed this function to allow for null. I'm very open to suggestions here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kongtiaowang Any thoughts on how to solve that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnsaigle other than resolving this ^ issue and travis, i think everything else looks good. will test in a sec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apache php7.2 upgrade not working for me so can't test at the moment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should go ahead and merge this. I think the nullable strings thing is likely a quirk of PHPUnit. In any case if any of the params is null then the DB will error out anyway due to invalid credentials.
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 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 the database class throws an exception in the failure case following discussion in #3854. This will allow us to remove Bool as one of the return types in the future. Related issues: #3854, #3878
php/libraries/Database.class.inc
Outdated
| string $database = null, | ||
| string $username = null, | ||
| string $password = null, | ||
| string $host = null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to fail, null isn't a sting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran phan locally and it didn't seem to have an issue.... which is weird cause you're right.
I'll change this and the if statement below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's.. problematic? Maybe it just checks it at the call site and it's never called without them being explicitly passed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe? Not sure how the internals of phan work.
|
This depends on #3909 to pass Travis. Will rebase this once that is merged. |
|
Maybe this PR should also add |
|
Yeah I had that earlier but think I removed it because something was going wrong and I thought it was the cause. I'll add it back. |
|
@johnsaigle #3909 is merged |
zaliqarosli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
first round changes
| $trackChanges = true | ||
| ?string $username, | ||
| ?string $password, | ||
| ?string $host, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnsaigle other than resolving this ^ issue and travis, i think everything else looks good. will test in a sec
… into 180814-FunctionSignaturesDatabase


Brief summary of changes
Type declarations and return type declarations are added to all function signatures in the Database class. This follows a discussion about moving toward the universal use of PHP's typing features to prevent notices, warnings, and other nasty behaviour resulting from type coercion.
I tried not to alter any functionality by following the return types already listed in the PHP Docs for each function:
Default values were largely left as is even in cases where another might arguably be better (e.g. Strings set tonullinstead of the empty string)Further work to be done
Some of the functions were not given return types (notably all
pselect*functions) because they return mixed types. We need to refactor these functions to behave consistently.This resolves issue...
Nothing concrete but many potential issues arising from PHP's type coercion.
Related PRs:
A check means it's merged
To test this change...
Important if you get an error about a return type like
...must be an instance of void, none returned, that means the version of PHP being used by your Apache is older than PHP 7.1. As of the last release we require >= 7.2 so be sure to correct this.