Skip to content
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

Apply coding style #652

Merged
merged 14 commits into from
Oct 11, 2023
Merged

Apply coding style #652

merged 14 commits into from
Oct 11, 2023

Conversation

lehecht
Copy link
Contributor

@lehecht lehecht commented Sep 15, 2023

.php_cs is outdated and cannot be used anymore due to PHP CS Fixer 3 upgrade. Instead use .php-cs-fixer.php and updated methods.

.php_cs is outdated and cannot be used anymore
due to PHP CS Fixer 3 upgrade. Instead use
.php-cs-fixer.php and updated methods.
Copy link
Member

@mzur mzur left a comment

Choose a reason for hiding this comment

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

Thanks! Please also update the fix script in composer.json to use the new config file.

Beside the comments below, a problem I always had is the indentation that CS fixer wants to enforce. For example, it suggests something like this:

         $this->putJson("/api/v1/maia/annotation-candidates/{$a->id}", [
-                // Must be points of a circle.
-                'points' => [10, 20],
-            ])
+            // Must be points of a circle.
+            'points' => [10, 20],
+        ])
             ->assertStatus(422);

But I want the parenthesis of putJson() to end at the same level of indentation than the chained assertStatus(). Either the parenthesis (and arguments) are indented one level or the chained method is unindented. Can you find a ruleset that does this? You can check the proposed output with composer fix -- --dry-run --diff.

.php-cs-fixer.php Outdated Show resolved Hide resolved
.php-cs-fixer.php Outdated Show resolved Hide resolved
.php-cs-fixer.php Outdated Show resolved Hide resolved
.php-cs-fixer.php Show resolved Hide resolved
@mzur mzur linked an issue Sep 15, 2023 that may be closed by this pull request
@mzur
Copy link
Member

mzur commented Sep 15, 2023

Also please remember to put the Resolves #651 in the commit message body. This makes the commit title more readable and GitHub will automatically link the issue to the PR 😉 Example:

Fix name of .php_cs file

.php_cs is outdated and cannot be used anymore
due to PHP CS Fixer 3 upgrade. Instead use
.php-cs-fixer.php and updated methods.

Resolves #651

@lehecht
Copy link
Contributor Author

lehecht commented Sep 15, 2023

Ah yes. Sorry, I completely forgot about this. Shall I change this and force push the changes?

@mzur
Copy link
Member

mzur commented Sep 15, 2023

Force push because of the commit message? No, it's fine. Just do it next time 😉

@lehecht lehecht self-assigned this Sep 18, 2023
@mzur
Copy link
Member

mzur commented Sep 25, 2023

I've opened a discussion for the method chain indentation issue: PHP-CS-Fixer/PHP-CS-Fixer#7319

@mzur
Copy link
Member

mzur commented Sep 25, 2023

In the discussion, the use_arrow_functions rule was suggested. I think I'd like to include this rule here, too.

@mzur
Copy link
Member

mzur commented Sep 25, 2023

So, one possible solution (from the discussion linked above) would be to make sure the line with the opening [ or { is properly indented like this:

$this
    ->putJson("/api/v1/maia/annotation-candidates/{$a->id}", [
        // Must be points of a circle.
        'points' => [10, 20],
    ])
    ->assertStatus(422);

or this:

$parentConflicts = 
    $mergeLabels->filter(function ($label) {
        return array_key_exists('conflicting_parent_id', $label);
    })
    ->each(function ($label) use ($parentConflictResolution) {

This should work both for the multiline array case and for the multiline closure case. I could live with this coding style but unfortunately all these cases would have to be updated by hand. We could do this all with great effort now and then enforce the style in the future. On the other hand, this is a cosmetic change and might cost a lot of time for "nothing". @lehecht Could you estimate how much manual work would be involved?

@lehecht
Copy link
Contributor Author

lehecht commented Sep 28, 2023

Should something like

$query = $this->volume->files()
  ->when($this->only, function ($query) {
   return $query->whereIn('id', $this->only);
  });

be changed to this

$query = $this
  ->volume->files()
  ->when($this->only, fn ($query) => $query->whereIn('id', $this->only));

or to this ?

$query =
$this->volume->files()
  ->when($this->only, fn ($query) => $query->whereIn('id', $this->only));

@lehecht
Copy link
Contributor Author

lehecht commented Sep 28, 2023

Could you estimate how much manual work would be involved?

I wrote a small python parser to help recognize some lines with wrong indentation and fix them. This helps a lot but will still require proof reading. Furthermore testing is needed. I wouldn't rely on the composer test command alone since some bugs could occur after using several functions consecutively. So I guess it will take between 3-4 days.

@mzur
Copy link
Member

mzur commented Sep 28, 2023

I would change your example from

$query = $this->volume->files()
   ->when($this->only, function ($query) {
      return $query->whereIn('id', $this->only);
   });

to

$query =  $this->volume
   ->files()
   ->when($this->only, fn ($query) => $query->whereIn('id', $this->only));

To be more consistent, I'll update my example from above to

$parentConflicts = $mergeLabels
    ->filter(function ($label) {
        return array_key_exists('conflicting_parent_id', $label);
    })
    ->each(function ($label) use ($parentConflictResolution) {

It's also only a problem with the CS Fixer if the closure or array is opened at a different level of indentation than it should be closed (see my examples above). In your example 1) the closure starts at the same level than it should be closed and 2) in the updated code there is no multi-line closure at all any more.

I wouldn't rely on the composer test command alone since some bugs could occur after using several functions consecutively.

If that's the case then we sould update the tests 😉 Do you have an example for that?

So I guess it will take between 3-4 days.

That's a lot but I think it's worth it. Afterwards we can enforce the coding style in pull requests.

@lehecht lehecht marked this pull request as draft September 29, 2023 07:09
@lehecht
Copy link
Contributor Author

lehecht commented Sep 29, 2023

I applied your changes and a new rule (no_unused_imports) to the app directory. Could you look through some files to check if you like it? I will apply those changes to all other files after you gave me your feedback.

Some candidates to check could be:

@lehecht
Copy link
Contributor Author

lehecht commented Sep 29, 2023

If that's the case then we sould update the tests 😉 Do you have an example for that?

No, I don't have one. I just meant that more complex bugs sometimes occur when several functions were used consecutively. But maybe I'm worrying too much about it. I also don't know how every test looks like. So if you think, it's enough to just run composer test then I will just do that.

@lehecht lehecht requested a review from mzur September 29, 2023 07:51
Copy link
Member

@mzur mzur 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 many changes that I wouldn't do. I've tried to give a few examples.

If this is getting too complicated, let me know. We could always disable any indentation style checks, too.

app/Annotation.php Outdated Show resolved Hide resolved
app/AnnotationSession.php Outdated Show resolved Hide resolved
app/AnnotationSession.php Outdated Show resolved Hide resolved
app/AnnotationSession.php Outdated Show resolved Hide resolved
app/AnnotationSession.php Outdated Show resolved Hide resolved
app/Http/Controllers/Api/AnnotationSessionController.php Outdated Show resolved Hide resolved
@lehecht lehecht marked this pull request as ready for review October 5, 2023 07:34
@mzur mzur self-requested a review October 5, 2023 09:02
@mzur
Copy link
Member

mzur commented Oct 5, 2023

I saw that you marked this ready for review and I'll have a look. Next time please also click to re-request my review otherwise I won't get a notification for this 😉

Copy link
Member

@mzur mzur left a comment

Choose a reason for hiding this comment

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

Looks like a nice cleanup! I really like all the arrow functions that we now got for free and that make the code more readable.

I've added several suggestions that should make the code even more readable in context of what we agreed on here. It's probably easiest for you if you commit them as a batch here in the GitHub UI (if you agree with them).

Do you also want to add the GitHub action that will complain about incorrect coding style in this PR?

When this is all sorted out and working, the modules come next 😉 But maybe you should work on something else in the meantime so you don't do CS fixed for the rest of the year 😄 You could pick up one module after the other inbetween other tasks (bugfixes, features).

app/AnnotationSession.php Outdated Show resolved Hide resolved
app/Jobs/CloneImagesOrVideos.php Outdated Show resolved Hide resolved
app/Jobs/CloneImagesOrVideos.php Outdated Show resolved Hide resolved
tests/php/Jobs/CloneImagesOrVideosTest.php Outdated Show resolved Hide resolved
tests/php/Jobs/CloneImagesOrVideosTest.php Outdated Show resolved Hide resolved
tests/php/Jobs/CloneImagesOrVideosTest.php Outdated Show resolved Hide resolved
tests/php/Jobs/CloneImagesOrVideosTest.php Outdated Show resolved Hide resolved
tests/php/Jobs/CreateNewImagesOrVideosTest.php Outdated Show resolved Hide resolved
@mzur mzur changed the title Resolves #651 by using .php-cs-fixer.php Apply coding style Oct 6, 2023
@lehecht
Copy link
Contributor Author

lehecht commented Oct 9, 2023

Looks like a nice cleanup! I really like all the arrow functions that we now got for free and that make the code more readable.

I've added several suggestions that should make the code even more readable in context of what we agreed on here. It's probably easiest for you if you commit them as a batch here in the GitHub UI (if you agree with them).

Thank you for your big support! I'm sorry that you still had to put so much time into it. I really just went through all marked files of git diff.

Co-authored-by: Martin Zurowietz <[email protected]>
@lehecht
Copy link
Contributor Author

lehecht commented Oct 9, 2023

Do you also want to add the GitHub action that will complain about incorrect coding style in this PR?

Yes! But I will need some more informations on how to do it.

@lehecht lehecht requested a review from mzur October 9, 2023 08:12
Copy link
Member

@mzur mzur 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 3 unresolved comments from my previous review.

I think we are almost done now. But I suggest that the GitHub action should be added in another PR so we can merge this huge one first.

When you want to implement a new GitHub action, you add a new file to .github/workflows. However, we already have lint.yml there to which the new job could be added, too. As a start, all you have to do is to copy the lint-php block and append it as new cs-php block to the file. Then you only have to update the last command from composer lint to composer fix -- --dry-run. After that we can try to figure out how to add GitHub annotations.

app/AnnotationSession.php Outdated Show resolved Hide resolved
app/Project.php Outdated Show resolved Hide resolved
Comment on lines 78 to 79
$this
->postJson("/api/v1/projects/{$id}/invitations", [
Copy link
Member

Choose a reason for hiding this comment

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

This has to be updated, too.

tests/php/Http/Controllers/Api/VolumeControllerTest.php Outdated Show resolved Hide resolved
tests/php/Jobs/CloneImagesOrVideosTest.php Outdated Show resolved Hide resolved
tests/php/Jobs/CloneImagesOrVideosTest.php Outdated Show resolved Hide resolved
@lehecht
Copy link
Contributor Author

lehecht commented Oct 11, 2023

After that we can try to figure out how to add GitHub annotations.

Unfortunately, the given website states that PHP-CS Fixer doesn't support GitHub annotations.

@mzur
Copy link
Member

mzur commented Oct 11, 2023

The linked article suggests a solution with php-cs-fixer fix -vvv --dry-run --format=checkstyle | cs2pr. We can try that in #677.

@mzur mzur merged commit 759687c into master Oct 11, 2023
@mzur mzur deleted the php-cs-fixer branch October 11, 2023 13:19
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.

Update .php-cs file
2 participants