Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Aug 26, 2024
1 parent ba3d6e5 commit 5c6192b
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/linter/quickfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (g *QuickFixGenerator) NullForNotNullableProperty(prop *ir.PropertyStmt) qu
}
}

func (g *QuickFixGenerator) NullableType(param ir.Node) quickfix.TextEdit {
func (g *QuickFixGenerator) NotExplicitNullableParam(param ir.Node) quickfix.TextEdit {
var pos *position.Position
var value string

Expand Down
2 changes: 1 addition & 1 deletion src/linter/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func addBuiltinCheckers(reg *CheckersRegistry) {
},

{
Name: "nullableType",
Name: "NotExplicitNullableParam",
Default: true,
Quickfix: true,
Comment: "Report not nullable param can be null.",
Expand Down
8 changes: 6 additions & 2 deletions src/linter/root_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ func (r *rootChecker) CheckParamNullability(p *ir.Parameter) {
paramType = paramIdentifier
}

if paramName, ok := paramType.(*ir.Name); ok && isMixedLikeType(paramName.Value) {
return
}

defValue, defValueOk := p.DefaultValue.(*ir.ConstFetchExpr)
if !defValueOk {
return
Expand All @@ -628,8 +632,8 @@ func (r *rootChecker) CheckParamNullability(p *ir.Parameter) {
return
}

r.walker.Report(paramType, LevelWarning, "nullableType", "parameter with null default value should be explicitly nullable")
r.walker.addQuickFix("nullableType", r.quickfix.NullableType(paramType))
r.walker.Report(paramType, LevelWarning, "NotExplicitNullableParam", "parameter with null default value should be explicitly nullable")
r.walker.addQuickFix("NotExplicitNullableParam", r.quickfix.NotExplicitNullableParam(paramType))
}

func (r *rootChecker) CheckTypeHintFunctionParam(p *ir.Parameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,34 @@ function multipleArgsExample(string $a, int $b = null, bool $c = null) {

test.RunAndMatch()
}

func TestNullableOrString(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
function nullableOrString(null|string $a = null) {
return 0;
}
`)

test.RunAndMatch()
}

func TestNullableOrClass(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
class MyClass1 {
}
class MyClass2 {
public function myMethod(null|MyClass1 $a = null) {
return 0;
}
}
`)

test.Expect = []string{
"Missing PHPDoc for \\MyClass2::myMethod public method",
}

test.RunAndMatch()
}
12 changes: 6 additions & 6 deletions src/tests/golden/testdata/flysystem/golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ 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);
^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/flysystem/src/FileExistsException.php:21
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)
^^^^^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/flysystem/src/FileNotFoundException.php:21
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/flysystem/src/FileNotFoundException.php:21
public function __construct($path, $code = 0, BaseException $previous = null)
^^^^^^^^^^^^^
MAYBE typeHint: Specify the type for the parameter $config in PHPDoc, 'array' type hint too generic at testdata/flysystem/src/Filesystem.php:63
Expand All @@ -88,13 +88,13 @@ MAYBE typeHint: Specify the type for the parameter $config in PHPDoc, 'array'
MAYBE typeHint: Specify the type for the parameter $config in PHPDoc, 'array' type hint too generic at testdata/flysystem/src/Filesystem.php:257
public function createDir($dirname, array $config = [])
^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/flysystem/src/Filesystem.php:362
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/flysystem/src/Filesystem.php:362
public function get($path, Handler $handler = null)
^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/flysystem/src/FilesystemInterface.php:274
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);
^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/flysystem/src/Handler.php:28
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/flysystem/src/Handler.php:28
public function __construct(FilesystemInterface $filesystem = null, $path = null)
^^^^^^^^^^^^^^^^^^^
WARNING unused: Variable $e is unused (use $_ to ignore this inspection or specify --unused-var-regex flag) at testdata/flysystem/src/Handler.php:129
Expand All @@ -103,7 +103,7 @@ WARNING unused: Variable $e is unused (use $_ to ignore this inspection or speci
WARNING unused: Variable $e is unused (use $_ to ignore this inspection or specify --unused-var-regex flag) at testdata/flysystem/src/MountManager.php:275
} catch (PluginNotFoundException $e) {
^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/flysystem/src/MountManager.php:642
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/flysystem/src/MountManager.php:642
public function get($path, Handler $handler = null)
^^^^^^^
MAYBE deprecatedUntagged: Call to deprecated method {\League\Flysystem\FilesystemInterface}->get() at testdata/flysystem/src/MountManager.php:646
Expand Down
12 changes: 6 additions & 6 deletions src/tests/golden/testdata/mustache/golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,25 @@ ERROR undefinedMethod: Call to undefined method {\Mustache_Cache}->setLogger()
MAYBE ternarySimplify: Could rewrite as `$this->delimiters ?: '{{ }}'` at testdata/mustache/src/Mustache/Engine.php:628
'delimiters' => $this->delimiters ? $this->delimiters : '{{ }}',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Engine.php:727
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Engine.php:727
private function loadSource($source, Mustache_Cache $cache = null)
^^^^^^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Exception/SyntaxException.php:24
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Exception/SyntaxException.php:24
public function __construct($msg, array $token, Exception $previous = null)
^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Exception/UnknownFilterException.php:23
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Exception/UnknownFilterException.php:23
public function __construct($filterName, Exception $previous = null)
^^^^^^^^^
MAYBE missingPhpdoc: Missing PHPDoc for \Mustache_Exception_UnknownFilterException::getFilterName public method at testdata/mustache/src/Mustache/Exception/UnknownFilterException.php:34
public function getFilterName()
^^^^^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Exception/UnknownHelperException.php:23
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Exception/UnknownHelperException.php:23
public function __construct($helperName, Exception $previous = null)
^^^^^^^^^
MAYBE missingPhpdoc: Missing PHPDoc for \Mustache_Exception_UnknownHelperException::getHelperName public method at testdata/mustache/src/Mustache/Exception/UnknownHelperException.php:34
public function getHelperName()
^^^^^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Exception/UnknownTemplateException.php:23
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Exception/UnknownTemplateException.php:23
public function __construct($templateName, Exception $previous = null)
^^^^^^^^^
MAYBE missingPhpdoc: Missing PHPDoc for \Mustache_Exception_UnknownTemplateException::getTemplateName public method at testdata/mustache/src/Mustache/Exception/UnknownTemplateException.php:34
Expand All @@ -178,7 +178,7 @@ MAYBE missingPhpdoc: Missing PHPDoc for \Mustache_Exception_UnknownTemplateExc
WARNING regexpVet: '\w' intersects with '\d' in [\w\d\.] at testdata/mustache/src/Mustache/Loader/InlineLoader.php:115
foreach (preg_split("/^@@(?= [\w\d\.]+$)/m", $data, -1) as $chunk) {
^^^^^^^^^^^^^^^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Parser.php:73
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/mustache/src/Mustache/Parser.php:73
private function buildTree(array &$tokens, array $parent = null)
^^^^^
WARNING switchDefault: Add 'default' branch to avoid unexpected unhandled condition values at testdata/mustache/src/Mustache/Parser.php:307
Expand Down
6 changes: 3 additions & 3 deletions src/tests/golden/testdata/parsedown/golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ MAYBE typeHint: Specify the type for the parameter $Block in PHPDoc, 'array' t
MAYBE trailingComma: Last element in a multi-line array should have a trailing comma at testdata/parsedown/parsedown.php:560
'handler' => array(
^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/parsedown/parsedown.php:574
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/parsedown/parsedown.php:574
protected function blockList($Line, array $CurrentBlock = null)
^^^^^
MAYBE typeHint: Specify the type for the parameter $CurrentBlock in PHPDoc, 'array' type hint too generic at testdata/parsedown/parsedown.php:574
Expand Down Expand Up @@ -70,7 +70,7 @@ MAYBE typeHint: Specify the type for the parameter $Block in PHPDoc, 'array' t
MAYBE regexpSimplify: May re-write '/^>[ ]?+(.*+)/' as '/^> ?+(.*+)/' at testdata/parsedown/parsedown.php:774
if ($Line['text'][0] === '>' and preg_match('/^>[ ]?+(.*+)/', $Line['text'], $matches))
^^^^^^^^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/parsedown/parsedown.php:811
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/parsedown/parsedown.php:811
protected function blockSetextHeader($Line, array $Block = null)
^^^^^
MAYBE typeHint: Specify the type for the parameter $Block in PHPDoc, 'array' type hint too generic at testdata/parsedown/parsedown.php:811
Expand All @@ -88,7 +88,7 @@ MAYBE regexpSimplify: May re-write '/^\[(.+?)\]:[ ]*+<?(\S+?)>?(?:[ ]+["\'(](.
MAYBE ternarySimplify: Could rewrite as `$matches[3] ?? null` at testdata/parsedown/parsedown.php:881
'title' => isset($matches[3]) ? $matches[3] : null,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
WARNING nullableType: parameter with null default value should be explicitly nullable at testdata/parsedown/parsedown.php:897
WARNING NotExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/parsedown/parsedown.php:897
protected function blockTable($Line, array $Block = null)
^^^^^
MAYBE typeHint: Specify the type for the parameter $Block in PHPDoc, 'array' type hint too generic at testdata/parsedown/parsedown.php:897
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ function nullableArray(array $a = null) {
function multipleArgsExample(string $a, int $b = null, bool $c = null) {
return 0;
}

function nullableOrString(null|string $a = null) {
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ function nullableArray(?array $a = null) {
function multipleArgsExample(string $a, ?int $b = null, ?bool $c = null) {
return 0;
}

function nullableOrString(null|string $a = null) {
return 0;
}

0 comments on commit 5c6192b

Please sign in to comment.