Skip to content

Commit

Permalink
Prepare for PHPStan 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Oct 2, 2024
1 parent c91fead commit f692c3f
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 37 deletions.
2 changes: 2 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ services:
allowedCheckedExceptionCallables: %shipmonkRules.forbidCheckedExceptionInCallable.allowedCheckedExceptionCallables%
-
class: ShipMonk\PHPStan\Rule\ForbidCheckedExceptionInYieldingMethodRule
arguments:
exceptionTypeResolver: @PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver
-
class: ShipMonk\PHPStan\Rule\ForbidCustomFunctionsRule
arguments:
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/EnforceListReturnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\VerbosityLevel;
use function count;
use function method_exists;

/**
* @implements Rule<ReturnStatementsNode>
Expand All @@ -31,7 +32,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if (AccessoryArrayListType::isListTypeEnabled() === false) {
if (method_exists(AccessoryArrayListType::class, 'isListTypeEnabled') && !AccessoryArrayListType::isListTypeEnabled()) { // @phpstan-ignore function.alreadyNarrowedType (changes in v2)
return [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rule/ForbidCheckedExceptionInCallableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function processArrowFunction(
$result = $this->nodeScopeResolver->processExprNode(
new Expression($node->expr),
$node->expr,
$scope->enterArrowFunction($node),
$scope->enterArrowFunction($node, null),
static function (): void {
},
ExpressionContext::createDeep(),
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidCheckedExceptionInYieldingMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver;
use PHPStan\Rules\Exceptions\ExceptionTypeResolver;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -16,9 +16,9 @@
class ForbidCheckedExceptionInYieldingMethodRule implements Rule
{

private DefaultExceptionTypeResolver $exceptionTypeResolver;
private ExceptionTypeResolver $exceptionTypeResolver;

public function __construct(DefaultExceptionTypeResolver $exceptionTypeResolver)
public function __construct(ExceptionTypeResolver $exceptionTypeResolver)
{
$this->exceptionTypeResolver = $exceptionTypeResolver;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidFetchOnMixedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -25,11 +25,11 @@
class ForbidFetchOnMixedRule implements Rule
{

private Standard $printer;
private Printer $printer;

private bool $checkExplicitMixed;

public function __construct(Standard $printer, bool $checkExplicitMixed)
public function __construct(Printer $printer, bool $checkExplicitMixed)
{
$this->printer = $printer;
$this->checkExplicitMixed = $checkExplicitMixed;
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidMethodCallOnMixedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -24,11 +24,11 @@
class ForbidMethodCallOnMixedRule implements Rule
{

private Standard $printer;
private Printer $printer;

private bool $checkExplicitMixed;

public function __construct(Standard $printer, bool $checkExplicitMixed)
public function __construct(Printer $printer, bool $checkExplicitMixed)
{
$this->printer = $printer;
$this->checkExplicitMixed = $checkExplicitMixed;
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidNotNormalizedTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use PhpParser\PrettyPrinter\Standard as PhpParserPrinter;
use PHPStan\Analyser\NameScope;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\PhpDoc\TypeNodeResolver;
use PHPStan\PhpDocParser\Ast\Node as PhpDocRootNode;
Expand Down Expand Up @@ -57,7 +57,7 @@ class ForbidNotNormalizedTypeRule implements Rule

private TypeNodeResolver $typeNodeResolver;

private PhpParserPrinter $phpParserPrinter;
private Printer $phpParserPrinter;

private bool $checkDisjunctiveNormalForm;

Expand All @@ -69,7 +69,7 @@ class ForbidNotNormalizedTypeRule implements Rule
public function __construct(
FileTypeMapper $fileTypeMapper,
TypeNodeResolver $typeNodeResolver,
PhpParserPrinter $phpParserPrinter,
Printer $phpParserPrinter,
bool $checkDisjunctiveNormalForm
)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidNullInInterpolatedStringRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use PhpParser\Node;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -18,9 +18,9 @@
class ForbidNullInInterpolatedStringRule implements Rule
{

private Standard $printer;
private Printer $printer;

public function __construct(Standard $printer)
public function __construct(Printer $printer)
{
$this->printer = $printer;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidUnusedExceptionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -22,9 +22,9 @@
class ForbidUnusedExceptionRule implements Rule
{

private Standard $printer;
private Printer $printer;

public function __construct(Standard $printer)
public function __construct(Printer $printer)
{
$this->printer = $printer;
}
Expand Down
9 changes: 6 additions & 3 deletions src/Rule/RequirePreviousExceptionPassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\IdentifierRuleError;
Expand All @@ -35,11 +35,14 @@
class RequirePreviousExceptionPassRule implements Rule
{

private Standard $printer;
private Printer $printer;

private bool $reportEvenIfExceptionIsNotAcceptableByRethrownOne;

public function __construct(Standard $printer, bool $reportEvenIfExceptionIsNotAcceptableByRethrownOne = false)
public function __construct(
Printer $printer,
bool $reportEvenIfExceptionIsNotAcceptableByRethrownOne = false
)
{
$this->printer = $printer;
$this->reportEvenIfExceptionIsNotAcceptableByRethrownOne = $reportEvenIfExceptionIsNotAcceptableByRethrownOne;
Expand Down
4 changes: 2 additions & 2 deletions tests/Rule/ForbidCheckedExceptionInYieldingMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ShipMonk\PHPStan\Rule;

use ForbidCheckedExceptionInYieldingMethodRule\CheckedException;
use PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver;
use PHPStan\Rules\Exceptions\ExceptionTypeResolver;
use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;
use Throwable;
Expand All @@ -16,7 +16,7 @@ class ForbidCheckedExceptionInYieldingMethodRuleTest extends RuleTestCase

protected function getRule(): Rule
{
$exceptionTypeResolverMock = self::createMock(DefaultExceptionTypeResolver::class);
$exceptionTypeResolverMock = $this->createMock(ExceptionTypeResolver::class);
$exceptionTypeResolverMock
->expects(self::any())
->method('isCheckedException')
Expand Down
4 changes: 2 additions & 2 deletions tests/Rule/ForbidFetchOnMixedRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ShipMonk\PHPStan\Rule;

use PhpParser\PrettyPrinter\Standard;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;

Expand All @@ -15,7 +15,7 @@ class ForbidFetchOnMixedRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new ForbidFetchOnMixedRule(
self::getContainer()->getByType(Standard::class),
self::getContainer()->getByType(Printer::class), // @phpstan-ignore phpstanApi.classConstant
(bool) self::getContainer()->getParameter('checkExplicitMixed'),
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Rule/ForbidMethodCallOnMixedRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ShipMonk\PHPStan\Rule;

use PhpParser\PrettyPrinter\Standard;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;

Expand All @@ -15,7 +15,7 @@ class ForbidMethodCallOnMixedRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new ForbidMethodCallOnMixedRule(
self::getContainer()->getByType(Standard::class),
self::getContainer()->getByType(Printer::class), // @phpstan-ignore phpstanApi.classConstant
(bool) self::getContainer()->getParameter('checkExplicitMixed'),
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Rule/ForbidNotNormalizedTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ShipMonk\PHPStan\Rule;

use PhpParser\PrettyPrinter\Standard;
use PHPStan\Node\Printer\Printer;
use PHPStan\PhpDoc\TypeNodeResolver;
use PHPStan\Type\FileTypeMapper;
use ShipMonk\PHPStan\RuleTestCase;
Expand All @@ -18,7 +18,7 @@ protected function getRule(): ForbidNotNormalizedTypeRule
return new ForbidNotNormalizedTypeRule(
self::getContainer()->getByType(FileTypeMapper::class),
self::getContainer()->getByType(TypeNodeResolver::class),
self::getContainer()->getByType(Standard::class),
self::getContainer()->getByType(Printer::class), // @phpstan-ignore phpstanApi.classConstant
true,
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Rule/ForbidNullInInterpolatedStringRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ShipMonk\PHPStan\Rule;

use PhpParser\PrettyPrinter\Standard;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;

Expand All @@ -15,7 +15,7 @@ class ForbidNullInInterpolatedStringRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new ForbidNullInInterpolatedStringRule(
self::getContainer()->getByType(Standard::class),
self::getContainer()->getByType(Printer::class), // @phpstan-ignore phpstanApi.classConstant
);
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Rule/ForbidUnusedExceptionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ShipMonk\PHPStan\Rule;

use PhpParser\PrettyPrinter\Standard;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;
use function array_merge;
Expand All @@ -15,7 +15,9 @@ class ForbidUnusedExceptionRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new ForbidUnusedExceptionRule(new Standard());
return new ForbidUnusedExceptionRule(
self::getContainer()->getByType(Printer::class), // @phpstan-ignore phpstanApi.classConstant
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Rule/RequirePreviousExceptionPassRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ShipMonk\PHPStan\Rule;

use LogicException;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;

Expand All @@ -22,7 +22,7 @@ protected function getRule(): Rule
}

return new RequirePreviousExceptionPassRule(
self::getContainer()->getByType(Standard::class),
self::getContainer()->getByType(Printer::class), // @phpstan-ignore phpstanApi.classConstant
$this->reportEvenIfExceptionIsNotAcceptableByRethrownOne,
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Rule/data/ForbidFetchOnMixedRuleTest/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Foo {
$mixed::CONST; // error: Constant fetch ::CONST is prohibited on unknown type ($mixed)
$mixed::class; // error: Constant fetch ::class is prohibited on unknown type ($mixed)
$unknown->fetch2; // error: Property fetch ->fetch2 is prohibited on unknown type ($unknown)
$unknown?->fetch3; // error: Property fetch ->fetch3 is prohibited on unknown type ($unknown)
$unknown::$fetch2; // error: Property fetch ::$fetch2 is prohibited on unknown type ($unknown)
$array[0]->fetch3; // error: Property fetch ->fetch3 is prohibited on unknown type ($array[0])

Expand Down

0 comments on commit f692c3f

Please sign in to comment.