Skip to content

Conversation

@johnsaigle
Copy link
Contributor

@johnsaigle johnsaigle commented Aug 14, 2018

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:

  • In some cases where the documentation conflicted with the actual return type, the documentation was amended and the functionality preserved.
  • Functions that were documented as returning void but had a return statement were changed to not return anything.
  • Default values were largely left as is even in cases where another might arguably be better (e.g. Strings set to null instead of the empty string)
    • This has been changed based on feedback by @driusan .

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.

  • The Database class should work as before. If it doesn't, it has been used improperly and the calling classes should be modified to use it as documented.

@johnsaigle johnsaigle added Category: Cleanup PR or issue introducing/requiring at least one clean-up operation [branch] major labels Aug 14, 2018
@johnsaigle johnsaigle added this to the 21.0.0 milestone Aug 14, 2018
@johnsaigle johnsaigle changed the title [Core] Add type declarations to function signatures [Core: Database] Add type declarations to function signatures Aug 14, 2018
@driusan driusan added the State: Needs work PR awaiting additional work by the author to proceed label Aug 16, 2018
@driusan
Copy link
Collaborator

driusan commented Aug 16, 2018

(Added Needs Work because it's failing Travis, haven't looked into why.)

Copy link
Contributor

@PapillonMcGill PapillonMcGill left a comment

Choose a reason for hiding this comment

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

LGTM

@johnsaigle
Copy link
Contributor Author

@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 Blocked is more appropriate than Needs Work?

@driusan driusan added State: Blocked PR or issue awaiting an external event such as the merge or another PR to proceed and removed State: Needs work PR awaiting additional work by the author to proceed labels Aug 20, 2018
@driusan
Copy link
Collaborator

driusan commented Aug 20, 2018

Sure, I changed the tag.

$trackChanges = true
?string $username,
?string $password,
?string $host,
Copy link
Collaborator

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?

Copy link
Contributor Author

@johnsaigle johnsaigle Aug 22, 2018

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.

Copy link
Collaborator

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?

Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

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.

driusan pushed a commit that referenced this pull request Aug 27, 2018
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
string $database = null,
string $username = null,
string $password = null,
string $host = null,
Copy link
Collaborator

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.

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 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.

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

@johnsaigle
Copy link
Contributor Author

This depends on #3909 to pass Travis. Will rebase this once that is merged.

@driusan
Copy link
Collaborator

driusan commented Aug 29, 2018

Maybe this PR should also add declare(strict_types=1); to the file?

@johnsaigle
Copy link
Contributor Author

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.

@driusan
Copy link
Collaborator

driusan commented Sep 10, 2018

@johnsaigle #3909 is merged

@driusan driusan removed the State: Blocked PR or issue awaiting an external event such as the merge or another PR to proceed label Sep 10, 2018
Copy link
Contributor

@zaliqarosli zaliqarosli left a comment

Choose a reason for hiding this comment

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

first round changes

@johnsaigle johnsaigle added the "Help! I don't understand Travis!" PR is having a beef with TRAVIS. Someone needs to help label Oct 2, 2018
@kongtiaowang
Copy link
Contributor

screen shot 2018-10-15 at 11 15 10 am
screen shot 2018-10-15 at 11 14 51 am
It will help you to sove the Travis issue.

@kongtiaowang kongtiaowang removed the "Help! I don't understand Travis!" PR is having a beef with TRAVIS. Someone needs to help label Oct 15, 2018
$trackChanges = true
?string $username,
?string $password,
?string $host,
Copy link
Contributor

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

@johnsaigle johnsaigle added the State: Discussion required PR or issue that requires the resolution of a discussion with the relevant parties to proceed label Nov 19, 2018
@driusan driusan merged commit 426fe51 into aces:major Nov 19, 2018
@johnsaigle johnsaigle removed the State: Discussion required PR or issue that requires the resolution of a discussion with the relevant parties to proceed label Dec 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Category: Cleanup PR or issue introducing/requiring at least one clean-up operation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants