Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ Rules blink runs natively, per source. Regenerate with `blink/bin/rule-counts.sh
| PHP-CS-Fixer | 282 | 282 | 100% |
| Symplify | 34 | 34 | 100% |
| PHP_CodeSniffer | 114 | 114 | 100% |
| slevomat | 37 | 132 | 28% |
| **total** | **467** | **562** | **83%** |
| slevomat | 50 | 132 | 37% |
| **total** | **480** | **562** | **85%** |
<!-- parity-counts:end -->
6 changes: 3 additions & 3 deletions blink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ those expectations can differ from upstream's `.inc.fixed`. Regenerate them with
| PHP_CodeSniffer | 114 | 114 | 100% |
| **total** | **430** | **430** | **100%** |

blink runs 473 rules in all, its own extras included.
blink runs 486 rules in all, its own extras included.
<!-- rule-counts:end -->

### slevomat sniffs
Expand All @@ -46,14 +46,14 @@ measured against every fixable slevomat sniff in `slevomat-fixable-sniffs.txt`
| ControlStructures | 0 | 21 | 0% |
| Exceptions | 0 | 3 | 0% |
| Functions | 14 | 14 | 100% |
| Namespaces | 0 | 13 | 0% |
| Namespaces | 13 | 13 | 100% |
| Numbers | 0 | 1 | 0% |
| Operators | 4 | 4 | 100% |
| PHP | 9 | 9 | 100% |
| TypeHints | 0 | 15 | 0% |
| Variables | 0 | 1 | 0% |
| Whitespaces | 0 | 1 | 0% |
| **total** | **37** | **132** | **28%** |
| **total** | **50** | **132** | **37%** |
<!-- slevomat-counts:end -->

## Build
Expand Down
12 changes: 12 additions & 0 deletions blink/internal/fixer/rules/priorities.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ var fixerPriority = map[string]int{
"OpeningBraceSpaceSniff": 0,

// slevomat sniffs
"ReferenceUsedNamesOnlySniff": 0,
"UnusedUsesSniff": 0,
"FullyQualifiedClassNameInAnnotationSniff": 0,
"FullyQualifiedGlobalFunctionsSniff": 0,
"FullyQualifiedGlobalConstantsSniff": 0,
"FullyQualifiedExceptionsSniff": 0,
"UselessAliasSniff": 0,
"UseSpacingSniff": 0,
"UseFromSameNamespaceSniff": 0,
"UseDoesNotStartWithBackslashSniff": 0,
"NamespaceSpacingSniff": 0,
"AlphabeticallySortedUsesSniff": 0,
"AlphabeticallySortedByKeysSniff": 0,
"ArrayAccessSniff": 0,
"ArrayBracketSpacingSniff": 0,
Expand Down
13 changes: 13 additions & 0 deletions blink/internal/fixer/rules/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,19 @@ func ConstructFixers() []fixer.Fixer {
SlevomatTypeCast{},
SlevomatUselessParentheses{},
SlevomatUselessSemicolon{},
SlevomatAlphabeticallySortedUses{},
SlevomatFullyQualifiedClassNameInAnnotation{},
SlevomatFullyQualifiedExceptions{},
SlevomatFullyQualifiedGlobalConstants{},
SlevomatFullyQualifiedGlobalFunctions{},
SlevomatNamespaceDeclaration{},
SlevomatNamespaceSpacing{},
SlevomatReferenceUsedNamesOnly{},
SlevomatUnusedUses{},
SlevomatUseDoesNotStartWithBackslash{},
SlevomatUseFromSameNamespace{},
SlevomatUseSpacing{},
SlevomatUselessAlias{},
OperatorLinebreak{},
PhpdocSeparation{},
PhpdocToComment{},
Expand Down
25 changes: 13 additions & 12 deletions blink/internal/fixer/rules/slevomat.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,33 @@ type slvHistory struct {
loop int
}

const slvMaxLoops = 50
const slvMaxLoops = 100

// slvRun runs process over the stream's PHP_CodeSniffer file until a pass fixes
// nothing, relexing the stream after every pass that did.
func slvRun(s *tokens.Stream, process func(f *slvFile)) bool {
changed := false
src := s.Render()
original := s.Render()
src := original
history := map[int]*slvHistory{}
for loop := range slvMaxLoops {
f := newSlvFile(lexer.Lex(src))
f.loop, f.history = loop, history
process(f)
if f.numFixes == 0 {
break
next := src
if f.numFixes > 0 {
next = f.contents()
}
next := f.contents()
if next == src {
break
if src == original {
return false
}
s.ReplaceRange(0, s.Len()-1, lexer.Lex(src))
return true
}
src = next
changed = true
}
if changed {
s.ReplaceRange(0, s.Len()-1, lexer.Lex(src))
}
return changed
// ECS gives up on a file whose fixes never settle and leaves it unchanged
return false
}

// slvEachToken calls process for every token match accepts, skipping the tokens
Expand Down
18 changes: 16 additions & 2 deletions blink/internal/fixer/rules/slevomat_functions_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,26 @@ func (f *slvFile) lineCallIsName(i int) bool {
if t.kind != token.Ident {
return false
}
if !slvlNotNames[strings.ToLower(t.orig)] {
lc := strings.ToLower(t.orig)
switch lc {
case `\true`, `\false`, `\null`, `\exit`, `\die`:
// PHP_CodeSniffer keeps these keyword tokens even when fully qualified
return false
}
if !slvlNotNames[lc] {
return true
}
// a member or declared name is T_STRING whatever it is called ($o->eval())
p := f.prevEffective(i - 1)
return f.is(p, "->") || f.is(p, "?->") || f.is(p, "::") || f.isKeyword(p, "function") || f.isKeyword(p, "const")
if f.is(p, "->") || f.is(p, "?->") || f.is(p, "::") || f.isKeyword(p, "function") || f.isKeyword(p, "const") {
return true
}
switch lc {
case "true", "false", "null":
next := f.nextEffective(i+1, len(f.toks))
return f.is(next, "(") || f.is(next, "::")
}
return false
}

// lineCallIsPlainString tells a T_STRING from a qualified name.
Expand Down
145 changes: 145 additions & 0 deletions blink/internal/fixer/rules/slevomat_namespaces_annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package rules

import (
"slices"
"strings"

"github.com/ecsphp/ecs-src/blink/internal/fixer"
"github.com/ecsphp/ecs-src/blink/internal/lexer"
"github.com/ecsphp/ecs-src/blink/internal/tokens"
)

// Slevomat: https://github.com/slevomat/coding-standard/blob/master/SlevomatCodingStandard/Sniffs/Namespaces/FullyQualifiedClassNameInAnnotationSniff.php

// SlevomatFullyQualifiedClassNameInAnnotation writes the class and constant
// names in annotations fully qualified.
type SlevomatFullyQualifiedClassNameInAnnotation struct {
ignoredAnnotationNames []string
}

func (SlevomatFullyQualifiedClassNameInAnnotation) Name() string {
return `SlevomatCodingStandard\Sniffs\Namespaces\FullyQualifiedClassNameInAnnotationSniff`
}

func (SlevomatFullyQualifiedClassNameInAnnotation) SourceURL() string {
return fixer.SourceURLFor(SlevomatFullyQualifiedClassNameInAnnotation{}.Name())
}

func (r SlevomatFullyQualifiedClassNameInAnnotation) WithConfig(config map[string]any) fixer.Fixer {
r.ignoredAnnotationNames = nil
if names, ok := config["ignoredAnnotationNames"].([]any); ok {
for _, n := range names {
if s, ok := n.(string); ok && strings.TrimSpace(s) != "" {
r.ignoredAnnotationNames = append(r.ignoredAnnotationNames, strings.TrimSpace(s))
}
}
}
return r
}

func (r SlevomatFullyQualifiedClassNameInAnnotation) Fix(s *tokens.Stream) bool {
aborted := false
src := s.Render()
changed := slvRun(s, func(f *slvFile) {
if aborted {
return
}
d := newSlvDocs(f)
f.eachToken(f.isDocOpen, func(open int) int {
if !aborted && !r.process(d, open) {
aborted = true
}
return 0
})
})
if aborted && changed {
s.ReplaceRange(0, s.Len()-1, lexer.Lex(src))
return false
}
return changed
}

// process fixes one doc comment; it is false where upstream throws.
func (r SlevomatFullyQualifiedClassNameInAnnotation) process(d *slvDocs, open int) bool {
for _, a := range d.annotations(open, "") {
identifiers := pdNodesByType(a.node, "IdentifierTypeNode")
if slices.Contains(r.ignoredAnnotationNames, a.name) {
continue
}
for _, n := range identifiers {
name := n.name
lower := strings.ToLower(name)
if slvTypeHintIsSimple(lower) || slvTypeHintIsSimpleUnofficial(lower) || !slvTypeHintIsTypeName(name) ||
d.isTypeDefinedInAnnotation(open, name) {
continue
}
fq := d.useStatements().resolveClassName(d.f, name, open)
if fq == name {
continue
}
if !d.fix(open, n, fq) {
return false
}
}
for _, n := range pdNodesByType(a.node, "ConstFetchNode") {
isClassConstant := n.className != ""
name := n.name
if isClassConstant {
name = n.className
}
if name == "self" {
continue
}
var fq string
if isClassConstant {
fq = d.useStatements().resolveClassName(d.f, name, open)
} else {
fq = d.resolveConstantName(name, open)
}
if fq == name {
continue
}
replacement := fq
if isClassConstant {
replacement = fq + "::" + n.name
}
if !d.fix(open, n, replacement) {
return false
}
}
}
return true
}

var slvTypeHintSimpleList = []string{"int", "integer", "false", "float", "string", "bool", "boolean",
"callable", "self", "array", "iterable", "void", "never"}

// slvTypeHintIsSimple is TypeHintHelper::isSimpleTypeHint.
func slvTypeHintIsSimple(t string) bool { return slices.Contains(slvTypeHintSimpleList, t) }

var slvTypeHintUnofficialList = []string{"null", "mixed", "scalar", "numeric", "true", "object",
"resource", "static", "$this", "array-key", "list", "non-empty-array", "non-empty-list", "empty",
"positive-int", "non-positive-int", "negative-int", "non-negative-int", "literal-int", "int-mask",
"min", "max", "callable-array", "callable-string"}

// slvTypeHintIsSimpleUnofficial is TypeHintHelper::isSimpleUnofficialTypeHints.
func slvTypeHintIsSimpleUnofficial(t string) bool {
return slices.Contains(slvTypeHintUnofficialList, t) || strings.HasSuffix(strings.ToLower(t), "-string")
}

// slvTypeHintIsTypeName is TypeHelper::isTypeName: backslash separated PHP names,
// optionally fully qualified.
func slvTypeHintIsTypeName(name string) bool {
name = strings.TrimPrefix(name, `\`)
for part := range strings.SplitSeq(name, `\`) {
if part == "" || pdIsDigit(part[0]) {
return false
}
for i := range len(part) {
if c := part[i]; !pdIsLetter(c) && !pdIsDigit(c) && c != '_' && c < 0x7f {
return false
}
}
}
return true
}
Loading
Loading