-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Switch to phpcs and upgrade codebase #2596
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
Conversation
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.
A lot of errors to solve.
Arg types cannot be added until Laravel adds them.
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 pass through the src
folder. Tests will follow later.
src/Concerns/ManagesTransactions.php
Outdated
trait ManagesTransactions | ||
{ | ||
protected ?Session $session = null; | ||
protected Session|null $session = 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.
There's a phpcs rule that enforces the union type syntax for nullable types. I personally prefer the more concise ?<type>
syntax for this. If you want to continue using ?<type>
, exclude the SlevomatCodingStandard.TypeHints.UnionTypeHintFormat.DisallowedShortNullable
rule.
src/Collection.php
Outdated
@@ -56,21 +58,23 @@ public function __call(string $method, array $parameters) | |||
|
|||
// Convert the query parameters to a json string. | |||
array_walk_recursive($parameters, function (&$item, $key) { | |||
if ($item instanceof ObjectID) { | |||
$item = (string) $item; | |||
if (! ($item instanceof ObjectID)) { |
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.
The Doctrine Coding Standard enforces an "early exit" rule, meaning that all conditionals must be written to return early to reduce code nesting. It can be useful, but I agree that it doesn't help in this case. You can either add a @phpcs-ignore
comment above the line, or remove the rule entirely if @jmikola has no objections.
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.
Adding // @phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
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.
Instead, I excluded this rule globally.
if (preg_match('/^mongodb(?:[+]srv)?:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) { | ||
$config['database'] = $matches[1]; | ||
} else { | ||
if (! preg_match('/^mongodb(?:[+]srv)?:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) { |
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.
Note: this is the kind of change made by the "early exit" rule - usually it reduces complexity of the code.
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.
It's OK for this.
src/Query/Builder.php
Outdated
@@ -982,9 +1013,11 @@ protected function compileWheres(): array | |||
} elseif (isset($where['values'])) { | |||
if (is_array($where['values'])) { | |||
array_walk_recursive($where['values'], function (&$item, $key) { | |||
if ($item instanceof DateTimeInterface) { | |||
$item = new UTCDateTime($item); | |||
if (! ($item instanceof DateTimeInterface)) { |
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.
if (! ($item instanceof DateTimeInterface)) { | |
if (! $item instanceof DateTimeInterface) { |
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.
Reverted early exit and added phpcs:ignore
src/Relations/EmbedsOneOrMany.php
Outdated
@@ -195,35 +203,31 @@ protected function getIdsArrayFrom($ids) | |||
} | |||
|
|||
foreach ($ids as &$id) { | |||
if ($id instanceof Model) { | |||
$id = $id->getKey(); | |||
if (! ($id instanceof 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.
if (! ($id instanceof Model)) { | |
if (! $id instanceof 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.
Thanks for the review. I added excluded rules and made a lot of necessary manual changes to please phpcs.
if (preg_match('/^mongodb(?:[+]srv)?:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) { | ||
$config['database'] = $matches[1]; | ||
} else { | ||
if (! preg_match('/^mongodb(?:[+]srv)?:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) { |
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.
It's OK for this.
src/Collection.php
Outdated
@@ -56,21 +58,23 @@ public function __call(string $method, array $parameters) | |||
|
|||
// Convert the query parameters to a json string. | |||
array_walk_recursive($parameters, function (&$item, $key) { | |||
if ($item instanceof ObjectID) { | |||
$item = (string) $item; | |||
if (! ($item instanceof ObjectID)) { |
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.
Adding // @phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
src/Eloquent/Model.php
Outdated
$relations[] = $key.'.'.$entityValue; | ||
} | ||
if (! ($relation instanceof QueueableEntity)) { | ||
continue; |
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.
Adding phpcs:ignore
here
src/Query/Builder.php
Outdated
@@ -880,7 +910,7 @@ protected function performUpdate($query, array $options = []) | |||
|
|||
$wheres = $this->compileWheres(); | |||
$result = $this->collection->updateMany($wheres, $query, $options); | |||
if (1 == (int) $result->isAcknowledged()) { | |||
if ((int) $result->isAcknowledged() === 1) { |
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.
Done for the the file.
src/Query/Builder.php
Outdated
@@ -982,9 +1013,11 @@ protected function compileWheres(): array | |||
} elseif (isset($where['values'])) { | |||
if (is_array($where['values'])) { | |||
array_walk_recursive($where['values'], function (&$item, $key) { | |||
if ($item instanceof DateTimeInterface) { | |||
$item = new UTCDateTime($item); | |||
if (! ($item instanceof DateTimeInterface)) { |
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.
Reverted early exit and added phpcs:ignore
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.
There are a few more phpcs violation that should be fixable using phpcbf
, other than that current changes look good.
I forgot running phpcbf after the last rebase. Fixed. |
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 🤞
To be consistent with other MongoDB PHP projects, we'll use phpcs to analyze and fix the code style.
Summary of changes:
@var
annotations in tests, replaced byassertInstanceOf
orassert
@param
&@return
phpdoc when duplicate of native typesAdded argument and return types from phpdoc. Removal of redundant phpdocs.