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

Phpstan 2 #25

Merged
merged 5 commits into from
Dec 20, 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
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
php-version: '8.4'
coverage: none

- name: Install composer dependencies
Expand Down
17 changes: 9 additions & 8 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.2, 8.3 ]
laravel: [ 10.*, 11.* ]
php: [ 8.3, 8.4 ]
laravel: [ ^11.0 ]
carbon: [ ^2.0, ^3.0 ]
stability: [ prefer-lowest, prefer-stable ]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*
- laravel: ^11.0
testbench: ^9.0
exclude:
- laravel: 10.*
carbon: ^3.0
- php: 8.4
stability: prefer-lowest
- php: 8.4
carbon: ^2.0


name: P${{ matrix.php }} - L${{ matrix.laravel }} - C${{ matrix.carbon }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Extra rules for phpstan analysis

[![Latest Version on Packagist](https://img.shields.io/packagist/v/soyhuce/phpstan-extension.svg?style=flat-square)](https://packagist.org/packages/soyhuce/phpstan-extension)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/soyhuce/phpstan-extension/run-tests?label=tests)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/soyhuce/phpstan-extension/Check%20&%20fix%20styling?label=code%20style)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[![GitHub PHPStan Action Status](https://img.shields.io/github/workflow/status/soyhuce/phpstan-extension/PHPStan?label=phpstan)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3APHPStan+branch%3Amain)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/soyhuce/phpstan-extension/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/soyhuce/phpstan-extension/php-cs-fixer.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![GitHub PHPStan Action Status](https://img.shields.io/github/actions/workflow/status/soyhuce/phpstan-extension/phpstan.yml?branch=main&label=phpstan)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3APHPStan+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/soyhuce/phpstan-extension.svg?style=flat-square)](https://packagist.org/packages/soyhuce/phpstan-extension)

Strict rules for PHPStan and helpers for Laravel
Expand Down
21 changes: 10 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@
}
],
"require": {
"php": "^8.2",
"illuminate/support": "^10.0 || ^11.0",
"php": "^8.3",
"illuminate/support": "^11.0",
"nesbot/carbon": "^2.0 || ^3.0",
"phpstan/phpstan": "^1.10.57"
"phpstan/phpstan": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.7",
"larastan/larastan": "^2.0.1",
"nunomaduro/collision": "^7.10 || ^8.0",
"orchestra/testbench": "^8.0 || ^9.0",
"pestphp/pest": "^2.24",
"pestphp/pest-plugin-laravel": "^2.2",
"larastan/larastan": "^3.0",
"nunomaduro/collision": "^8.0",
"orchestra/testbench": "^9.0",
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^10.4"
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ parameters:
- src
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: true

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
Loading