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

chore: Upgraded to Larastan 3.0 and PHPStan 2.0 #191

Merged
merged 1 commit into from
Dec 2, 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
37 changes: 9 additions & 28 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ jobs:

strategy:
matrix:
php: [8.1]
php: [8.2]
dependencies: [lowest]

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v4"

- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@v2"
Expand Down Expand Up @@ -63,12 +63,12 @@ jobs:

strategy:
matrix:
php: [8.1]
php: [8.2]
dependencies: [lowest]

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v4"

- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@v2"
Expand Down Expand Up @@ -116,32 +116,13 @@ jobs:

strategy:
matrix:
php: [8.1, 8.2, 8.3, 8.4 ]
orchestra: [ "^7.47", "^8.13", "^9.0" ]
php: [ 8.2, 8.3, 8.4 ]
orchestra: [ "^9.0" ]
dependencies: [ lowest, highest ]
exclude:
- php: "8.1"
orchestra: "^9.0"
dependencies: "highest"
- php: "8.1"
orchestra: "^9.0"
dependencies: "lowest"
- php: "8.4"
orchestra: "^7.47"
dependencies: "highest"
- php: "8.4"
orchestra: "^7.47"
dependencies: "lowest"
- php: "8.4"
orchestra: "^8.13"
dependencies: "highest"
- php: "8.4"
orchestra: "^8.13"
dependencies: "lowest"

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v4"

- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@v2"
Expand Down Expand Up @@ -179,13 +160,13 @@ jobs:

strategy:
matrix:
php: [8.1]
php: [8.2]
dependencies:
- "lowest"

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v4"

- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@v2"
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ composer.lock
vendor
coverage
.phpunit.result.cache
.idea
.idea
tests/.phpunit.cache
tests/phpunit.xml.bak
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"MIT"
],
"require": {
"php": "^8.0",
"larastan/larastan": "^2.9.12",
"phpstan/phpstan": "^1.12"
"php": "^8.2",
"larastan/larastan": "^3.0",
"phpstan/phpstan": "^2.0"
},
"require-dev": {
"doctrine/coding-standard": "^12",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^9.6 || ^10",
"orchestra/testbench": "^v7.47.0 || ^8.13.0 || ^9.0.9",
"doctrine/coding-standard": "^12.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^10.0",
"orchestra/testbench" : "^9.0",
"roave/security-advisories": "dev-latest"
},
"config": {
Expand Down
47 changes: 38 additions & 9 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,32 +1,61 @@
parameters:
ignoreErrors:
-
message: "#^Calling PHPStan\\\\Reflection\\\\Php\\\\PhpFunctionReflection\\:\\:getFileName\\(\\) is not covered by backward compatibility promise\\. The method might change in a minor PHPStan version\\.$#"
message: '#^Doing instanceof PHPStan\\Type\\Generic\\GenericObjectType is error\-prone and deprecated\.$#'
identifier: phpstanApi.instanceofType
count: 1
path: src/Rules/NoDynamicWhereRule.php

-
message: '#^Doing instanceof PHPStan\\Type\\ObjectType is error\-prone and deprecated\. Use Type\:\:isObject\(\) or Type\:\:getObjectClassNames\(\) instead\.$#'
identifier: phpstanApi.instanceofType
count: 3
path: src/Rules/NoDynamicWhereRule.php

-
message: '#^Asking about instanceof PHPStan\\Reflection\\Php\\PhpFunctionReflection is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#'
identifier: phpstanApi.class
count: 1
path: src/Rules/NoGlobalLaravelFunctionRule.php

-
message: "#^Calling PHPStan\\\\Reflection\\\\Php\\\\PhpFunctionReflection\\:\\:getName\\(\\) is not covered by backward compatibility promise\\. The method might change in a minor PHPStan version\\.$#"
message: '#^Calling PHPStan\\Reflection\\Php\\PhpFunctionReflection\:\:getFileName\(\) is not covered by backward compatibility promise\. The method might change in a minor PHPStan version\.$#'
identifier: phpstanApi.method
count: 1
path: src/Rules/NoGlobalLaravelFunctionRule.php

-
message: "#^Calling PHPStan\\\\Reflection\\\\Php\\\\PhpParameterFromParserNodeReflection\\:\\:getType\\(\\) is not covered by backward compatibility promise\\. The method might change in a minor PHPStan version\\.$#"
count: 3
message: '#^Calling PHPStan\\Reflection\\Php\\PhpFunctionReflection\:\:getName\(\) is not covered by backward compatibility promise\. The method might change in a minor PHPStan version\.$#'
identifier: phpstanApi.method
count: 1
path: src/Rules/NoGlobalLaravelFunctionRule.php

-
message: '#^Calling PHPStan\\Reflection\\Php\\PhpParameterFromParserNodeReflection\:\:getType\(\) is not covered by backward compatibility promise\. The method might change in a minor PHPStan version\.$#'
identifier: phpstanApi.method
count: 1
path: src/Rules/NoLocalQueryScopeRule.php

-
message: "#^Calling PHPStan\\\\Reflection\\\\Php\\\\PhpParameterFromParserNodeReflection\\:\\:getType\\(\\) is not covered by backward compatibility promise\\. The method might change in a minor PHPStan version\\.$#"
count: 3
message: '#^Doing instanceof PHPStan\\Type\\ObjectType is error\-prone and deprecated\. Use Type\:\:isObject\(\) or Type\:\:getObjectClassNames\(\) instead\.$#'
identifier: phpstanApi.instanceofType
count: 1
path: src/Rules/NoValidationInControllerRule.php

-
message: '#^Calling PHPStan\\Reflection\\Php\\PhpParameterFromParserNodeReflection\:\:getType\(\) is not covered by backward compatibility promise\. The method might change in a minor PHPStan version\.$#'
identifier: phpstanApi.method
count: 1
path: src/Rules/ScopeShouldReturnQueryBuilderRule.php

-
message: "#^Creating new PHPStan\\\\File\\\\FileHelper is not covered by backward compatibility promise\\. The class might change in a minor PHPStan version\\.$#"
message: '#^Creating new PHPStan\\File\\FileHelper is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#'
identifier: phpstanApi.constructor
count: 1
path: tests/Rules/ListenerShouldHaveVoidReturnTypeRuleTest.php

-
message: "#^Parameter \\#2 \\$listenerPaths of class Vural\\\\LarastanStrictRules\\\\Rules\\\\ListenerShouldHaveVoidReturnTypeRule constructor expects array\\<string\\>, mixed given\\.$#"
message: '#^Parameter \#2 \$listenerPaths of class Vural\\LarastanStrictRules\\Rules\\ListenerShouldHaveVoidReturnTypeRule constructor expects array\<string\>, mixed given\.$#'
identifier: argument.type
count: 1
path: tests/Rules/ListenerShouldHaveVoidReturnTypeRuleTest.php

5 changes: 2 additions & 3 deletions src/Rules/ListenerShouldHaveVoidReturnTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\File\FileHelper;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -52,7 +51,7 @@ public function processNode(Node $node, Scope $scope): array
}

// handle method should except event as parameter
if (count(ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getParameters()) < 1) {
if (count($methodReflection->getParameters()) < 1) {
return [];
}

Expand All @@ -72,7 +71,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (! (new VoidType())->isSuperTypeOf(ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType())->yes()) {
if (! (new VoidType())->isSuperTypeOf($methodReflection->getReturnType())->yes()) {
return [
RuleErrorBuilder::message("Listeners handle method should have 'void' return type.")
->identifier('larastanStrictRules.listenerShouldHaveVoidReturnType')
Expand Down
12 changes: 5 additions & 7 deletions src/Rules/NoLocalQueryScopeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpParameterFromParserNodeReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;

use function count;
use function strpos;
Expand Down Expand Up @@ -68,16 +66,16 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

/** @var PhpParameterFromParserNodeReflection $firstParameter */
$firstParameter = $parametersAcceptor->getParameters()[0];
$firstParameter = $methodReflection->getParameters()[0];

$parameterClassNames = $firstParameter->getType()->getObjectClassNames();

if (! ($firstParameter->getType() instanceof ObjectType)) {
if (count($parameterClassNames) !== 1) {
return [];
}

if ($firstParameter->getType()->getClassName() !== Builder::class && ! $this->provider->getClass($firstParameter->getType()->getClassName())->isSubclassOf(Builder::class)) {
if ($parameterClassNames[0] !== Builder::class && ! $this->provider->getClass($parameterClassNames[0])->isSubclassOf(Builder::class)) {
return [];
}

Expand Down
18 changes: 8 additions & 10 deletions src/Rules/ScopeShouldReturnQueryBuilderRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpParameterFromParserNodeReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;

use function count;
use function str_starts_with;
Expand Down Expand Up @@ -67,30 +65,30 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

/** @var PhpParameterFromParserNodeReflection $firstParameter */
$firstParameter = $parametersAcceptor->getParameters()[0];
$firstParameter = $methodReflection->getParameters()[0];

$parameterClassNames = $firstParameter->getType()->getObjectClassNames();

if (! ($firstParameter->getType() instanceof ObjectType)) {
if (count($parameterClassNames) < 1) {
return [];
}

if ($firstParameter->getType()->getClassName() !== Builder::class && ! $this->provider->getClass($firstParameter->getType()->getClassName())->isSubclassOf(Builder::class)) {
if ($parameterClassNames[0] !== Builder::class && ! $this->provider->getClass($parameterClassNames[0])->isSubclassOf(Builder::class)) {
return [];
}

$returnType = $parametersAcceptor->getReturnType();
$returnTypeClassNames = $methodReflection->getReturnType()->getObjectClassNames();

if (! ($returnType instanceof ObjectType)) {
if (count($returnTypeClassNames) !== 1) {
return [
RuleErrorBuilder::message('Query scope should return query builder instance.')
->identifier('larastanStrictRules.scopeShouldReturnQueryBuilderRule')
->build(),
];
}

if ($returnType->getClassName() !== Builder::class && ! $this->provider->getClass($returnType->getClassName())->isSubclassOf(Builder::class)) {
if ($returnTypeClassNames[0] !== Builder::class && ! $this->provider->getClass($returnTypeClassNames[0])->isSubclassOf(Builder::class)) {
return [
RuleErrorBuilder::message('Query scope should return query builder instance.')
->identifier('larastanStrictRules.scopeShouldReturnQueryBuilderRule')
Expand Down
25 changes: 6 additions & 19 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="bootstrap.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutResourceUsageDuringSmallTests="true"
beStrictAboutTodoAnnotatedTests="true"
cacheResultFile="../.build/phpunit.cache"
colors="true" columns="max"
executionOrder="random"
verbose="true"
>
<coverage>
<include>
<directory suffix=".php">../src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" colors="true" columns="max" executionOrder="random" cacheDirectory=".phpunit.cache" beStrictAboutCoverageMetadata="true">
<testsuites>
<testsuite name="Rule tests">
<directory>./Rules</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
</phpunit>
Loading