Skip to content

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

Merged
merged 1 commit into from
Sep 6, 2023
Merged

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 4, 2023

To be consistent with other MongoDB PHP projects, we'll use phpcs to analyze and fix the code style.

Summary of changes:

  • added "declare(strict_types=1);" to all file headers
  • added "use function" for all functions used in each file (performance gain for functions with specific opcode compilation)
  • use of short closures
  • remove @var annotations in tests, replaced by assertInstanceOf or assert
  • Use early exit when appropriate, but disable the rule
  • Remove @param & @return phpdoc when duplicate of native types
  • Added argument and return types from phpdoc. Removal of redundant phpdocs.

@GromNaN GromNaN requested review from jmikola and alcaeus September 4, 2023 09:14
@GromNaN GromNaN marked this pull request as draft September 4, 2023 09:29
Copy link
Member Author

@GromNaN GromNaN left a 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.

Copy link
Member

@alcaeus alcaeus left a 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.

trait ManagesTransactions
{
protected ?Session $session = null;
protected Session|null $session = null;
Copy link
Member

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.

@@ -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)) {
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member Author

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)) {
Copy link
Member

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.

Copy link
Member Author

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.

@@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (! ($item instanceof DateTimeInterface)) {
if (! $item instanceof DateTimeInterface) {

Copy link
Member Author

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

@@ -195,35 +203,31 @@ protected function getIdsArrayFrom($ids)
}

foreach ($ids as &$id) {
if ($id instanceof Model) {
$id = $id->getKey();
if (! ($id instanceof Model)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (! ($id instanceof Model)) {
if (! $id instanceof Model) {

Copy link
Member Author

@GromNaN GromNaN left a 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)) {
Copy link
Member Author

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.

@@ -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)) {
Copy link
Member Author

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

$relations[] = $key.'.'.$entityValue;
}
if (! ($relation instanceof QueueableEntity)) {
continue;
Copy link
Member Author

Choose a reason for hiding this comment

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

Adding phpcs:ignore here

@@ -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) {
Copy link
Member Author

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.

@@ -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)) {
Copy link
Member Author

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

@GromNaN GromNaN marked this pull request as ready for review September 6, 2023 06:44
Copy link
Member

@alcaeus alcaeus left a 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.

@GromNaN
Copy link
Member Author

GromNaN commented Sep 6, 2023

I forgot running phpcbf after the last rebase. Fixed.

@GromNaN GromNaN requested a review from alcaeus September 6, 2023 12:12
Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

LGTM 🤞

@GromNaN GromNaN merged commit 9bc8999 into mongodb:master Sep 6, 2023
@GromNaN GromNaN deleted the phpcs branch September 6, 2023 12:48
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.

2 participants