Skip to content

Commit

Permalink
Fixed PHPStan level 8 errors in TestUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
nkl-kst committed Feb 5, 2024
1 parent 16cb8f4 commit 9657d61
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions test/unit/Util/TestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,38 @@ public static function expectEndpoint(MockObject $requestBuilderMock, string $en
}

/**
* @param object $object Object to get property from
* @param object $object Object to get property from
* @param string $property Property to get
*
* @return mixed|null
*
* @throws ReflectionException
*/
public static function getHiddenProperty(object $object, string $property)
{
$ref = new ReflectionObject($object);

try {
$prop = $ref->hasProperty($property) ? $ref->getProperty($property) : null;
if (!$prop && $ref->getParentClass()) {
$prop = $ref->getParentClass()->getProperty($property);
}
} catch (ReflectionException $e) {
return null;
$prop = $ref->hasProperty($property) ? $ref->getProperty($property) : null;
if (!$prop && $ref->getParentClass()) {
$prop = $ref->getParentClass()->getProperty($property);
}
$prop->setAccessible(true);
assert(!is_null($prop));

$prop->setAccessible(true);
return $prop->getValue($object);
}

/**
* @param class-string $class Class to get static property from
* @param string $property Property to get
* @param class-string $class Class to get static property from
* @param string $property Property to get
*
* @return mixed|null
* @return mixed
*
* @throws ReflectionException
*/
public static function getHiddenStaticProperty(string $class, string $property)
{
try {
return (new ReflectionClass($class))->getStaticPropertyValue($property);
} catch (ReflectionException $e) {
return null;
}
return (new ReflectionClass($class))->getStaticPropertyValue($property);
}

/**
Expand All @@ -83,38 +80,41 @@ public static function setHiddenStaticProperty(string $class, string $property,
/**
* @param object $object Object to get method from
* @param string $method Method to get
*
* @throws ReflectionException
*/
public static function getHiddenMethod(object $object, string $method): ?Closure
public static function getHiddenMethod(object $object, string $method): Closure
{
$ref = new ReflectionObject($object);

try {
$meth = $ref->hasMethod($method) ? $ref->getMethod($method) : null;
if (!$meth && $ref->getParentClass()) {
$meth = $ref->getParentClass()->getMethod($method);
}
} catch (ReflectionException $e) {
return null;
$meth = $ref->hasMethod($method) ? $ref->getMethod($method) : null;
if (!$meth && $ref->getParentClass()) {
$meth = $ref->getParentClass()->getMethod($method);
}
assert(!is_null($meth));

$meth->setAccessible(true);
$closure = $meth->getClosure($object);
assert(!is_null($closure));

return $meth->getClosure($object);
return $closure;
}

/**
* @param class-string $class Class to get static method from
* @param string $method Method to get
* @param class-string $class Class to get static method from
* @param string $method Method to get
*
* @throws ReflectionException
*/
public static function getHiddenStaticMethod(string $class, string $method): ?Closure
public static function getHiddenStaticMethod(string $class, string $method): Closure
{
try {
$meth = (new ReflectionClass($class))->getMethod($method);
} catch (ReflectionException $e) {
return null;
}
$meth = (new ReflectionClass($class))->getMethod($method);
$meth->setAccessible(true);

return $meth->getClosure(null);
$closure = $meth->getClosure(null);
assert(!is_null($closure));

return $closure;
}

/**
Expand Down Expand Up @@ -142,7 +142,9 @@ public static function assertThatAllPropertiesAreInitialized($objects): void
$reflectionProperty = new ReflectionProperty($class, $property);

// Don't check property if null is allowed
if ($reflectionProperty->getType()->allowsNull()) {
$refPropType = $reflectionProperty->getType();
assert(!is_null($refPropType));
if ($refPropType->allowsNull()) {
continue;
}

Expand Down

0 comments on commit 9657d61

Please sign in to comment.