Skip to content

Commit

Permalink
Upgrade PHPStan to level 8
Browse files Browse the repository at this point in the history
  • Loading branch information
nkl-kst committed Jun 28, 2024
1 parent 1f6445c commit c6dcefe
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 7
level: 8
paths:
- src
- test
7 changes: 4 additions & 3 deletions src/Dependency/DependencyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use NklKst\TheSportsDb\Client\Endpoint\LookupEndpoint;
use NklKst\TheSportsDb\Client\Endpoint\ScheduleEndpoint;
use NklKst\TheSportsDb\Client\Endpoint\SearchEndpoint;
use NklKst\TheSportsDb\Exception\ClientInstantiationException;
use NklKst\TheSportsDb\Request\RequestBuilder;
use NklKst\TheSportsDb\Serializer\CountrySerializer;
use NklKst\TheSportsDb\Serializer\EquipmentSerializer;
Expand Down Expand Up @@ -72,7 +73,7 @@ class DependencyContainer
TimelineSerializer::class,
];

private static ?ContainerBuilder $builder = null;
private static ContainerBuilder $builder;

private static function load(): void
{
Expand Down Expand Up @@ -127,7 +128,7 @@ private static function compile(): void
self::$builder->compile();
}

public static function getClient(): ?Client
public static function getClient(): Client
{
// Load dependency container
self::load();
Expand All @@ -139,6 +140,6 @@ public static function getClient(): ?Client
}

// Something went wrong...
return null;
throw new ClientInstantiationException();
}
}
7 changes: 7 additions & 0 deletions src/Exception/ClientInstantiationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace NklKst\TheSportsDb\Exception;

use RuntimeException;

class ClientInstantiationException extends RuntimeException {}
2 changes: 1 addition & 1 deletion src/Serializer/AbstractSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract protected function getValidJsonRootNames(): array;
/**
* Get the JSON root property containing data.
*/
private function getJsonRootName(?object $json): ?string
private function getJsonRootName(object $json): ?string
{
foreach ($this->getValidJsonRootNames() as $rootName) {
if (property_exists($json, $rootName)) {
Expand Down
68 changes: 32 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,35 @@ 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);
}
$meth->setAccessible(true);
assert(!is_null($meth));

$meth->setAccessible(true);
return $meth->getClosure($object);
}

/**
* @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);
return $meth->getClosure();
}

/**
Expand Down Expand Up @@ -142,7 +136,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 c6dcefe

Please sign in to comment.