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
6 changes: 6 additions & 0 deletions blink/internal/fixer/rules/class_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func (f ClassDefinition) Fix(s *tokens.Stream) bool {
}
continue
}
// a genuine declaration is followed by the type name; a class-like keyword
// followed by "::" or "(" (e.g. "Enum::MODE_ADD") is a name the lexer tagged
// as a keyword, not a declaration
if n := nextSignificantIndex(s, i); n < 0 || s.At(n).Kind != token.Ident {
continue
}
if normalizeHeaderSpacing(s, i) {
changed = true
}
Expand Down
15 changes: 15 additions & 0 deletions blink/internal/fixer/rules/class_definition_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ func TestClassDefinitionSingleLine(t *testing.T) {
}
}

// A class-name reference the lexer tags as a class-like keyword (here "Enum" in
// "Enum::MODE_ADD") is not a declaration, so single_line must not flatten the code
// that follows it.
func TestClassDefinitionIgnoresKeywordNamedReference(t *testing.T) {
t.Parallel()
src := "<?php\nclass Foo\n{\n public function run(): void\n {\n $x = [\n" +
" Enum::MODE_ADD,\n Enum::MODE_EDIT,\n ];\n }\n\n" +
" public function other(): void\n {\n }\n}\n"

cfg := ClassDefinition{}.WithConfig(map[string]any{"single_line": true})
if got, changed := apply(t, cfg, src); changed || got != src {
t.Fatalf("single_line must not touch an Enum:: reference: changed=%v got=%q", changed, got)
}
}

func TestClassDefinitionSpaceBeforeParenthesis(t *testing.T) {
t.Parallel()
src := "<?php $x = new class() {};"
Expand Down
40 changes: 40 additions & 0 deletions blink/internal/runner/net_zero_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package runner

import (
"context"
"os"
"path/filepath"
"testing"

"blink/internal/config"
"blink/internal/fixer/rules"
)

// Two rules that fight to a draw must not be reported as a change: UnaryOperatorSpaces
// (full) strips the space after "!" and NotOperatorWithSuccessorSpace adds it back,
// so the rendered file is identical to the original. PHP-CS-Fixer reports only real
// diffs, so blink must too (otherwise the file shows up with an empty diff).
func TestRunNetZeroNotReported(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "sample.php")
src := "<?php\nif (! $a) {\n echo 1;\n}\n"
if err := os.WriteFile(path, []byte(src), 0o644); err != nil {
t.Fatal(err)
}

cfg := config.Configure().WithPaths(dir).WithRules(
rules.UnaryOperatorSpaces{Full: true},
rules.NotOperatorWithSuccessorSpace{},
)

results, err := Run(context.Background(), cfg, false, nil)
if err != nil {
t.Fatal(err)
}
if len(results) != 0 {
t.Fatalf("net-zero change must not be reported, got %+v", results)
}
if b, _ := os.ReadFile(path); string(b) != src {
t.Fatal("check mode must not modify the file")
}
}
5 changes: 5 additions & 0 deletions blink/internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func fixFile(cfg *config.Config, path string, write bool) (FileResult, error) {
}

res.After = stream.Render()
// Rules that fight to a draw (one edits, another reverts) leave the rendered
// file identical to the original; like PHP-CS-Fixer, report only real diffs.
if res.After == original {
res.AppliedRules = nil
}
if !res.Changed() {
return res, nil
}
Expand Down
Loading