Skip to content

Commit

Permalink
phpstan fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Dec 25, 2022
1 parent 26990a5 commit 82a2cdd
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Helpers/ObjectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
use ReflectionProperty;
use stdClass;

/**
* Helper for accessing object properties and methods
*/
class ObjectHelper
{
/**
* @param object $object
* @param string $propertyName
* @return bool
*/
public static function hasPublicProperty(object $object, string $propertyName): bool
{
if ($object instanceof stdClass) {
Expand All @@ -19,43 +27,82 @@ public static function hasPublicProperty(object $object, string $propertyName):
static::getReflectionProperty($object, $propertyName)->isPublic();
}

/**
* @param object $object
* @param string $methodName
* @return bool
*/
public static function hasPublicMethod(object $object, string $methodName): bool
{
return
static::hasMethod($object, $methodName) &&
static::getReflectionMethod($object, $methodName)->isPublic();
}

/**
* @param object $object
* @param string $propertyName
* @return bool
*/
public static function hasPropertyAccessibleByGetter(object $object, string $propertyName): bool
{
return static::hasPublicMethod($object, static::getPropertyGetterName($propertyName));
}

/**
* @param object $object
* @param string $propertyName
* @return mixed
*/
public static function getPropertyByGetter(object $object, string $propertyName)
{
return $object->{static::getPropertyGetterName($propertyName)}();
}

/**
* @param object $object
* @param string $propertyName
* @return bool
*/
public static function hasProperty(object $object, string $propertyName): bool
{
return property_exists($object, $propertyName);
}

/**
* @param object $object
* @param string $methodName
* @return bool
*/
public static function hasMethod(object $object, string $methodName): bool
{
return method_exists($object, $methodName);
}

/**
* @param object $object
* @param string $propertyName
* @return ReflectionProperty
*/
protected static function getReflectionProperty(object $object, string $propertyName): ReflectionProperty
{
return new ReflectionProperty(get_class($object), $propertyName);
}

/**
* @param object $object
* @param string $methodName
* @return ReflectionMethod
*/
protected static function getReflectionMethod(object $object, string $methodName): ReflectionMethod
{
return new ReflectionMethod(get_class($object), $methodName);
}

/**
* @param string $propertyName
* @return string
*/
protected static function getPropertyGetterName(string $propertyName): string
{
return 'get'.ucfirst($propertyName);
Expand Down

0 comments on commit 82a2cdd

Please sign in to comment.