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

[TASK] Update our coding standard #1264

Merged
merged 5 commits into from
Jun 30, 2024
Merged

[TASK] Update our coding standard #1264

merged 5 commits into from
Jun 30, 2024

Conversation

oliverklee
Copy link
Contributor

Switch to the PER2 coding standard and reduce the number of custom rules.

This basically aligns our code style with that used by the PHP-CSS-Parser project, which is very close to the PER-2 coding standard.

Fixes #1262

@oliverklee oliverklee added this to the 8.0.0 milestone Jun 6, 2024
@oliverklee oliverklee requested a review from JakeQZ June 6, 2024 14:25
@oliverklee oliverklee self-assigned this Jun 6, 2024
@oliverklee oliverklee marked this pull request as draft June 12, 2024 07:40
@oliverklee
Copy link
Contributor Author

It looks like I'll need to tweak the ruleset a bit to retain compatibility with PHP 7.3 and 7.4.

@oliverklee oliverklee force-pushed the task/fixer-rules branch 3 times, most recently from d3a69f5 to 7590cb4 Compare June 19, 2024 17:45
@oliverklee oliverklee marked this pull request as ready for review June 19, 2024 17:47
@oliverklee
Copy link
Contributor Author

@JakeQZ friendly ping :-)

@JakeQZ
Copy link
Contributor

JakeQZ commented Jun 22, 2024

@JakeQZ friendly ping :-)

Sorry, I missed this amongst the other 464 emails in the last 5 days. Not even exaggerating - I just counted them ;)

Copy link
Contributor

@JakeQZ JakeQZ left a comment

Choose a reason for hiding this comment

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

I'll need to go through the removed settings in more detail, which will take a bit of time I don't have right now. But there is the match PHP 8 issue.

'no_unused_imports' => true,
'ordered_imports' => true,
// overwrite the PER2 defaults to restore compatibility with PHP 7.x
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'match']],
Copy link
Contributor

Choose a reason for hiding this comment

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

As per issue on sister project, we can't include match here, until PHP-CS-Fixer fixes the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's removed now.

Copy link
Contributor

Choose a reason for hiding this comment

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

The match doesn't appear to be removed from where I'm looking.

(BTW, I've realized now that the defaults haven't changed with the new PHP-CS-Fixer version, it's rather than it's included in the @PER-CS2.0 rule set - and that may have changed between Fixer versions.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, it seems match was there 2 times. I've removed the other one now as well, and also created MyIntervals/PHP-CSS-Parser#629.

Copy link
Contributor

@JakeQZ JakeQZ left a comment

Choose a reason for hiding this comment

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

I've been through all the added and removed rules, and checked what the defaults are, and whether they are included in any of the rule sets that will be active following this change.

Quite a few of the removed rules I think we need to keep, because they are not applied by default, nor included in an rule set.

Many of the other removed rules seemed to be incorrectly specified as a Boolean, when in fact what is expected is an array of configuration values. I've ignored those, assuming they were invalid and would have had no effect anyway. I don't know if I'm missing something, like if it's possible to provide a Boolean when an array is expected, and that would result in enabling all options (though in some cases the options aren't Boolean either).

Some of the added rules appear unnecessary as they are the default values anyway. At least one of the added rules is given as a Boolean when an array is expected.

Comment on lines 39 to 41
// casing
'magic_constant_casing' => true,
'native_function_casing' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to keep these, since they are not part of any of the rule sets.

Copy link
Contributor

Choose a reason for hiding this comment

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

GitHub diff isn't highlighting the lines I selected, so you'll need to check the smallprint, e.g. "Comment on lines -39 to -41" to see how many preceding lines I'm referring to. If there's nothing there, it means I'm just commenting on a single line.

Comment on lines 46 to 47
'modernize_types_casting' => true,
'no_short_bool_cast' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto these two.

// class notation
'class_attributes_separation' => true,
'no_blank_lines_after_class_opening' => true,
'no_php4_constructor' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

And this one.

// (no rules used from this section)

// comment
'no_empty_comment' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

And this one.


// control structure
'elseif' => true,
'no_superfluous_elseif' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

This one...

'phpdoc_separation' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
Copy link
Contributor

Choose a reason for hiding this comment

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

And this.

'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],

// return notation
'no_useless_return' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

And this.

Comment on lines 137 to 139
'no_empty_statement' => true,
'no_singleline_whitespace_before_semicolons' => true,
'semicolon_after_instruction' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

And these three.

'space_after_semicolon' => true,

// strict
'declare_strict_types' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

And this.

'declare_strict_types' => true,

// string notation
'escape_implicit_backslashes' => ['single_quoted' => true],
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be replaced with string_implicit_backslashes => ['single_quoted' => 'escape'], which isn't in any of the rule sets used (escape_implicit_backslashes is deprecated).

Switch to the PER2 coding standard and reduce the number
of custom rules.

This basically aligns our code style with that used by
the PHP-CSS-Parser project, which is very close to the
PER-2 coding standard.

Fixes #1262
@oliverklee
Copy link
Contributor Author

@JakeQZ Thank your for doing the painstaking work of going through this with a fine-grained comb! 🙏 I've now updated the ruleset accordingly.

Copy link
Contributor

@JakeQZ JakeQZ left a comment

Choose a reason for hiding this comment

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

I think I've checked most if not all the initial review change suggestions. However, it's tricky since GitHub drops some of the original review comments from the diff if it can't work out where to place them now. (In some cases that's quite poor - e..g. lines added then removed - the review comment disappears too.)

One thing I have spotted: if we don't want no_useless_else, we probably also don't want no_superfluous_elseif, for consistency.

'no_superfluous_elseif' => true,
'no_unneeded_control_parentheses' => true,
'no_unneeded_curly_braces' => true,
'no_useless_else' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

On that basis, we also need to remove the no_superfluous_elseif.


// control structure
'elseif' => true,
'no_superfluous_elseif' => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

If we remove no_useless_else, we should also remove no_superfluous_elseif.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@oliverklee oliverklee requested a review from JakeQZ June 30, 2024 15:30
Copy link
Contributor

@JakeQZ JakeQZ left a comment

Choose a reason for hiding this comment

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

Having finalized this for now (it will change in future, I'm sure) here, we should probably update the equivalent on the sister project to match. And like the contrubution guidelines, CoC, and other bits and bobs, keep them in sync.

I wonder if there's a way of using a template to keep cross-project files like these in sync. You mentioned that the CoC came from a GitHub template (I think). Can we introduce our own repository-owner templates to help?

@JakeQZ JakeQZ merged commit a8574c7 into main Jun 30, 2024
23 checks passed
@JakeQZ JakeQZ deleted the task/fixer-rules branch June 30, 2024 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch to the PER2 coding standard and reduce the number of custom rules
2 participants