Skip to content

Commit

Permalink
linter: new rule about deprecated string interpolation (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio authored Dec 10, 2024
1 parent ea9cc3f commit 2d18e46
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
23 changes: 21 additions & 2 deletions docs/checkers_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1471,6 +1472,24 @@ in_array("what", $s, true)
<p><br></p>


### `stringInterpolationDeprecated` checker

#### Description

Report deprecated string interpolation style

#### Non-compliant code:
```php
${variable}
```

#### Compliant code:
```php
{$variable}
```
<p><br></p>


### `stripTags` checker

#### Description
Expand Down
16 changes: 16 additions & 0 deletions src/linter/block_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type blockLinter struct {

func (b *blockLinter) enterNode(n ir.Node) {
switch n := n.(type) {

case *ir.Encapsed:
b.checkStringInterpolationDeprecation(n)

case *ir.Assign:
b.checkAssign(n)

Expand Down Expand Up @@ -207,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] != '$' {
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() {
Expand Down
9 changes: 9 additions & 0 deletions src/linter/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
50 changes: 50 additions & 0 deletions src/tests/checkers/string_interpolation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package checkers

import (
"testing"

"github.com/VKCOM/noverify/src/linttest"
)

func TestInterpolationDeprecated1(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
declare(strict_types = 1);
$name = 'PHP';
echo "Hello ${name}";
`)
test.Expect = []string{
`stringInterpolationDeprecated: use {$variable} instead ${variable}`,
}
test.RunAndMatch()
}

func TestInterpolationDeprecated2(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
declare(strict_types = 1);
$name = 'PHP';
echo "Hello {$name}";
`)
test.RunAndMatch()
}

func TestInterpolationDeprecated3(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
declare(strict_types = 1);
$name = 'PHP';
$lang = 'language';
echo "Hello {$name} ${lang}";
`)
test.Expect = []string{
`stringInterpolationDeprecated: use {$variable} instead ${variable}`,
}
test.RunAndMatch()
}

0 comments on commit 2d18e46

Please sign in to comment.