Skip to content

feature: introduce single_space_around_construct, deprecate single_space_after_construct#6857

Merged
keradus merged 8 commits intoPHP-CS-Fixer:masterfrom
keradus:single_space_around_construct
Mar 29, 2023
Merged

feature: introduce single_space_around_construct, deprecate single_space_after_construct#6857
keradus merged 8 commits intoPHP-CS-Fixer:masterfrom
keradus:single_space_around_construct

Conversation

@keradus
Copy link
Copy Markdown
Member

@keradus keradus commented Mar 24, 2023

I aimed to solve first TODO item from #4885
I limited to needed changes, I was not redoing the logic of old fixer.

@keradus keradus marked this pull request as ready for review March 24, 2023 23:20
@keradus keradus mentioned this pull request Mar 24, 2023
2 tasks
@coveralls
Copy link
Copy Markdown

coveralls commented Mar 24, 2023

Pull Request Test Coverage Report for Build 4516272913

  • 183 of 188 (97.34%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.008%) to 93.002%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixer.php 11 12 91.67%
src/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixer.php 171 175 97.71%
Totals Coverage Status
Change from base Build 4515154257: 0.008%
Covered Lines: 22711
Relevant Lines: 24420

💛 - Coveralls

* @author Andreas Möller <am@localheinz.com>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*/
final class SingleSpaceAroundConstructFixer extends AbstractFixer implements ConfigurableFixerInterface
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

maybe we should call it SpacingAroundConstructFixer, so in future we would be open for no whitespace before/after given construct, or multiple ones? (eg multi-line instead of single space, for specific cases) ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good idea, but probably properties' names should be aligned too ($****SingleSpace). Also FixerDefinition must be changed to not explicitly state "single space" as the only way. In general, a lot to change 😅.

Copy link
Copy Markdown
Member

@Wirone Wirone left a comment

Choose a reason for hiding this comment

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

Few thoughts, it's hard to review this PR on mobile, so I don't know if I missed something or not 😅.


/**
* @var array<string, int>
* {@inheritdoc}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Side note, nothing to be done: I find such phpDocs with only {@inheritdoc} superfluous.

Copy link
Copy Markdown
Member Author

@keradus keradus Mar 25, 2023

Choose a reason for hiding this comment

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

to be considered as practice to use or not to use them for whole project. probably a fixer, if we want to remove them.

*/
private static array $tokenMapContainASingleSpace = [
// for now, only one case - but we are ready to extend it, when we learn about new cases to cover
'yield_from' => T_YIELD_FROM,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't const_import and function_import be here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

no. they do not contain whitespace.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

They do? Is it for use function, use const? 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yield from is ONE token, will be fixed by contain option

use function are THREE tokens, will be fixed by preceded / followed

ref https://3v4l.org/mW39A

* @author Andreas Möller <am@localheinz.com>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*/
final class SingleSpaceAroundConstructFixer extends AbstractFixer implements ConfigurableFixerInterface
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good idea, but probably properties' names should be aligned too ($****SingleSpace). Also FixerDefinition must be changed to not explicitly state "single space" as the only way. In general, a lot to change 😅.

}
}

$this->fixTokenMapPrecededByASingleSpace = [];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Property has [] as default value, I believe it's not needed to assign it here and we can just iterate over configuration array?

Same for other properties below.

If it's just for safety (calling configure() multiple times), then 👍.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes. it's part of public interface and one can call it any moment. we do it like this for any rule.


$whitespaceTokenIndex = $index + 1;

if ($tokens[$whitespaceTokenIndex]->equalsAny([',', ';', ')', [CT::T_ARRAY_SQUARE_BRACE_CLOSE], [CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE]])) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we extract all these conditions that do continue to separate method and call it here for better readability?

Copy link
Copy Markdown
Member Author

@keradus keradus Mar 25, 2023

Choose a reason for hiding this comment

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

copy-pasted from SingleSpaceAfterConstructFixer. goal of this PR is not refactoring of existing logic, but to make final step to deprecate BracesFixer at lowest remaining effort, as described in opening msg. If you up for refactoring, please raise a (sub?) PR

I limited to needed changes, I was not redoing the logic of old fixer.

return false;
}

private function isMultilineExtendsOrImplementsWithMoreThanOneAncestor(Tokens $tokens, int $index): bool
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like something that may be helpful in other fixers, so maybe we can extract it somewhere? Same with isMultilineConstant().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

same as #6857 (comment)

@@ -491,6 +491,7 @@ private static function getFixersPriorityGraph(): array
'not_operator_with_successor_space',
'php_unit_dedicate_assert',
'single_space_after_construct',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't it a part of single_space_around_construct?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

as long as both fixers exist, both should be tested. including the priority of them.

extra points for our self-tests to detect if it's missing and fail another test in such case.

/**
* @var array<string, null|int>
*/
private static array $tokenMapContainASingleSpace = [
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These properties are used statically only in configure() which is not static, what is the advantage of having them defined like that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

another $tokenMap that was already existing is declared static in master branch, moved it as-is and kept the pattern.
I guess the initial reason was that they are same for any instance of this class.

@keradus keradus merged commit e04eef3 into PHP-CS-Fixer:master Mar 29, 2023
@keradus keradus deleted the single_space_around_construct branch March 29, 2023 22:12
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.

3 participants