From aaa38eea3a4055bad0031318e1b4be0189a77ad4 Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Fri, 29 Mar 2024 19:24:45 +0300 Subject: [PATCH 1/5] solution prototype --- src/linter/block_linter.go | 16 ++++++++++++++++ src/linter/report.go | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/src/linter/block_linter.go b/src/linter/block_linter.go index 3f6cbd4f..d82c3ee9 100644 --- a/src/linter/block_linter.go +++ b/src/linter/block_linter.go @@ -22,8 +22,24 @@ type blockLinter struct { quickfix *QuickFixGenerator } +func (b *blockLinter) checkStringInterpolationDeprecated(str *ir.Encapsed) { + for _, item := range str.Parts { + variable, ok := item.(*ir.SimpleVar) + if ok { + if variable.IdentifierTkn.Value[0] != 36 { // 36 is $ + b.report(str, LevelWarning, "stringInterpolationDeprecated", "use {$variable} instead ${variable}}") + break + } + } + } +} + func (b *blockLinter) enterNode(n ir.Node) { switch n := n.(type) { + + case *ir.Encapsed: + b.checkStringInterpolationDeprecated(n) + case *ir.Assign: b.checkAssign(n) diff --git a/src/linter/report.go b/src/linter/report.go index b36363b9..97ad85b7 100644 --- a/src/linter/report.go +++ b/src/linter/report.go @@ -618,6 +618,15 @@ g();`, }`, }, + { + Name: "stringInterpolationDeprecated", + Default: true, + Quickfix: false, + Comment: `Report deprecated string interpolation style`, + Before: `${variable}`, + After: `{$variable}`, + }, + { Name: "misspellName", Default: true, From 479ce1ccc860916b789c6771ede228d6bac3a734 Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Mon, 1 Apr 2024 17:30:26 +0300 Subject: [PATCH 2/5] updated solution --- src/linter/block_linter.go | 26 +++++----- .../checkers/string_interpolation_test.go | 49 +++++++++++++++++++ 2 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 src/tests/checkers/string_interpolation_test.go diff --git a/src/linter/block_linter.go b/src/linter/block_linter.go index d82c3ee9..e929fdc0 100644 --- a/src/linter/block_linter.go +++ b/src/linter/block_linter.go @@ -22,23 +22,11 @@ type blockLinter struct { quickfix *QuickFixGenerator } -func (b *blockLinter) checkStringInterpolationDeprecated(str *ir.Encapsed) { - for _, item := range str.Parts { - variable, ok := item.(*ir.SimpleVar) - if ok { - if variable.IdentifierTkn.Value[0] != 36 { // 36 is $ - b.report(str, LevelWarning, "stringInterpolationDeprecated", "use {$variable} instead ${variable}}") - break - } - } - } -} - func (b *blockLinter) enterNode(n ir.Node) { switch n := n.(type) { case *ir.Encapsed: - b.checkStringInterpolationDeprecated(n) + b.checkStringInterpolationDeprecation(n) case *ir.Assign: b.checkAssign(n) @@ -223,6 +211,18 @@ func (b *blockLinter) enterNode(n ir.Node) { } } +func (b *blockLinter) checkStringInterpolationDeprecation(str *ir.Encapsed) { + for _, item := range str.Parts { + variable, ok := item.(*ir.SimpleVar) + if ok { + if variable.IdentifierTkn.Value[0] != 36 { // 36 is $ + b.report(str, LevelWarning, "stringInterpolationDeprecated", "use {$variable} instead ${variable}") + break + } + } + } +} + func (b *blockLinter) checkUnaryPlus(n *ir.UnaryPlusExpr) { val := constfold.Eval(b.classParseState(), n.Expr) if val.IsValid() { diff --git a/src/tests/checkers/string_interpolation_test.go b/src/tests/checkers/string_interpolation_test.go new file mode 100644 index 00000000..0152fc6f --- /dev/null +++ b/src/tests/checkers/string_interpolation_test.go @@ -0,0 +1,49 @@ +package checkers + +import ( + "github.com/VKCOM/noverify/src/linttest" + "testing" +) + +func TestInterpolationDeprecated1(t *testing.T) { + test := linttest.NewSuite(t) + test.AddFile(` Date: Mon, 1 Apr 2024 17:33:33 +0300 Subject: [PATCH 3/5] updated solution --- src/tests/checkers/string_interpolation_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tests/checkers/string_interpolation_test.go b/src/tests/checkers/string_interpolation_test.go index 0152fc6f..3f895203 100644 --- a/src/tests/checkers/string_interpolation_test.go +++ b/src/tests/checkers/string_interpolation_test.go @@ -1,8 +1,9 @@ package checkers import ( - "github.com/VKCOM/noverify/src/linttest" "testing" + + "github.com/VKCOM/noverify/src/linttest" ) func TestInterpolationDeprecated1(t *testing.T) { From cb8e459d499f3f625c99504bcb1c27dc104a604f Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Mon, 8 Apr 2024 12:40:46 +0300 Subject: [PATCH 4/5] refactoring --- src/linter/block_linter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linter/block_linter.go b/src/linter/block_linter.go index e929fdc0..11e11b2e 100644 --- a/src/linter/block_linter.go +++ b/src/linter/block_linter.go @@ -215,7 +215,7 @@ func (b *blockLinter) checkStringInterpolationDeprecation(str *ir.Encapsed) { for _, item := range str.Parts { variable, ok := item.(*ir.SimpleVar) if ok { - if variable.IdentifierTkn.Value[0] != 36 { // 36 is $ + if variable.IdentifierTkn.Value[0] != '$' { b.report(str, LevelWarning, "stringInterpolationDeprecated", "use {$variable} instead ${variable}") break } From 4e95985e1535d52ecad1d598da223796b151739f Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Tue, 10 Dec 2024 19:25:05 +0300 Subject: [PATCH 5/5] doc update --- docs/checkers_doc.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/docs/checkers_doc.md b/docs/checkers_doc.md index 166416b1..bb31729b 100644 --- a/docs/checkers_doc.md +++ b/docs/checkers_doc.md @@ -4,7 +4,7 @@ | Total checks | Checks enabled by default | Disabled checks by default | Autofixable checks | | ------------ | ------------------------- | -------------------------- | ------------------ | -| 104 | 86 | 18 | 14 | +| 105 | 87 | 18 | 14 | ## Table of contents - Enabled by default @@ -75,6 +75,7 @@ - [`stdInterface` checker](#stdinterface-checker) - [`strangeCast` checker](#strangecast-checker) - [`strictCmp` checker](#strictcmp-checker) + - [`stringInterpolationDeprecated` checker](#stringinterpolationdeprecated-checker) - [`stripTags` checker](#striptags-checker) - [`switchEmpty` checker](#switchempty-checker) - [`switchSimplify` checker](#switchsimplify-checker) @@ -1183,7 +1184,7 @@ interface Iface { #### Description -Report not nullable param can be null. +Report not nullable param with explicit null default value. #### Non-compliant code: ```php @@ -1471,6 +1472,24 @@ in_array("what", $s, true)


+### `stringInterpolationDeprecated` checker + +#### Description + +Report deprecated string interpolation style + +#### Non-compliant code: +```php +${variable} +``` + +#### Compliant code: +```php +{$variable} +``` +


+ + ### `stripTags` checker #### Description