Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linter: deprecated support classes fields #1220

Merged
merged 4 commits into from
Dec 19, 2024
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
11 changes: 9 additions & 2 deletions src/linter/block_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,17 @@ func (b *blockLinter) checkClass(class *ir.ClassStmt) {

var members = make([]int, 0, len(class.Stmts))
for _, stmt := range class.Stmts {
switch stmt := stmt.(type) {
switch value := stmt.(type) {
case *ir.ClassMethodStmt:
members = append(members, classMethod)
b.walker.CheckParamNullability(stmt.Params)
b.walker.CheckParamNullability(value.Params)
case *ir.PropertyListStmt:
for _, element := range value.Doc.Parsed {
if element.Name() == "deprecated" {
b.report(stmt, LevelNotice, "deprecated", "Has deprecated field in class %s", class.ClassName.Value)
}
}
members = append(members, classOtherMember)
default:
members = append(members, classOtherMember)
}
Expand Down
1 change: 1 addition & 0 deletions src/linter/phpdoc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type classPHPDocParseResult struct {
mixins []string
packageName string
internal bool
deprecated bool
}

func parseClassPHPDocMethod(classNode ir.Node, ctx *rootContext, result *classPHPDocParseResult, part *phpdoc.RawCommentPart) {
Expand Down
18 changes: 12 additions & 6 deletions src/linter/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ func (d *rootWalker) EnterNode(n ir.Node) (res bool) {
d.reportPHPDocErrors(doc.errs)
d.handleClassDoc(doc, &cl)

if doc.deprecated {
d.Report(n, LevelNotice, "deprecated", "Has deprecated class %s", n.ClassName.Value)
}

d.meta.Classes.Set(d.ctx.st.CurrentClass, cl)

case *ir.TraitStmt:
Expand Down Expand Up @@ -814,23 +818,25 @@ func (d *rootWalker) parseClassPHPDoc(class ir.Node, doc phpdoc.Comment) classPH
return result
}

// TODO: allocate maps lazily.
// Class may not have any @property or @method annotations.
// In that case we can handle avoid map allocations.
result.properties = make(meta.PropertiesMap)
result.methods = meta.NewFunctionsMap()

for _, part := range doc.Parsed {
d.checker.checkPHPDocRef(class, part)
switch part.Name() {
case "property", "property-read", "property-write":
if result.properties == nil {
result.properties = make(meta.PropertiesMap)
}
parseClassPHPDocProperty(class, &d.ctx, &result, part.(*phpdoc.TypeVarCommentPart))
case "method":
if result.methods.H == nil {
result.methods = meta.NewFunctionsMap()
}
parseClassPHPDocMethod(class, &d.ctx, &result, part.(*phpdoc.RawCommentPart))
case "mixin":
parseClassPHPDocMixin(class, d.ctx.st, &result, part.(*phpdoc.RawCommentPart))
case "package":
parseClassPHPDocPackage(class, d.ctx.st, &result, part.(*phpdoc.PackageCommentPart))
case "deprecated":
result.deprecated = true
case "internal":
result.internal = true
}
Expand Down
24 changes: 24 additions & 0 deletions src/tests/checkers/deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,27 @@ WithText::staticMethod();
}
test.RunAndMatch()
}

func TestDeprecatedClassAndProperty(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
declare(strict_types = "1");

/**
* @deprecated class
*/
class OldClass
{
/**
* @deprecated property
*/
public $prp;
}

`)
test.Expect = []string{
"Has deprecated class OldClass",
"Has deprecated field in class OldClass",
}
test.RunAndMatch()
}
9 changes: 9 additions & 0 deletions src/tests/golden/testdata/flysystem/golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ MAYBE missingPhpdoc: Missing PHPDoc for \League\Flysystem\Adapter\Polyfill\Str
MAYBE missingPhpdoc: Missing PHPDoc for \League\Flysystem\Adapter\Polyfill\StreamedWritingTrait::update public method at testdata/flysystem/src/Adapter/Polyfill/StreamedWritingTrait.php:59
abstract public function update($pash, $contents, Config $config);
^^^^^^
MAYBE deprecated: Has deprecated class Directory at testdata/flysystem/src/Directory.php:8
class Directory extends Handler
^
MAYBE deprecated: Has deprecated class File at testdata/flysystem/src/File.php:8
class File extends Handler
^
WARNING notExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/flysystem/src/FileExistsException.php:21
public function __construct($path, $code = 0, BaseException $previous = null)
^^^^^^^^^^^^^
Expand Down Expand Up @@ -94,6 +100,9 @@ WARNING notExplicitNullableParam: parameter with null default value should be ex
WARNING notExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/flysystem/src/FilesystemInterface.php:274
public function get($path, Handler $handler = null);
^^^^^^^
MAYBE deprecated: Has deprecated class Handler at testdata/flysystem/src/Handler.php:10
abstract class Handler
^
WARNING unused: Variable $e is unused (use $_ to ignore this inspection or specify --unused-var-regex flag) at testdata/flysystem/src/Handler.php:129
} catch (BadMethodCallException $e) {
^^
Expand Down
3 changes: 3 additions & 0 deletions src/tests/golden/testdata/inflector/golden.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
MAYBE deprecated: Has deprecated class Inflector at testdata/inflector/lib/Doctrine/Common/Inflector/Inflector.php:33
final class Inflector
^
WARNING errorSilence: Don't use @, silencing errors is bad practice at testdata/inflector/lib/Doctrine/Common/Inflector/Inflector.php:54
@trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Loading