-
Notifications
You must be signed in to change notification settings - Fork 1
Modernize code #155
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
base: main
Are you sure you want to change the base?
Modernize code #155
Conversation
a48b99b to
7d98bbe
Compare
7d98bbe to
e194919
Compare
87bf2cd to
a83714b
Compare
40c7f0b to
be36f28
Compare
The base branch was changed.
…tructor implementation
Where an InvalidArgumentException was caused by a simple type mismatch, typed parameters will now casue a TypeError instead. Tests have been adjusted accordingly.
a83714b to
1538b72
Compare
src/Query.php
Outdated
| * @return $this | ||
| */ | ||
| public function peekAhead($peekAhead = true) | ||
| public function peekAhead($peekAhead = true): static |
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.
Can this be bool $peekAhead?
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've checked out all usages, the only cases where this could end up not being a bool are in some controllers like this one:
https://github.com/Icinga/icingaweb2-module-x509/blob/81b7e5b6611e5a0c09bb19faca194583a79b462c/application/controllers/UsageController.php#L88
Where view->compact is passed, which could technically return null or some other type. The ActionController always initializes this property with a bool in the constructor and all reassignments also use bool values. When testing the affected controllers in the UI everything worked fine aswell, so I suppose we can type the parameter as bool.
src/Query.php
Outdated
| /** | ||
| * Yield the query's results | ||
| * | ||
| * @return \Generator |
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.
You can simplify this to Generator.
src/Relation.php
Outdated
| * @return \Generator | ||
| */ | ||
| public function resolve() | ||
| public function resolve(): \Generator |
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.
Add use statement and simplify both PHPDoc and return type to Generator.
src/Relation/BelongsToMany.php
Outdated
| } | ||
|
|
||
| public function resolve() | ||
| public function resolve(): \Generator |
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.
Add use statement.
src/UnionQuery.php
Outdated
| * @return ?Query[] | ||
| */ | ||
| public function getUnions() | ||
| public function getUnions(): ?array |
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 method does not return null, does it?
| $this->assertSame($targetClass, $relation->getTargetClass()); | ||
| } | ||
|
|
||
| public function testSetTargetClassThrowsInvalidArgumentExceptionIfNotString() |
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 test can be removed because we are no longer testing an implementation detail, but PHP itself.
| /** @var Model The target model */ | ||
| protected $model; | ||
| /** @var ?Model The target model */ | ||
| protected ?Model $model; |
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 think it always makes sense to initialize nullable properties with null, as you do everywhere else. Even here, where the constructor sets the property.
| * @return Model | ||
| */ | ||
| public function getModel() | ||
| public function getModel(): Model |
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 method may return null.
Instead of using the type \Generator add a use statement so that docstrings and return types can be cleaned up.
Since the implementation will return at least an empty array null can be removed from the type.
Since model is nullable in the constructor the getModel() return type must allow null.
The removed test previously expected an InvalidArgumentException, which became obsolete with the addition of typed parameters so the test is no longer necessary.
All usages are compatible with the bool type, so the parameter is typed as bool and the cast is removed.
selfwithstatic.TypeErrorinstead.