Skip to content

Commit

Permalink
misc: update PHPStan to version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Dec 19, 2024
1 parent b698def commit 9ef3cfb
Show file tree
Hide file tree
Showing 49 changed files with 178 additions and 144 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"require-dev": {
"phpunit/phpunit": "^10.5",
"infection/infection": "^0.27",
"phpstan/phpstan": "^1.3",
"phpstan/phpstan-strict-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"friendsofphp/php-cs-fixer": "^3.4",
"marcocesarato/php-conventional-changelog": "^1.12",
"vimeo/psalm": "^5.0",
"mikey179/vfsstream": "^1.6.10",
"rector/rector": "^1.0"
"rector/rector": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
81 changes: 39 additions & 42 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion qa/PHPStan/Extension/ApiAndInternalAnnotationCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function processNode(Node $node, Scope $scope): array
return [
RuleErrorBuilder::message(
'Missing annotation `@api` or `@internal`.'
)->build(),
)->identifier('valinor.api_or_internal_annotation')->build(),
];
}

Expand Down
18 changes: 7 additions & 11 deletions qa/PHPStan/Extension/ArgumentsMapperPHPStanExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ClosureType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
Expand Down Expand Up @@ -40,20 +38,18 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method

$type = $scope->getType($arguments[0]->value);

if ($type instanceof ClosureType) {
$parameters = $type->getParameters();
} elseif ($type instanceof ConstantArrayType) {
$acceptors = $type->getCallableParametersAcceptors($scope);
if (! $type->isCallable()->yes()) {
return new MixedType();
}

if (count($acceptors) !== 1) {
return new MixedType();
}
$acceptors = $type->getCallableParametersAcceptors($scope);

$parameters = $acceptors[0]->getParameters();
} else {
if (count($acceptors) !== 1) {
return new MixedType();
}

$parameters = $acceptors[0]->getParameters();

$builder = ConstantArrayTypeBuilder::createEmpty();

foreach ($parameters as $parameter) {
Expand Down
18 changes: 7 additions & 11 deletions qa/PHPStan/Extension/TreeMapperPHPStanExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
use PHPStan\PhpDoc\TypeStringResolver;
use PHPStan\PhpDocParser\Parser\ParserException;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

use function implode;

final class TreeMapperPHPStanExtension implements DynamicMethodReturnTypeExtension
{
public function __construct(private TypeStringResolver $resolver) {}
Expand Down Expand Up @@ -60,16 +58,14 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method

private function type(Type $type): Type
{
if ($type instanceof GenericClassStringType) {
return $type->getGenericType();
if ($type->isClassString()->yes()) {
return $type->getClassStringObjectType();
}

if ($type instanceof ConstantStringType) {
return $this->resolver->resolve($type->getValue());
}
if ($type->isConstantValue()->yes()) {
$value = implode('', $type->getConstantScalarValues());

if ($type instanceof ClassStringType) {
return new ObjectWithoutClassType();
return $this->resolver->resolve($value);
}

return new MixedType();
Expand Down
5 changes: 0 additions & 5 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@

$config->parallel();
$config->skip([
AddLiteralSeparatorToNumberRector::class,
ClassPropertyAssignToConstructorPromotionRector::class,
NullToStrictStringFuncCallArgRector::class,
ReadOnlyPropertyRector::class,
MixedTypeRector::class => [
__DIR__ . '/tests/Unit/Definition/Repository/Reflection/ReflectionClassDefinitionRepositoryTest',
__DIR__ . '/tests/Integration/Mapping/TypeErrorDuringMappingTest.php',
],
RestoreDefaultNullToNullableTypePropertyRector::class => [
__DIR__ . '/tests/Integration/Mapping/Other/FlexibleCastingMappingTest.php',
__DIR__ . '/tests/Integration/Mapping/SingleNodeMappingTest',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function (ReflectionAttribute $attribute) {
return array_values(array_map(
fn (ReflectionAttribute $attribute) => new AttributeDefinition(
$this->classDefinitionRepository->for(new NativeClassType($attribute->getName())),
$attribute->getArguments(),
array_values($attribute->getArguments()),
),
$attributes,
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function __construct(AttributesRepository $attributesRepository)

public function for(ReflectionMethod $reflection, ReflectionTypeResolver $typeResolver): MethodDefinition
{
/** @var non-empty-string $name */
$name = $reflection->name;
$signature = $reflection->getDeclaringClass()->name . '::' . $reflection->name . '()';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ private function extractImportedAliasesFromDocBlock(string $className): array

next($tokens);

/** @var int|null $key / Somehow PHPStan does not properly infer the key */
$key = key($tokens);

// @phpstan-ignore identical.alwaysFalse (Somehow PHPStan does not properly infer the key)
if ($key === null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ private function extractLocalAliasesFromDocBlock(string $className): array
next($tokens);
}

/** @var int|null $key / Somehow PHPStan does not properly infer the key */
$key = key($tokens);

// @phpstan-ignore notIdentical.alwaysTrue (Somehow PHPStan does not properly infer the key)
if ($key !== null) {
$aliases[$name] = $annotation->allAfter($key);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class Settings
/** @var CacheInterface<mixed> */
public CacheInterface $cache;

/** @var non-empty-array<non-empty-string> */
/** @var non-empty-list<non-empty-string> */
public array $supportedDateFormats = self::DEFAULT_SUPPORTED_DATETIME_FORMATS;

public bool $enableFlexibleCasting = false;
Expand Down
3 changes: 2 additions & 1 deletion src/Mapper/Object/DateTimeFormatConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
*/
final class DateTimeFormatConstructor
{
/** @var non-empty-array<non-empty-string> */
/** @var non-empty-list<non-empty-string> */
private array $formats;

/**
* @no-named-arguments
* @param non-empty-string $format
* @param non-empty-string ...$formats
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Mapper/Object/Factory/DateTimeObjectBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class DateTimeObjectBuilderFactory implements ObjectBuilderFactory
{
public function __construct(
private ObjectBuilderFactory $delegate,
/** @var non-empty-array<non-empty-string> */
/** @var non-empty-list<non-empty-string> */
private array $supportedDateFormats,
private FunctionDefinitionRepository $functionDefinitionRepository
) {}
Expand Down
1 change: 1 addition & 0 deletions src/Mapper/Object/FunctionObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function build(array $arguments): object
$arguments = new MethodArguments($parameters, $arguments);

try {
/** @var object */
return ($this->function->callback)(...$arguments);
} catch (Exception $exception) {
throw UserlandError::from($exception);
Expand Down
Loading

0 comments on commit 9ef3cfb

Please sign in to comment.