Skip to content

Commit a83b91f

Browse files
committed
Mark reflection parameter checks and type stringification as non-throwing
1 parent d9ad990 commit a83b91f

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎conf/config.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ parameters:
156156
- ../stubs/ReflectionMethod.stub
157157
- ../stubs/ReflectionParameter.stub
158158
- ../stubs/ReflectionProperty.stub
159+
- ../stubs/ReflectionType.stub
159160
- ../stubs/iterable.stub
160161
- ../stubs/ArrayObject.stub
161162
- ../stubs/WeakReference.stub

‎stubs/ReflectionParameter.stub‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ class ReflectionParameter
1616
{
1717
}
1818

19+
/**
20+
* @throws void
21+
*/
22+
public function isOptional(): bool
23+
{
24+
}
25+
1926
}

‎stubs/ReflectionType.stub‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
abstract class ReflectionType
4+
{
5+
6+
/**
7+
* @throws void
8+
*/
9+
public function __toString(): string
10+
{
11+
}
12+
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace ReflectionParameterTypeCatch;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/** @param class-string $className */
8+
function parameterType(string $className, string $propertyName): void
9+
{
10+
$sourceInterfaceReflection = new \ReflectionClass($className);
11+
$paramType = null;
12+
try {
13+
$reflectionMethod = $sourceInterfaceReflection->getMethod('set' . ucfirst($propertyName));
14+
$reflectionParams = $reflectionMethod->getParameters();
15+
if (isset($reflectionParams[0])) {
16+
$paramType = $reflectionParams[0]->getType();
17+
if (($paramType !== null) && $reflectionParams[0]->isOptional()) {
18+
$paramType = '?' . $paramType;
19+
}
20+
}
21+
22+
if ($paramType !== null) {
23+
$paramType = (string) $paramType;
24+
}
25+
} catch (\Exception $e) {
26+
}
27+
28+
assertType('string|null', $paramType);
29+
}
30+
31+
function stringifyType(\ReflectionType $type): void
32+
{
33+
$string = null;
34+
try {
35+
$string = (string) $type;
36+
} catch (\Exception $e) {
37+
}
38+
39+
assertType('string', $string);
40+
}
41+
42+
function checkOptional(\ReflectionParameter $parameter): void
43+
{
44+
$optional = null;
45+
try {
46+
$optional = $parameter->isOptional();
47+
} catch (\Exception $e) {
48+
}
49+
50+
assertType('bool', $optional);
51+
}

0 commit comments

Comments
 (0)