Skip to content

Commit d27640a

Browse files
committed
Blink: port the fixable slevomat PHP sniffs
Ports DisallowDirectMagicInvokeCall, ForbiddenClasses, ReferenceSpacing, RequireExplicitAssertion, RequireNowdoc, ShortList, TypeCast, UselessParentheses and UselessSemicolon, verified on golden outputs generated by ECS from the upstream test fixtures. The shim's braceOwner no longer reaches past a closed block, an object operator or an assignment, so $this->{$x} is not taken for a scope, and it gains PHP_CodeSniffer's scope_condition for trait use blocks.
1 parent eecfc14 commit d27640a

49 files changed

Lines changed: 3214 additions & 9 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ Rules blink runs natively, per source. Regenerate with `blink/bin/rule-counts.sh
3535
| PHP-CS-Fixer | 282 | 282 | 100% |
3636
| Symplify | 34 | 34 | 100% |
3737
| PHP_CodeSniffer | 38 | 114 | 33% |
38-
| slevomat | 28 | 132 | 21% |
39-
| **total** | **382** | **562** | **67%** |
38+
| slevomat | 37 | 132 | 28% |
39+
| **total** | **391** | **562** | **69%** |
4040
<!-- parity-counts:end -->

‎blink/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ those expectations can differ from upstream's `.inc.fixed`. Regenerate them with
2727
| PHP_CodeSniffer | 38 | 114 | 33% |
2828
| **total** | **354** | **430** | **82%** |
2929

30-
blink runs 388 rules in all, its own extras included.
30+
blink runs 397 rules in all, its own extras included.
3131
<!-- rule-counts:end -->
3232

3333
### slevomat sniffs
@@ -50,11 +50,11 @@ measured against every fixable slevomat sniff in `slevomat-fixable-sniffs.txt`
5050
| Namespaces | 0 | 13 | 0% |
5151
| Numbers | 0 | 1 | 0% |
5252
| Operators | 4 | 4 | 100% |
53-
| PHP | 0 | 9 | 0% |
53+
| PHP | 9 | 9 | 100% |
5454
| TypeHints | 0 | 15 | 0% |
5555
| Variables | 0 | 1 | 0% |
5656
| Whitespaces | 0 | 1 | 0% |
57-
| **total** | **28** | **132** | **21%** |
57+
| **total** | **37** | **132** | **28%** |
5858
<!-- slevomat-counts:end -->
5959

6060
## Build

‎blink/internal/fixer/rules/priorities.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,40 @@ var fixerPriority = map[string]int{
9797
"AttributeAndTargetSpacingSniff": 0,
9898
"AttributesOrderSniff": 0,
9999
"DisallowAttributesJoiningSniff": 0,
100+
"DisallowDirectMagicInvokeCallSniff": 0,
100101
"DisallowEqualOperatorsSniff": 0,
101102
"DisallowMultipleAttributesPerLineSniff": 0,
102103
"DisallowTrailingCommaInCallSniff": 0,
103104
"DisallowTrailingCommaInClosureUseSniff": 0,
104105
"DisallowTrailingCommaInDeclarationSniff": 0,
106+
"ForbiddenClassesSniff": 0,
105107
"LogicalOperatorSpacingSniff": 0,
106108
"MultiLineArrayEndBracketPlacementSniff": 0,
107109
"NamedArgumentSpacingSniff": 0,
108110
"NegationOperatorSpacingSniff": 0,
111+
"ReferenceSpacingSniff": 0,
109112
"RequireArrowFunctionSniff": 0,
110113
"RequireAttributeAfterDocCommentSniff": 0,
111114
"RequireCombinedAssignmentOperatorSniff": 0,
115+
"RequireExplicitAssertionSniff": 0,
112116
"RequireMultiLineCallSniff": 0,
117+
"RequireNowdocSniff": 0,
113118
"RequireSingleLineCallSniff": 0,
114119
"RequireTrailingCommaInCallSniff": 0,
115120
"RequireTrailingCommaInClosureUseSniff": 0,
116121
"RequireTrailingCommaInDeclarationSniff": 0,
117122
"SemicolonSpacingSniff": 0,
123+
"ShortListSniff": 0,
118124
"SingleLineArrayWhitespaceSniff": 0,
119125
"SpaceAfterNotSniff": 0,
120126
"SpreadOperatorSpacingSniff": 0,
121127
"StaticClosureSniff": 0,
122128
"TrailingArrayCommaSniff": 0,
129+
"TypeCastSniff": 0,
123130
"UnusedInheritedVariablePassedToClosureSniff": 0,
124131
"UselessParameterDefaultValueSniff": 0,
132+
"UselessParenthesesSniff": 0,
133+
"UselessSemicolonSniff": 0,
125134
// getPriority() is 0 upstream; raised to run before blank_line_after_opening_tag
126135
// so blink reaches the same output in a single pass (PHP-CS-Fixer loops to fixpoint)
127136
"LinebreakAfterOpeningTagFixer": 2,

‎blink/internal/fixer/rules/registry.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,15 @@ func ConstructFixers() []fixer.Fixer {
345345
SlevomatDisallowAttributesJoining{},
346346
SlevomatDisallowMultipleAttributesPerLine{},
347347
SlevomatRequireAttributeAfterDocComment{},
348+
SlevomatDisallowDirectMagicInvokeCall{},
349+
SlevomatForbiddenClasses{},
350+
SlevomatReferenceSpacing{},
351+
SlevomatRequireExplicitAssertion{},
352+
SlevomatRequireNowdoc{},
353+
SlevomatShortList{},
354+
SlevomatTypeCast{},
355+
SlevomatUselessParentheses{},
356+
SlevomatUselessSemicolon{},
348357
OperatorLinebreak{},
349358
PhpdocSeparation{},
350359
PhpdocToComment{},

‎blink/internal/fixer/rules/slevomat.go‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,14 +741,17 @@ func (f *slvFile) enclosingScope(i int) int {
741741
func (f *slvFile) braceOwner(open int) int {
742742
for j := open - 1; j >= 0; j-- {
743743
t := f.toks[j]
744+
if t.kind == token.Variable {
745+
return -1
746+
}
744747
if t.kind == token.Punct {
745748
switch t.orig {
746-
case ")", "]", "}":
749+
case ")", "]":
747750
if t.closer >= 0 {
748751
j = t.closer
749752
continue
750753
}
751-
case ";", "{", "(", "[":
754+
case ";", "{", "}", "(", "[", "->", "?->", "::", "=":
752755
return -1
753756
}
754757
}
@@ -764,6 +767,40 @@ func (f *slvFile) braceOwner(open int) int {
764767
return -1
765768
}
766769

770+
// isTraitUseBlock tells the "{" of a trait use's block.
771+
func (f *slvFile) isTraitUseBlock(open int) bool {
772+
for j := open - 1; j >= 0; j-- {
773+
switch {
774+
case f.isEmpty(j), f.isName(j), f.is(j, ","):
775+
continue
776+
case f.isKeyword(j, "use"):
777+
return true
778+
}
779+
return false
780+
}
781+
return false
782+
}
783+
784+
// scopeCondition is PHP_CodeSniffer's scope_condition: it returns the owner of the brace at i: "use" for a
785+
// trait use block, else the keyword braceOwner finds, or -1.
786+
func (f *slvFile) scopeCondition(i int) int {
787+
open := i
788+
if f.is(i, "}") {
789+
open = f.toks[i].closer
790+
}
791+
if open < 0 {
792+
return -1
793+
}
794+
if f.isTraitUseBlock(open) {
795+
for j := open - 1; j >= 0; j-- {
796+
if f.isKeyword(j, "use") {
797+
return j
798+
}
799+
}
800+
}
801+
return f.braceOwner(open)
802+
}
803+
767804
// isClosure tells a closure's "function" from a named function's.
768805
func (f *slvFile) isClosure(fn int) bool {
769806
n := f.nextEffective(fn+1, len(f.toks))

0 commit comments

Comments
 (0)