Skip to content

Commit

Permalink
refactored architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Aug 28, 2024
1 parent 87601d6 commit 13ad6c8
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 62 deletions.
36 changes: 36 additions & 0 deletions src/linter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,44 @@ func (b *blockWalker) handleAndCheckGlobalStmt(s *ir.GlobalStmt) {
}
}

func (b *blockWalker) CheckParamNullability(params []ir.Node) {
for _, param := range params {
if p, ok := param.(*ir.Parameter); ok {
var paramType ir.Node
paramType, paramOk := p.VariableType.(*ir.Name)
if !paramOk {
paramIdentifier, paramIdentifierOk := p.VariableType.(*ir.Identifier)
if !paramIdentifierOk {
continue
}
paramType = paramIdentifier
}

paramName, ok := paramType.(*ir.Name)
if ok {
if paramName.Value == "mixed" {
continue
}
}

defValue, defValueOk := p.DefaultValue.(*ir.ConstFetchExpr)
if !defValueOk {
continue
}

if defValue.Constant.Value != "null" {
continue
}

b.linter.report(paramType, LevelWarning, "notExplicitNullableParam", "parameter with null default value should be explicitly nullable")
b.r.addQuickFix("notExplicitNullableParam", b.linter.quickfix.notExplicitNullableParam(paramType))
}
}
}

func (b *blockWalker) handleFunction(fun *ir.FunctionStmt) bool {
if b.ignoreFunctionBodies {
b.CheckParamNullability(fun.Params)
return false
}

Expand Down
3 changes: 2 additions & 1 deletion src/linter/block_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ func (b *blockLinter) checkClass(class *ir.ClassStmt) {

var members = make([]int, 0, len(class.Stmts))
for _, stmt := range class.Stmts {
switch stmt.(type) {
switch stmt := stmt.(type) {
case *ir.ClassMethodStmt:
members = append(members, classMethod)
b.walker.CheckParamNullability(stmt.Params)
default:
members = append(members, classOtherMember)
}
Expand Down
32 changes: 0 additions & 32 deletions src/linter/root_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,38 +605,6 @@ func (r *rootChecker) checkFuncParam(p *ir.Parameter) {
return true
})
r.CheckTypeHintFunctionParam(p)
r.CheckParamNullability(p)
}

func (r *rootChecker) CheckParamNullability(p *ir.Parameter) {
var paramType ir.Node
paramType, paramOk := p.VariableType.(*ir.Name)
if !paramOk {
paramIdentifier, paramIdentifierOk := p.VariableType.(*ir.Identifier)
if !paramIdentifierOk {
return
}
paramType = paramIdentifier
}

paramName, ok := paramType.(*ir.Name)
if ok {
if paramName.Value == "mixed" {
return
}
}

defValue, defValueOk := p.DefaultValue.(*ir.ConstFetchExpr)
if !defValueOk {
return
}

if defValue.Constant.Value != "null" {
return
}

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
15 changes: 6 additions & 9 deletions src/tests/golden/testdata/flysystem/golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,21 @@ MAYBE typeHint: Specify the type for the parameter $config in PHPDoc, 'array'
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 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 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
} catch (BadMethodCallException $e) {
^^
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/MountManager.php:275
} catch (PluginNotFoundException $e) {
^^
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
return $this->getFilesystem($prefix)->get($path);
^^^
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)
^^^^^^^
WARNING unused: Variable $e is unused (use $_ to ignore this inspection or specify --unused-var-regex flag) at testdata/flysystem/src/Plugin/ForcedCopy.php:33
} catch (FileNotFoundException $e) {
^^
Expand Down
22 changes: 11 additions & 11 deletions src/tests/golden/testdata/mustache/golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,33 +157,33 @@ WARNING notExplicitNullableParam: parameter with null default value should be ex
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 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 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)
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_UnknownHelperException::getHelperName public method at testdata/mustache/src/Mustache/Exception/UnknownHelperException.php:34
public function getHelperName()
^^^^^^^^^^^^^
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)
^^^^^^^^^
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_UnknownTemplateException::getTemplateName public method at testdata/mustache/src/Mustache/Exception/UnknownTemplateException.php:34
public function getTemplateName()
^^^^^^^^^^^^^^^
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)
^^^^^^^^^
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 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
switch ($name) {
^
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 errorSilence: Don't use @, silencing errors is bad practice at testdata/mustache/src/Mustache/Source/FilesystemSource.php:53
$this->stat = @stat($this->fileName);
^^^^^^^^^^^^^^^^^^^^^^
Expand Down
18 changes: 9 additions & 9 deletions src/tests/golden/testdata/parsedown/golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ 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 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
protected function blockList($Line, array $CurrentBlock = null)
^^^^^^^^^
Expand Down Expand Up @@ -70,9 +67,6 @@ 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 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
protected function blockSetextHeader($Line, array $Block = null)
^^^^^^^^^^^^^^^^^
Expand All @@ -88,9 +82,6 @@ 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 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
protected function blockTable($Line, array $Block = null)
^^^^^^^^^^
Expand Down Expand Up @@ -181,6 +172,15 @@ MAYBE typeHint: Specify the type for the parameter $Element in PHPDoc, 'array'
MAYBE implicitModifiers: Specify the access modifier for \Parsedown::instance method explicitly at testdata/parsedown/parsedown.php:1938
static function instance($name = 'default')
^^^^^^^^
WARNING notExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/parsedown/parsedown.php:574
protected function blockList($Line, array $CurrentBlock = null)
^^^^^
WARNING notExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/parsedown/parsedown.php:811
protected function blockSetextHeader($Line, array $Block = null)
^^^^^
WARNING notExplicitNullableParam: parameter with null default value should be explicitly nullable at testdata/parsedown/parsedown.php:897
protected function blockTable($Line, array $Block = null)
^^^^^
ERROR classMembersOrder: Property $breaksEnabled must go before methods in the class Parsedown at testdata/parsedown/parsedown.php:66
protected $breaksEnabled;
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 13ad6c8

Please sign in to comment.