Skip to content

Commit ecd165a

Browse files
committed
[Maintenance] Fix PHPStan errors related to class-string
1 parent 8abbc69 commit ecd165a

File tree

6 files changed

+53
-11
lines changed

6 files changed

+53
-11
lines changed

phpstan.neon

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ includes:
55
- vendor/phpstan/phpstan-phpunit/rules.neon
66

77
parameters:
8-
checkGenericClassInNonGenericObjectType: false
9-
checkMissingIterableValueType: false
108
reportUnmatchedIgnoredErrors: false
119

1210
excludePaths:
@@ -72,7 +70,7 @@ parameters:
7270
- '/Parameter \#1 \$maxPerPage of method Pagerfanta\\Pagerfanta<mixed>::setMaxPerPage\(\) expects int<1, max>, int given\./'
7371
- '/Parameter \#1 \$array[0-9]? of function array_multisort expects array, array\|int given\./'
7472
- '/Parameter \#2 \$class of static method Webmozart\\Assert\\Assert::isInstanceOf\(\) expects class-string<object>, string given./'
75-
- '/Parameter \#1 \$objectOrClass of class ReflectionClass constructor expects class-string<object>|object, object|string given./'
73+
- '/Parameter \#1 \$objectOrClass of class ReflectionClass constructor expects class-string<object>\|object, object\|string given./'
7674
- '/Parameter \#1 \$package of method Symfony\\Component\\DependencyInjection\\Alias::setDeprecated\(\)/'
7775
- '/Return typehint of method Sylius\\Bundle\\ResourceBundle\\Routing\\CrudRoutesAttributesLoader::getClassAttributes\(\) has invalid type ReflectionAttribute./'
7876
- '/Return typehint of method Sylius\\Bundle\\ResourceBundle\\Routing\\RoutesAttributesLoader::getClassAttributes\(\) has invalid type ReflectionAttribute./'
@@ -102,3 +100,7 @@ parameters:
102100
- '/Parameter #1 \$submittedData of method Symfony\\Component\\Form\\FormInterface::submit\(\) expects array\|string\|null, mixed given\./'
103101
- '/Parameter #2 \$callback of function preg_replace_callback expects callable\(array<int\|string, string>\): string, Closure\(array\): mixed given\./'
104102
- '/Unable to resolve the template type T in call to method Doctrine\\Persistence\\ObjectManager::getClassMetadata\(\)/'
103+
-
104+
identifier: missingType.generics
105+
-
106+
identifier: missingType.iterableValue

src/Bundle/Routing/CrudRoutesAttributesLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ public function __invoke(): RouteCollection
3939
$routeCollection = new RouteCollection();
4040
$paths = $this->mapping['paths'] ?? [];
4141

42-
/** @var string $className */
4342
foreach (ClassReflection::getResourcesByPaths($paths) as $className) {
4443
$this->addRoutesForSyliusCrudRoutesAttributes($routeCollection, $className);
4544
}
4645

4746
return $routeCollection;
4847
}
4948

49+
/**
50+
* @param class-string $className
51+
*/
5052
private function addRoutesForSyliusCrudRoutesAttributes(RouteCollection $routeCollection, string $className): void
5153
{
5254
$attributes = ClassReflection::getClassAttributes($className, SyliusCrudRoutes::class);

src/Bundle/Routing/RoutesAttributesLoader.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function __invoke(): RouteCollection
3232
$routeCollection = new RouteCollection();
3333
$paths = $this->mapping['paths'] ?? [];
3434

35-
/** @var string $className */
3635
foreach (ClassReflection::getResourcesByPaths($paths) as $className) {
3736
$this->routesAttributesFactory->createRouteForClass($routeCollection, $className);
3837
$this->attributesOperationRouteFactory->createRouteForClass($routeCollection, $className);

src/Component/legacy/tests/Reflection/ClassInfoTraitTest.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,38 @@ private function getClassInfoTraitImplementation(): object
2828
};
2929
}
3030

31-
public function testDoctrineRealClassName(): void
31+
/**
32+
* @dataProvider getValidClasses
33+
*/
34+
public function testRealClassName(string $class): void
3235
{
3336
$classInfo = $this->getClassInfoTraitImplementation();
3437

35-
$this->assertSame(Book::class, $classInfo->getRealClassName('Proxies\__CG__\App\Entity\Book'));
38+
$this->assertSame(Book::class, $classInfo->getRealClassName($class));
3639
}
3740

38-
public function testProxyManagerRealClassName(): void
41+
/**
42+
* @dataProvider getInvalidClasses
43+
*/
44+
public function testThrowsExceptionIfUnproxiedClassDoNotExist(string $class): void
3945
{
46+
$this->expectException(\InvalidArgumentException::class);
47+
4048
$classInfo = $this->getClassInfoTraitImplementation();
4149

42-
$this->assertSame(Book::class, $classInfo->getRealClassName('MongoDBODMProxies\__PM__\App\Entity\Book\Generated'));
50+
$classInfo->getRealClassName($class);
51+
}
52+
53+
public function getInvalidClasses(): Iterable
54+
{
55+
yield ['class' => 'Proxies\__CG__\App\Entity\Book1'];
56+
yield ['class' => 'MongoDBODMProxies\__PM__\App\Entity\Book1\Generated'];
57+
}
58+
59+
public function getValidClasses(): Iterable
60+
{
61+
yield ['class' => 'Proxies\__CG__\App\Entity\Book'];
62+
yield ['class' => 'MongoDBODMProxies\__PM__\App\Entity\Book\Generated'];
4363
}
4464

4565
public function testUnmarkedRealClassName(): void

src/Component/src/Reflection/ClassInfoTrait.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace Sylius\Resource\Reflection;
1515

16+
use Webmozart\Assert\Assert;
17+
1618
/**
1719
* Retrieves information about a class.
1820
*
@@ -22,6 +24,8 @@ trait ClassInfoTrait
2224
{
2325
/**
2426
* Get class name of the given object.
27+
*
28+
* @return class-string
2529
*/
2630
private function getObjectClass(object $object): string
2731
{
@@ -30,6 +34,10 @@ private function getObjectClass(object $object): string
3034

3135
/**
3236
* Get the real class name of a class name that could be a proxy.
37+
*
38+
* @param class-string $className
39+
*
40+
* @return class-string
3341
*/
3442
private function getRealClassName(string $className): string
3543
{
@@ -43,15 +51,23 @@ private function getRealClassName(string $className): string
4351
}
4452

4553
if (false !== $positionCg) {
46-
return substr($className, $positionCg + 8);
54+
$unProxiedClassName = substr($className, $positionCg + 8);
55+
56+
Assert::classExists($unProxiedClassName);
57+
58+
return $unProxiedClassName;
4759
}
4860

4961
$className = ltrim($className, '\\');
5062

51-
return substr(
63+
$unProxiedClassName = substr(
5264
$className,
5365
8 + $positionPm,
5466
strrpos($className, '\\') - ($positionPm + 8),
5567
);
68+
69+
Assert::classExists($unProxiedClassName);
70+
71+
return $unProxiedClassName;
5672
}
5773
}

src/Component/src/Reflection/ClassReflection.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
final class ClassReflection
1919
{
20+
/**
21+
* @return \Generator<class-string>
22+
*/
2023
public static function getResourcesByPaths(array $paths): iterable
2124
{
2225
foreach ($paths as $resourceDirectory) {

0 commit comments

Comments
 (0)