Skip to content

Commit 287c95d

Browse files
committed
new rule about absence of the declare section, test, updated .gitignore
1 parent 326d6be commit 287c95d

File tree

6 files changed

+45
-0
lines changed

6 files changed

+45
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ src/cmd/stubs/phpstorm-stubs/
66
y.output
77
.idea
88
vendor
9+
src/tests/golden/testdata/quickfix/*.fix

src/linter/block.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ func (b *blockWalker) EnterNode(n ir.Node) (res bool) {
378378
for _, st := range s.Stmts {
379379
b.addStatement(st)
380380
}
381+
b.handleDeclareSection(s)
381382
case *ir.StmtList:
382383
for _, st := range s.Stmts {
383384
b.addStatement(st)
@@ -551,6 +552,23 @@ func (b *blockWalker) handleAndCheckGlobalStmt(s *ir.GlobalStmt) {
551552
}
552553
}
553554

555+
func (b *blockWalker) handleDeclareSection(root *ir.Root) {
556+
isExist := false
557+
558+
for _, stmt := range root.Stmts {
559+
_, ok := stmt.(*ir.DeclareStmt)
560+
if ok {
561+
isExist = true
562+
break
563+
}
564+
}
565+
566+
if !isExist {
567+
b.report(root, LevelWarning, "noDeclareSection", "Missed declare(strict_types = 1) directive")
568+
b.r.addQuickFix("noDeclareSection", b.linter.quickfix.CreateDeclareStrictTypes(root))
569+
}
570+
}
571+
554572
func (b *blockWalker) handleFunction(fun *ir.FunctionStmt) bool {
555573
if b.ignoreFunctionBodies {
556574
return false

src/linter/quickfix.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ func (g *QuickFixGenerator) NullForNotNullableProperty(prop *ir.PropertyStmt) qu
4747
}
4848
}
4949

50+
func (g *QuickFixGenerator) CreateDeclareStrictTypes(root *ir.Root) quickfix.TextEdit {
51+
return quickfix.TextEdit{
52+
StartPos: root.Position.StartPos,
53+
EndPos: root.Position.StartPos,
54+
Replacement: "declare(strict_types = 1);\n",
55+
}
56+
}
57+
5058
func (g *QuickFixGenerator) GetType(node ir.Node, isFunctionName, nodeText string, isNegative bool) quickfix.TextEdit {
5159
pos := ir.GetPosition(node)
5260

src/linter/report.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ func addBuiltinCheckers(reg *CheckersRegistry) {
2525
After: `$s = strip_tags($s, '<br>')`,
2626
},
2727

28+
{
29+
Name: "noDeclareSection",
30+
Default: true,
31+
Quickfix: true,
32+
Comment: "Report declare(strict_types=1) has not been set.",
33+
Before: ` `,
34+
After: `declare(strict_types = 1);`,
35+
},
36+
2837
{
2938
Name: "emptyStmt",
3039
Default: true,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
function test(): string {
3+
return "test";
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
declare(strict_types = 1);
3+
function test(): string {
4+
return "test";
5+
}

0 commit comments

Comments
 (0)