Skip to content

Commit 5a3c331

Browse files
authored
Merge pull request #343 from jolicode/feat/phpstan-2
chore(lint): update to phpstan 2 / update some types
2 parents ec0026f + cc2b1a1 commit 5a3c331

126 files changed

Lines changed: 404 additions & 330 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

castor.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,37 @@
1111
use function Castor\PHPQa\phpstan;
1212
use function Castor\run;
1313

14+
const PHP_CS_FIXER_VERSION = '3.95.1';
15+
1416
#[AsTask('cs:check', namespace: 'qa', description: 'Check for coding standards without fixing them')]
15-
function qa_cs_check()
17+
function qa_cs_check(): void
1618
{
17-
php_cs_fixer(['fix', '--config', __DIR__ . '/.php-cs-fixer.php', '--dry-run', '--diff'], '3.92.3', [
19+
php_cs_fixer(['fix', '--config', __DIR__ . '/.php-cs-fixer.php', '--dry-run', '--diff'], PHP_CS_FIXER_VERSION, [
1820
'kubawerlos/php-cs-fixer-custom-fixers' => '^3.21',
1921
]);
2022
}
2123

2224
#[AsTask('cs:fix', namespace: 'qa', description: 'Fix all coding standards', aliases: ['cs'])]
23-
function qa_cs_fix()
25+
function qa_cs_fix(): void
2426
{
25-
php_cs_fixer(['fix', '--config', __DIR__ . '/.php-cs-fixer.php', '-v'], '3.92.3', [
27+
php_cs_fixer(['fix', '--config', __DIR__ . '/.php-cs-fixer.php', '-v'], PHP_CS_FIXER_VERSION, [
2628
'kubawerlos/php-cs-fixer-custom-fixers' => '^3.21',
2729
]);
2830
}
2931

3032
#[AsTask('phpstan', namespace: 'qa', description: 'Run PHPStan for static analysis', aliases: ['phpstan'])]
31-
function qa_phpstan(bool $generateBaseline = false)
33+
function qa_phpstan(bool $generateBaseline = false): void
3234
{
3335
$params = ['analyse', '--configuration', __DIR__ . '/phpstan.neon', '--memory-limit=-1', '-v'];
3436
if ($generateBaseline) {
3537
$params[] = '--generate-baseline';
3638
}
3739

38-
phpstan($params, '1.12.23');
40+
phpstan($params, '2.1.51');
3941
}
4042

4143
#[AsTask('mapper', namespace: 'debug', description: 'Debug a mapper', aliases: ['debug'])]
42-
function debug_mapper(string $source, string $target, string $load = '')
44+
function debug_mapper(string $source, string $target, string $load = ''): void
4345
{
4446
require_once __DIR__ . '/vendor/autoload.php';
4547

@@ -79,13 +81,13 @@ function debug_mapper(string $source, string $target, string $load = '')
7981
}
8082

8183
#[AsTask('install', namespace: 'doc', description: 'Install tool for documentation (need poetry)')]
82-
function doc_install()
84+
function doc_install(): void
8385
{
8486
run('poetry install');
8587
}
8688

8789
#[AsTask('server', namespace: 'doc', description: 'Serve documentation')]
88-
function doc_serve()
90+
function doc_serve(): void
8991
{
9092
run('poetry run mkdocs serve -a localhost:8000');
9193
}
@@ -113,7 +115,7 @@ function build_assets(): void
113115
}
114116

115117
#[AsTask('build-github-pages', namespace: 'doc', description: 'Serve documentation')]
116-
function doc_build_github_pages()
118+
function doc_build_github_pages(): void
117119
{
118120
// clean .build directory
119121
run('rm -rf ./.build', context: context()->withAllowFailure());

phpstan.neon

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,49 @@
11
parameters:
2-
level: max
3-
paths:
4-
- src/
2+
level: max
3+
paths:
4+
- src/
55

6-
tmpDir: cache
6+
tmpDir: cache
7+
treatPhpDocTypesAsCertain: false
78

8-
ignoreErrors:
9-
-
10-
message: "#^Method AutoMapper\\\\ObjectMapper\\\\ObjectMapper\\:\\:map\\(\\) should return T of object but returns object\\|null\\.$#"
11-
count: 1
12-
path: src/ObjectMapper/ObjectMapper.php
9+
ignoreErrors:
10+
-
11+
message: "#^Method AutoMapper\\\\ObjectMapper\\\\ObjectMapper\\:\\:map\\(\\) should return T of object but returns object\\|null\\.$#"
12+
count: 1
13+
path: src/ObjectMapper/ObjectMapper.php
14+
15+
-
16+
message: '#^Parameter \#2 \$array of function implode expects array\<string\>, array given\.$#'
17+
identifier: argument.type
18+
count: 1
19+
path: src/EventListener/MapListener.php
20+
21+
-
22+
message: '#^Binary operation "\." between mixed and string results in an error\.$#'
23+
identifier: binaryOp.invalid
24+
count: 1
25+
path: src/EventListener/MapToContextListener.php
26+
27+
-
28+
message: '#^Parameter \$mapping of class AutoMapper\\Metadata\\Discriminator constructor expects array\<string, class\-string\>, array given\.$#'
29+
identifier: argument.type
30+
count: 2
31+
path: src/EventListener/Symfony/ClassDiscriminatorListener.php
32+
33+
-
34+
message: '#^Call to function function_exists\(\) with callable\-string will always evaluate to true\.$#'
35+
identifier: function.alreadyNarrowedType
36+
count: 1
37+
path: src/Generator/PropertyConditionsGenerator.php
38+
39+
-
40+
message: '#^Method AutoMapper\\Loader\\FileLoader\:\:getRegistry\(\) should return array\<class\-string, string\> but returns array\.$#'
41+
identifier: return.type
42+
count: 1
43+
path: src/Loader/FileLoader.php
44+
45+
-
46+
message: '#^Property AutoMapper\\Loader\\FileLoader\:\:\$registry \(array\<class\-string, string\>\) does not accept mixed\.$#'
47+
identifier: assign.propertyType
48+
count: 1
49+
path: src/Loader/FileLoader.php

src/Attribute/MapFrom.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
final readonly class MapFrom
1414
{
1515
/**
16-
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $source The specific source class name or array. If null this attribute will be used for all source classes.
17-
* @param string|null $property The source property name. If null, the target property name will be used.
18-
* @param int|null $maxDepth The maximum depth of the mapping. If null, the default max depth will be used.
19-
* @param string|callable(mixed $value, object|array<string, mixed> $source, array<string, mixed> $context): mixed $transformer A transformer id or a callable that transform the value during mapping
20-
* @param bool|null $ignore If true, the property will be ignored during mapping
21-
* @param string|null $if The condition to map the property, using the expression language
22-
* @param string[]|null $groups The groups to map the property
23-
* @param string|null $dateTimeFormat The date-time format to use when transforming this property
24-
* @param bool|null $extractTypesFromGetter If true, the types will be extracted from the getter method
25-
* @param bool|null $identifier If true, the property will be used as an identifier
26-
* @param Type|string|null $sourcePropertyType Override the source property type, where this property is mapped from
27-
* @param Type|string|null $targetPropertyType Override the target property type, which in this case is the property type where the attribute is defined
16+
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $source The specific source class name or array. If null this attribute will be used for all source classes.
17+
* @param string|null $property The source property name. If null, the target property name will be used.
18+
* @param int|null $maxDepth The maximum depth of the mapping. If null, the default max depth will be used.
19+
* @param string|callable(mixed $value, object|array<string, mixed> $source, array<string, mixed> $context): mixed|null $transformer A transformer id or a callable that transform the value during mapping
20+
* @param bool|null $ignore If true, the property will be ignored during mapping
21+
* @param string|null $if The condition to map the property, using the expression language
22+
* @param string[]|null $groups The groups to map the property
23+
* @param string|null $dateTimeFormat The date-time format to use when transforming this property
24+
* @param bool|null $extractTypesFromGetter If true, the types will be extracted from the getter method
25+
* @param bool|null $identifier If true, the property will be used as an identifier
26+
* @param Type|string|null $sourcePropertyType Override the source property type, where this property is mapped from
27+
* @param Type|string|null $targetPropertyType Override the target property type, which in this case is the property type where the attribute is defined
2828
*/
2929
public function __construct(
3030
public string|array|null $source = null,

src/AttributeReference/ReflectionReference.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public static function fromMethod(string $className, string $methodName): self
4646
return self::$method[$key];
4747
}
4848

49+
/**
50+
* @param class-string $className
51+
*/
4952
public static function fromProperty(string $className, string $propertyName): self
5053
{
5154
$key = $className . '::$' . $propertyName;

src/AutoMapper.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,9 @@ public function getMapper(string $source, string $target): MapperInterface
8585

8686
public function map(array|object $source, string|array|object $target, array $context = []): array|object|null
8787
{
88-
$sourceType = $targetType = null;
89-
9088
if (\is_object($source)) {
91-
/** @var class-string<object> $sourceType */
9289
$sourceType = $source::class;
93-
} elseif (\is_array($source)) {
90+
} else {
9491
$sourceType = 'array';
9592
}
9693

@@ -102,7 +99,7 @@ public function map(array|object $source, string|array|object $target, array $co
10299
$targetType = 'array';
103100
$context[MapperContext::TARGET_TO_POPULATE] = $target;
104101
$context[MapperContext::DEEP_TARGET_TO_POPULATE] ??= true;
105-
} elseif (\is_string($target)) {
102+
} else {
106103
$targetType = $target;
107104
}
108105

@@ -116,6 +113,7 @@ public function map(array|object $source, string|array|object $target, array $co
116113
public function mapCollection(iterable $collection, string $target, array $context = []): array
117114
{
118115
$output = [];
116+
/** @var string|int $k */
119117
foreach ($collection as $k => $item) {
120118
if (\is_array($item) && 'array' === $target) {
121119
throw new InvalidMappingException('Cannot map this value, both source and target are array.');

src/EventListener/MapListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected function getTransformerFromMapAttribute(string $class, MapTo|MapFrom $
4949
$transformer = new PropertyTransformer($transformerCallable);
5050
} elseif (!\is_object($transformerCallable) && \is_callable($transformerCallable, false, $callableName)) {
5151
$transformer = new CallableTransformer($callableName);
52-
} elseif (\is_object($transformerCallable) && \is_callable($transformerCallable)) {
52+
} elseif (\is_callable($transformerCallable)) {
5353
$transformer = new ReferenceTransformer($reference);
5454
} elseif (\is_string($transformerCallable) && method_exists($class, $transformerCallable)) {
5555
$reflMethod = new \ReflectionMethod($class, $transformerCallable);

src/EventListener/MapperListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private function getMapperAttribute(GenerateMapperEvent $event, bool $allowParen
105105
}
106106

107107
// sort by priority
108-
usort($mappers, fn (array $a, array $b) => $a[0]->priority <=> $b[0]->priority);
108+
usort($mappers, static fn (array $a, array $b) => $a[0]->priority <=> $b[0]->priority);
109109

110110
return $mappers[0];
111111
}

src/Extractor/AddRemoveWriteMutator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getExpression(Expr $output, Expr $value, bool $byRef = false): E
2222
]);
2323
}
2424

25-
public function getRemoveExpression(Expr $object, Expr $value): ?Expr
25+
public function getRemoveExpression(Expr $object, Expr $value): Expr
2626
{
2727
return new Expr\MethodCall($object, $this->removeMethodName, [
2828
new Arg($value),

src/Extractor/ArrayReadAccessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getExpression(Expr $input, bool $target = false): Expr
2222
return new Expr\ArrayDimFetch($input, new Scalar\String_($this->property));
2323
}
2424

25-
public function getIsDefinedExpression(Expr\Variable $input, bool $nullable = false, bool $target = false): ?Expr
25+
public function getIsDefinedExpression(Expr\Variable $input, bool $nullable = false, bool $target = false): Expr
2626
{
2727
if ($this->isArrayAccess) {
2828
return new Expr\MethodCall($input, 'offsetExists', [new Arg(new Scalar\String_($this->property))]);

src/Extractor/FromSourceMappingExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function transformSourceType(string $target, ?Type $type = null): ?Type
104104
}
105105

106106
// Transform datetime to string
107-
if ($type instanceof Type\ObjectType && $type->getClassName() !== null && (\DateTimeInterface::class === $type->getClassName() || is_subclass_of($type->getClassName(), \DateTimeInterface::class))) {
107+
if ($type instanceof Type\ObjectType && (\DateTimeInterface::class === $type->getClassName() || is_subclass_of($type->getClassName(), \DateTimeInterface::class))) {
108108
return Type::string();
109109
}
110110

0 commit comments

Comments
 (0)