Skip to content

Commit

Permalink
Upgrade to PHPStan 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Dec 20, 2024
1 parent 3de0317 commit 7515571
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
8 changes: 6 additions & 2 deletions src/Rules/CarbonCopyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\ObjectType;
Expand All @@ -30,7 +32,7 @@ public function getNodeType(): string

/**
* @param MethodCall $node
* @return array<string>
* @return list<RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
Expand Down Expand Up @@ -67,7 +69,9 @@ static function (Type $type) use ($name): bool {
}

return [
"Usage of \\Carbon\\CarbonInterface::{$name}() is prohibited. You should use CarbonImmutable and remove {$name}() call.",
RuleErrorBuilder::message("Usage of \\Carbon\\CarbonInterface::{$name}() is prohibited. You should use CarbonImmutable and remove {$name}() call.")
->identifier('carbon.copy')
->build(),
];
}
}
16 changes: 11 additions & 5 deletions src/Rules/NoAliasUseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Illuminate\Foundation\AliasLoader;
use PhpParser\Node;
use PhpParser\Node\Stmt\UseUse;
use PhpParser\Node\UseItem;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\UseUse>
Expand All @@ -23,12 +25,12 @@ public function __construct()

public function getNodeType(): string
{
return UseUse::class;
return UseItem::class;
}

/**
* @param UseUse $node
* @return array<string>
* @param UseItem $node
* @return list<RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -40,6 +42,10 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return ["Usage of alias {$usedClass} is prohibited, prefer the use of {$aliasedClass}."];
return [
RuleErrorBuilder::message("Usage of alias {$usedClass} is prohibited, prefer the use of {$aliasedClass}.")
->identifier('alias.use')
->build(),
];
}
}
20 changes: 13 additions & 7 deletions src/Rules/NoMutableDateTimeStaticCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\StaticCall>
*/
class NoMutableDateTimeStaticCallRule implements Rule
{
public function __construct(
private ReflectionProvider $reflectionProvider,
) {
}

public function getNodeType(): string
{
return StaticCall::class;
}

/**
* @param StaticCall $node
* @return array<string>
* @return list<RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -35,16 +43,14 @@ public function processNode(Node $node, Scope $scope): array
}

return [
"Static calls of mutable DateTime is forbidden, currently using {$class}.",
RuleErrorBuilder::message("Static calls of mutable DateTime is forbidden, currently using {$class}.")
->identifier('mutable.datetime.static.call')
->build(),
];
}

private function isAllowed(string $class): bool
{
if ($class === DateTime::class) {
return false;
}

return !is_subclass_of($class, DateTime::class);
return !$this->reflectionProvider->getClass($class)->is(DateTime::class);
}
}
32 changes: 20 additions & 12 deletions src/Rules/NoMutableDateTimeUseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,49 @@

use DateTime;
use PhpParser\Node;
use PhpParser\Node\Stmt\UseUse;
use PhpParser\Node\UseItem;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\UseUse>
*/
class NoMutableDateTimeUseRule implements Rule
{
public function __construct(
private ReflectionProvider $reflectionProvider,
) {
}

public function getNodeType(): string
{
return UseUse::class;
return UseItem::class;
}

/**
* @param UseUse $node
* @return array<string>
* @param UseItem $node
* @return list<RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
$usedClass = $node->name->toString();
$class = $node->name->toString();

if ($this->isAllowed($usedClass)) {
if ($this->isAllowed($class)) {
return [];
}

return ["Usage of mutable DateTime is forbidden, currently using {$usedClass}."];
return [
RuleErrorBuilder::message("Usage of mutable DateTime is forbidden, currently using {$class}.")
->identifier('mutable.datetime.use')
->build(),
];
}

private function isAllowed(string $class): bool
{
if ($class === DateTime::class) {
return false;
}

return !is_subclass_of($class, DateTime::class);
return !$this->reflectionProvider->getClass($class)->is(DateTime::class);
}
}
20 changes: 13 additions & 7 deletions src/Rules/NoNewMutableDateTimeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\New_>
*/
class NoNewMutableDateTimeRule implements Rule
{
public function __construct(
private ReflectionProvider $reflectionProvider,
) {
}

public function getNodeType(): string
{
return New_::class;
}

/**
* @param New_ $node
* @return array<string>
* @return list<RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -35,16 +43,14 @@ public function processNode(Node $node, Scope $scope): array
}

return [
"Instanciations of mutable DateTime is forbidden, currently using {$class}.",
RuleErrorBuilder::message("Instanciations of mutable DateTime is forbidden, currently using {$class}.")
->identifier('mutable.datetime.new')
->build(),
];
}

private function isAllowed(string $class): bool
{
if ($class === DateTime::class) {
return false;
}

return !is_subclass_of($class, DateTime::class);
return !$this->reflectionProvider->getClass($class)->is(DateTime::class);
}
}
1 change: 1 addition & 0 deletions tests/ExecutesLarastan.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Soyhuce\PhpstanExtension\Tests;

use function dirname;
use function sprintf;

trait ExecutesLarastan
{
Expand Down

0 comments on commit 7515571

Please sign in to comment.