feature: introduce single_space_around_construct, deprecate single_space_after_construct#6857
Conversation
…ion was added later and we didn't want to break BC by changing default option value
Pull Request Test Coverage Report for Build 4516272913
💛 - Coveralls |
| * @author Andreas Möller <am@localheinz.com> | ||
| * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
| */ | ||
| final class SingleSpaceAroundConstructFixer extends AbstractFixer implements ConfigurableFixerInterface |
There was a problem hiding this comment.
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) ?
There was a problem hiding this comment.
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 😅.
Wirone
left a comment
There was a problem hiding this comment.
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} |
There was a problem hiding this comment.
Side note, nothing to be done: I find such phpDocs with only {@inheritdoc} superfluous.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Shouldn't const_import and function_import be here?
There was a problem hiding this comment.
no. they do not contain whitespace.
There was a problem hiding this comment.
They do? Is it for use function, use const? 🤔
There was a problem hiding this comment.
yield from is ONE token, will be fixed by contain option
use function are THREE tokens, will be fixed by preceded / followed
| * @author Andreas Möller <am@localheinz.com> | ||
| * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
| */ | ||
| final class SingleSpaceAroundConstructFixer extends AbstractFixer implements ConfigurableFixerInterface |
There was a problem hiding this comment.
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 = []; |
There was a problem hiding this comment.
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 👍.
There was a problem hiding this comment.
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]])) { |
There was a problem hiding this comment.
Can we extract all these conditions that do continue to separate method and call it here for better readability?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
This looks like something that may be helpful in other fixers, so maybe we can extract it somewhere? Same with isMultilineConstant().
| @@ -491,6 +491,7 @@ private static function getFixersPriorityGraph(): array | |||
| 'not_operator_with_successor_space', | |||
| 'php_unit_dedicate_assert', | |||
| 'single_space_after_construct', | |||
There was a problem hiding this comment.
Isn't it a part of single_space_around_construct?
There was a problem hiding this comment.
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 = [ |
There was a problem hiding this comment.
These properties are used statically only in configure() which is not static, what is the advantage of having them defined like that?
There was a problem hiding this comment.
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.
I aimed to solve first TODO item from #4885
I limited to needed changes, I was not redoing the logic of old fixer.