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 @@ -34,7 +34,7 @@ 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 | 86 | 114 | 75% |
| PHP_CodeSniffer | 101 | 114 | 88% |
| slevomat | 37 | 132 | 28% |
| **total** | **439** | **562** | **78%** |
| **total** | **454** | **562** | **80%** |
<!-- parity-counts:end -->
6 changes: 3 additions & 3 deletions blink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ those expectations can differ from upstream's `.inc.fixed`. Regenerate them with
|---|---:|---:|---:|
| PHP-CS-Fixer | 282 | 282 | 100% |
| Symplify | 34 | 34 | 100% |
| PHP_CodeSniffer | 86 | 114 | 75% |
| **total** | **402** | **430** | **93%** |
| PHP_CodeSniffer | 101 | 114 | 88% |
| **total** | **417** | **430** | **96%** |

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

### slevomat sniffs
Expand Down
23 changes: 23 additions & 0 deletions blink/bin/php-builtin-functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# Regenerates internal/fixer/rules/php_builtin_functions.txt: the lowercased
# names of PHP's built-in functions, which LowercasePHPFunctionsSniff reads from
# get_defined_functions() at runtime. Lists every loaded extension except the
# non-bundled PECL ones, so the result does not depend on a developer's setup.
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
php -r '
$skip = ["xdebug", "mongodb", "rdkafka", "mailparse", "pcov", "imap", "apcu", "redis", "memcached", "imagick", "swoole", "grpc", "protobuf", "yaml", "igbinary", "msgpack", "amqp", "xhprof", "ast", "uopz"];
$names = [];
foreach (get_loaded_extensions() as $ext) {
if (in_array(strtolower($ext), $skip, true)) {
continue;
}
foreach (get_extension_funcs($ext) ?: [] as $fn) {
$names[strtolower($fn)] = true;
}
}
ksort($names, SORT_STRING);
echo implode("\n", array_keys($names)), "\n";
' > "$ROOT/internal/fixer/rules/php_builtin_functions.txt"
echo "Wrote $(wc -l < "$ROOT/internal/fixer/rules/php_builtin_functions.txt") functions"
2 changes: 2 additions & 0 deletions blink/bin/phpcs-fixtures.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#
# Covers only the sniffs blink implements; rerun after porting a sniff.
# An input whose ECS output equals it gets no .fixed file.
# Squiz FileComment fills a missing copyright year from the clock: after
# regenerating in a new year, bump the year pinned in file_comment_clock_test.go.
set -euo pipefail

UPSTREAM="${1:?usage: bin/phpcs-fixtures.sh <PHP_CodeSniffer checkout>}/src/Standards"
Expand Down
150 changes: 150 additions & 0 deletions blink/internal/fixer/rules/concatenation_spacing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package rules

import (
"strings"

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

// PHP_CodeSniffer: https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/4.x/src/Standards/Squiz/Sniffs/Strings/ConcatenationSpacingSniff.php
//
// ConcatenationSpacing puts exactly Spacing spaces (default 0) on both sides of
// the concat operator: "'a' . $b" -> "'a'.$b". With Spacing 0 a number next to
// the removed space is wrapped, "1 . 'x'" -> "(1).'x'", so it cannot read as a
// float. The space after a heredoc closer is kept; with IgnoreNewlines a line
// break around the operator is too.
type ConcatenationSpacing struct {
Spacing int
IgnoreNewlines bool
}

func (ConcatenationSpacing) Name() string {
return `PHP_CodeSniffer\Standards\Squiz\Sniffs\Strings\ConcatenationSpacingSniff`
}

func (ConcatenationSpacing) SourceURL() string {
return "https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/4.x/src/Standards/Squiz/Sniffs/Strings/ConcatenationSpacingSniff.php"
}

func (f ConcatenationSpacing) WithConfig(config map[string]any) fixer.Fixer {
f.Spacing = configInt(config, "spacing", f.Spacing)
if v, ok := config["ignoreNewlines"].(bool); ok {
f.IgnoreNewlines = v
}
return f
}

// concatNewline is the spacing measured on a side of a concat that crosses a line.
const concatNewline = -1

func (f ConcatenationSpacing) Fix(s *tokens.Stream) bool {
return phpcsRunUntilStable(s, f.pass)
}

func (f ConcatenationSpacing) pass(s *tokens.Stream) bool {
changed := false
for i := 0; i < s.Len(); i++ {
if t := s.At(i); t.Kind != token.Punct || t.Value != "." {
continue
}
// live coding: PHP_CodeSniffer needs two tokens after the operator
if i+2 >= s.Len() {
continue
}
prev := phpcsPrevNonEmpty(s, i-1)
ignoreBefore := prev >= 0 && isHeredocEnd(s, prev)

before := f.concatSideSpace(s, i-1)
after := f.concatSideSpace(s, i+1)
if (ignoreBefore || f.concatSpaceOK(before)) && f.concatSpaceOK(after) {
continue
}
padding := strings.Repeat(" ", f.Spacing)
// after first, so i stays valid
if after != concatNewline || !f.IgnoreNewlines {
if f.setConcatSpace(s, i+1, i+2, padding) {
changed = true
}
}
if !ignoreBefore && (before != concatNewline || !f.IgnoreNewlines) {
if removed, did := f.setConcatSpaceBefore(s, i, padding); did {
changed = true
i -= removed
}
}
}
return changed
}

func (f ConcatenationSpacing) concatSpaceOK(n int) bool {
return n == f.Spacing || (n == concatNewline && f.IgnoreNewlines)
}

// concatSideSpace measures the whitespace at w the way PHP_CodeSniffer does:
// 0 when there is none, concatNewline when it crosses a line, else its width.
func (ConcatenationSpacing) concatSideSpace(s *tokens.Stream, w int) int {
if !phpcsWhitespaceAt(s, w) {
return 0
}
if strings.ContainsAny(s.At(w).Value, "\r\n") {
return concatNewline
}
return phpcsTabWidth(s, w)
}

// setConcatSpace makes the whitespace at w (after the operator) padding, and
// wraps a number right after it when the space goes.
func (f ConcatenationSpacing) setConcatSpace(s *tokens.Stream, w, operand int, padding string) bool {
if !phpcsWhitespaceAt(s, w) {
if f.Spacing > 0 {
s.InsertAt(w, token.Token{Kind: token.Whitespace, Value: padding})
return true
}
return false
}
if padding != "" {
s.SetValue(w, padding)
} else {
s.RemoveAt(w)
operand--
}
if f.Spacing == 0 && operand < s.Len() {
wrapConcatNumber(s, operand)
}
return true
}

// setConcatSpaceBefore makes the whitespace before the operator at op padding,
// keeping a line comment's newline. It returns how many tokens were removed.
func (f ConcatenationSpacing) setConcatSpaceBefore(s *tokens.Stream, op int, padding string) (int, bool) {
w := op - 1
if !phpcsWhitespaceAt(s, w) {
if f.Spacing > 0 {
s.InsertAt(op, token.Token{Kind: token.Whitespace, Value: padding})
return -1, true
}
return 0, false
}
removed := 0
switch {
case padding != "":
keep := phpcsCommentNewline(s, w)
s.SetValue(w, s.At(w).Value[:keep]+padding)
case dropPhpcsWhitespace(s, w):
removed = 1
}
if f.Spacing == 0 && removed == 1 && w-1 >= 0 {
wrapConcatNumber(s, w-1)
}
return removed, true
}

// wrapConcatNumber parenthesizes the number at i once; a number between two
// concats is wrapped by the first, like PHP_CodeSniffer's conflicting changesets.
func wrapConcatNumber(s *tokens.Stream, i int) {
if t := s.At(i); t.Kind == token.Number && !strings.HasPrefix(t.Value, "(") {
s.SetValue(i, "("+t.Value+")")
}
}
107 changes: 107 additions & 0 deletions blink/internal/fixer/rules/file_comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package rules

import (
"regexp"
"strconv"
"strings"
"time"

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

// PHP_CodeSniffer: https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/4.x/src/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php
//
// SquizFileComment rewrites the @author and @copyright tags of the file
// docblock to Squiz's own; a copyright keeps its leading year range, or gets
// the current year.
type SquizFileComment struct{}

func (SquizFileComment) Name() string {
return `PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FileCommentSniff`
}

func (SquizFileComment) SourceURL() string {
return "https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/4.x/src/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php"
}

const (
squizAuthor = "Squiz Pty Ltd <products@squiz.net>"
squizCopyright = "Squiz Pty Ltd (ABN 77 084 670 600)"
)

var (
squizCopyrightOK = regexp.MustCompile(`^([0-9]{4})(-[0-9]{4})? (Squiz Pty Ltd \(ABN 77 084 670 600\))$`)
squizCopyrightYear = regexp.MustCompile(`^([0-9]{4}(-[0-9]{4})?)`)
)

// fileCommentNow is the clock the sniff fills a missing copyright year from; tests pin it.
var fileCommentNow = time.Now

var fileCommentIgnore = map[string]bool{
"class": true, "interface": true, "trait": true, "enum": true, "function": true,
"public": true, "private": true, "protected": true, "final": true, "static": true,
"abstract": true, "readonly": true, "const": true, "include": true,
"include_once": true, "require": true, "require_once": true,
}

func (SquizFileComment) Fix(s *tokens.Stream) bool {
open := -1
for i := range s.Len() {
if t := s.At(i); t.Kind == token.OpenTag && !strings.HasPrefix(t.Value, "<?=") {
open = i
break
}
}
if open < 0 {
return false
}
c := open + 1
for c < s.Len() && s.At(c).Kind == token.Whitespace {
c++
}
if c >= s.Len() || s.At(c).Kind != token.DocComment || !strings.HasSuffix(s.At(c).Value, "*/") {
return false
}
next := c + 1
for next < s.Len() {
if s.At(next).Kind == token.Whitespace {
next++
continue
}
if e := s.AttributeEnd(next); s.At(next).Kind == token.AttributeStart && e >= 0 {
next = e + 1
continue
}
break
}
if next < s.Len() && s.At(next).Kind == token.Keyword && fileCommentIgnore[strings.ToLower(s.At(next).Value)] {
return false
}
doc := s.At(c).Value
repl := map[int][2]string{}
for _, tag := range phpcsDocTags(doc, phpcsEolChar(s)) {
if tag.StrPos < 0 {
continue
}
switch tag.Name {
case "@author":
if tag.Str != squizAuthor {
repl[tag.StrPos] = [2]string{tag.Str, squizAuthor}
}
case "@copyright":
if !squizCopyrightOK.MatchString(tag.Str) {
year := squizCopyrightYear.FindString(tag.Str)
if year == "" {
year = strconv.Itoa(fileCommentNow().Year())
}
repl[tag.StrPos] = [2]string{tag.Str, year + " " + squizCopyright}
}
}
}
if len(repl) == 0 {
return false
}
s.SetValue(c, replaceDocStrings(doc, repl))
return true
}
9 changes: 9 additions & 0 deletions blink/internal/fixer/rules/file_comment_clock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package rules

import "time"

// the FileComment fixtures embed the year ECS generated them in; regenerating
// them with bin/phpcs-fixtures.sh in a later year means bumping this one too
func init() {
fileCommentNow = func() time.Time { return time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC) }
}
Loading
Loading