Skip to content

Commit

Permalink
Enhancement: Extract Analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 7, 2025
1 parent c633936 commit a8bf864
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 189 deletions.
3 changes: 3 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ parametersSchema:
])

services:
-
class: Ergebnis\PHPStan\Rules\Analyzer

-
class: Ergebnis\PHPStan\Rules\Classes\FinalRule
arguments:
Expand Down
55 changes: 55 additions & 0 deletions src/Analyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018-2025 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpstan-rules
*/

namespace Ergebnis\PHPStan\Rules;

use PhpParser\Node;

/**
* @internal
*/
final class Analyzer
{
/**
* @param null|Node\ComplexType|Node\Identifier|Node\Name $typeDeclaration
*/
public function isNullableTypeDeclaration($typeDeclaration): bool
{
if ($typeDeclaration instanceof Node\NullableType) {
return true;
}

if ($typeDeclaration instanceof Node\UnionType) {
foreach ($typeDeclaration->types as $type) {
if (!$type instanceof Node\Identifier) {
continue;
}

if ('null' === $type->toLowerString()) {
return true;
}
}
}

return false;
}

public function hasNullDefaultValue(Node\Param $parameter): bool
{
if (!$parameter->default instanceof Node\Expr\ConstFetch) {
return false;
}

return 'null' === $parameter->default->name->toLowerString();
}
}
33 changes: 9 additions & 24 deletions src/Closures/NoNullableReturnTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Closures;

use Ergebnis\PHPStan\Rules\Analyzer;
use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
Expand All @@ -23,6 +24,13 @@
*/
final class NoNullableReturnTypeDeclarationRule implements Rules\Rule
{
private Analyzer $analyzer;

public function __construct(Analyzer $analyzer)
{
$this->analyzer = $analyzer;
}

public function getNodeType(): string
{
return Node\Expr\Closure::class;
Expand All @@ -32,7 +40,7 @@ public function processNode(
Node $node,
Analyser\Scope $scope
): array {
if (!self::hasNullableReturnTypeDeclaration($node)) {
if (!$this->analyzer->isNullableTypeDeclaration($node->getReturnType())) {
return [];
}

Expand All @@ -42,27 +50,4 @@ public function processNode(
->build(),
];
}

private static function hasNullableReturnTypeDeclaration(Node\Expr\Closure $closure): bool
{
$returnType = $closure->getReturnType();

if ($returnType instanceof Node\NullableType) {
return true;
}

if ($returnType instanceof Node\UnionType) {
foreach ($returnType->types as $type) {
if (!$type instanceof Node\Identifier) {
continue;
}

if ('null' === $type->toString()) {
return true;
}
}
}

return false;
}
}
21 changes: 10 additions & 11 deletions src/Closures/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Closures;

use Ergebnis\PHPStan\Rules\Analyzer;
use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
Expand All @@ -23,6 +24,13 @@
*/
final class NoParameterWithNullDefaultValueRule implements Rules\Rule
{
private Analyzer $analyzer;

public function __construct(Analyzer $analyzer)
{
$this->analyzer = $analyzer;
}

public function getNodeType(): string
{
return Node\Expr\Closure::class;
Expand All @@ -36,8 +44,8 @@ public function processNode(
return [];
}

$parametersWithNullDefaultValue = \array_values(\array_filter($node->params, static function (Node\Param $parameter): bool {
return self::hasNullDefaultValue($parameter);
$parametersWithNullDefaultValue = \array_values(\array_filter($node->params, function (Node\Param $parameter): bool {
return $this->analyzer->hasNullDefaultValue($parameter);
}));

if (0 === \count($parametersWithNullDefaultValue)) {
Expand All @@ -61,13 +69,4 @@ public function processNode(
->build();
}, $parametersWithNullDefaultValue);
}

private static function hasNullDefaultValue(Node\Param $parameter): bool
{
if (!$parameter->default instanceof Node\Expr\ConstFetch) {
return false;
}

return 'null' === $parameter->default->name->toLowerString();
}
}
35 changes: 10 additions & 25 deletions src/Closures/NoParameterWithNullableTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Closures;

use Ergebnis\PHPStan\Rules\Analyzer;
use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
Expand All @@ -23,6 +24,13 @@
*/
final class NoParameterWithNullableTypeDeclarationRule implements Rules\Rule
{
private Analyzer $analyzer;

public function __construct(Analyzer $analyzer)
{
$this->analyzer = $analyzer;
}

public function getNodeType(): string
{
return Node\Expr\Closure::class;
Expand All @@ -36,8 +44,8 @@ public function processNode(
return [];
}

$parametersWithNullableTypeDeclaration = \array_values(\array_filter($node->params, static function (Node\Param $parameter): bool {
return self::hasNullableTypeDeclaration($parameter);
$parametersWithNullableTypeDeclaration = \array_values(\array_filter($node->params, function (Node\Param $parameter): bool {
return $this->analyzer->isNullableTypeDeclaration($parameter->type);
}));

if (0 === \count($parametersWithNullableTypeDeclaration)) {
Expand All @@ -61,27 +69,4 @@ public function processNode(
->build();
}, $parametersWithNullableTypeDeclaration);
}

private static function hasNullableTypeDeclaration(Node\Param $parameter): bool
{
if ($parameter->type instanceof Node\NullableType) {
return true;
}

if ($parameter->type instanceof Node\UnionType) {
foreach ($parameter->type->types as $type) {
if (!$type instanceof Node\Identifier) {
continue;
}

if ('null' !== $type->toString()) {
continue;
}

return true;
}
}

return false;
}
}
33 changes: 9 additions & 24 deletions src/Functions/NoNullableReturnTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Functions;

use Ergebnis\PHPStan\Rules\Analyzer;
use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
Expand All @@ -23,6 +24,13 @@
*/
final class NoNullableReturnTypeDeclarationRule implements Rules\Rule
{
private Analyzer $analyzer;

public function __construct(Analyzer $analyzer)
{
$this->analyzer = $analyzer;
}

public function getNodeType(): string
{
return Node\Stmt\Function_::class;
Expand All @@ -36,7 +44,7 @@ public function processNode(
return [];
}

if (!self::hasNullableReturnTypeDeclaration($node)) {
if (!$this->analyzer->isNullableTypeDeclaration($node->getReturnType())) {
return [];
}

Expand All @@ -51,27 +59,4 @@ public function processNode(
->build(),
];
}

private static function hasNullableReturnTypeDeclaration(Node\Stmt\Function_ $function): bool
{
$returnType = $function->getReturnType();

if ($returnType instanceof Node\NullableType) {
return true;
}

if ($returnType instanceof Node\UnionType) {
foreach ($returnType->types as $type) {
if (!$type instanceof Node\Identifier) {
continue;
}

if ('null' === $type->toString()) {
return true;
}
}
}

return false;
}
}
21 changes: 10 additions & 11 deletions src/Functions/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Functions;

use Ergebnis\PHPStan\Rules\Analyzer;
use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
Expand All @@ -23,6 +24,13 @@
*/
final class NoParameterWithNullDefaultValueRule implements Rules\Rule
{
private Analyzer $analyzer;

public function __construct(Analyzer $analyzer)
{
$this->analyzer = $analyzer;
}

public function getNodeType(): string
{
return Node\Stmt\Function_::class;
Expand All @@ -36,8 +44,8 @@ public function processNode(
return [];
}

$parametersWithNullDefaultValue = \array_values(\array_filter($node->params, static function (Node\Param $parameter): bool {
return self::hasNullDefaultValue($parameter);
$parametersWithNullDefaultValue = \array_values(\array_filter($node->params, function (Node\Param $parameter): bool {
return $this->analyzer->hasNullDefaultValue($parameter);
}));

if (0 === \count($parametersWithNullDefaultValue)) {
Expand All @@ -64,13 +72,4 @@ public function processNode(
->build();
}, $parametersWithNullDefaultValue);
}

private static function hasNullDefaultValue(Node\Param $parameter): bool
{
if (!$parameter->default instanceof Node\Expr\ConstFetch) {
return false;
}

return 'null' === $parameter->default->name->toLowerString();
}
}
35 changes: 10 additions & 25 deletions src/Functions/NoParameterWithNullableTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPStan\Rules\Functions;

use Ergebnis\PHPStan\Rules\Analyzer;
use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
Expand All @@ -23,6 +24,13 @@
*/
final class NoParameterWithNullableTypeDeclarationRule implements Rules\Rule
{
private Analyzer $analyzer;

public function __construct(Analyzer $analyzer)
{
$this->analyzer = $analyzer;
}

public function getNodeType(): string
{
return Node\Stmt\Function_::class;
Expand All @@ -36,8 +44,8 @@ public function processNode(
return [];
}

$parametersWithNullableTypeDeclaration = \array_values(\array_filter($node->params, static function (Node\Param $parameter): bool {
return self::hasNullableTypeDeclaration($parameter);
$parametersWithNullableTypeDeclaration = \array_values(\array_filter($node->params, function (Node\Param $parameter): bool {
return $this->analyzer->isNullableTypeDeclaration($parameter->type);
}));

if (0 === \count($parametersWithNullableTypeDeclaration)) {
Expand All @@ -64,27 +72,4 @@ public function processNode(
->build();
}, $parametersWithNullableTypeDeclaration);
}

private static function hasNullableTypeDeclaration(Node\Param $parameter): bool
{
if ($parameter->type instanceof Node\NullableType) {
return true;
}

if ($parameter->type instanceof Node\UnionType) {
foreach ($parameter->type->types as $type) {
if (!$type instanceof Node\Identifier) {
continue;
}

if ('null' !== $type->toString()) {
continue;
}

return true;
}
}

return false;
}
}
Loading

0 comments on commit a8bf864

Please sign in to comment.