Skip to content

Commit

Permalink
Reflecty 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeghe committed Dec 26, 2024
1 parent decfb6f commit feeca97
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Includes the following helpers:
- [Levex](https://github.com/nabeghe/levex-php) <small>v1.0.1</small>
- [Matcher](https://github.com/nabeghe/matcher-php)<small> v1.0.0</small>
- [Mem](https://github.com/nabeghe/mem-php) <small>v1.0.0</small>
- [Reflecty](https://github.com/nabeghe/reflecty-php) <small>v0.1.1</small>
- [Reflecty](https://github.com/nabeghe/reflecty-php) <small>v0.2.0</small>
- [Servery](https://github.com/nabeghe/servery-php) <small>v0.1.1</small>
- [Shortnum](https://github.com/nabeghe/shortnum-php) <small>v0.1.0</small>
- [SimpleCipher](https://github.com/nabeghe/simple-cipher-php) <small>v1.0.0</small>
Expand Down
117 changes: 109 additions & 8 deletions src/Reflecty/Reflecty.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static function classFullname($class)
public static function classBasename($class)
{
$class = static::classFullname($class);

return basename(str_replace('\\', '/', $class));
}

Expand Down Expand Up @@ -80,6 +81,7 @@ public static function classAncestors($class): array
while ($class = get_parent_class($class)) {
$ancestors[] = $class;
}

return $ancestors;
}

Expand Down Expand Up @@ -133,6 +135,7 @@ public static function traitExists($class, $trait): bool
if (is_object($class)) {
$class = get_class($class);
}

return in_array($trait, static::classUsesRecursive($class));
}

Expand Down Expand Up @@ -182,10 +185,11 @@ public static function constantExists($class, $constant, $visibility = null)
{
if ($visibility) {
$constantNames = static::constants($class, $visibility) ?? [];

return in_array($constant, $constantNames);
} else {
return defined(static::classFullname($class).'::'.$constant);
}

return defined(static::classFullname($class).'::'.$constant);
}

/**
Expand Down Expand Up @@ -232,14 +236,17 @@ public static function propertyAccessible($class, $property, ?bool $accessible =
try {
$r = new ReflectionClass($class);
$prop = $r->getProperty($property);

if ($accessible === null) {
return $prop->isPublic();
}

$prop->setAccessible($accessible);

return true;
} catch (ReflectionException $e) {
return null;
}
return null;
}

/**
Expand All @@ -252,10 +259,11 @@ public static function propertiesCount($class)
{
try {
$r = new ReflectionClass($class);

return count($r->getProperties());
} catch (\Throwable $e) {
return null;
}
return null;
}

/**
Expand All @@ -265,10 +273,11 @@ public static function propertiesCount($class)
* @param string $property
* @return bool|null
*/
public static function hasProperty($class, $property): ?bool
public static function propertyExists($class, $property): ?bool
{
try {
$r = new ReflectionClass($class);

return $r->hasProperty($property);
} catch (ReflectionException $e) {
return null;
Expand All @@ -283,18 +292,25 @@ public static function hasProperty($class, $property): ?bool
* @param mixed $default
* @return \ReflectionProperty|null
*/
public static function getProperty($class, $property, $default = null)
public static function property($class, $property, $default = null)
{
try {
$r = new ReflectionClass($class);

if ($r->hasProperty($property)) {
return $r->getProperty($property);
}
} catch (ReflectionException $e) {
}

return $default;
}

public static function getProperty($class, $property, $default = null)
{
return static::property($class, $property, $default);
}

/**
* Returns a property value of the given object/class.
*
Expand All @@ -303,18 +319,25 @@ public static function getProperty($class, $property, $default = null)
* @param mixed $default
* @return \ReflectionProperty|null
*/
public static function getPropertyValue($object, $property, $default = null)
public static function propertyValue($object, $property, $default = null)
{
try {
$r = new ReflectionClass($object);

if ($r->hasProperty($property)) {
return $r->getProperty($property)->getValue($object);
}
} catch (ReflectionException $e) {
} catch (\Throwable $e) {
}

return $default;
}

public static function getPropertyValue($object, $property, $default = null)
{
return static::propertyValue($object, $property, $default);
}

/**
* Returns the number of methods in a class/object.
*
Expand All @@ -325,9 +348,11 @@ public static function methodsCount($class)
{
try {
$r = new ReflectionClass($class);

return count($r->getMethods());
} catch (ReflectionException) {
}

return null;
}

Expand All @@ -344,13 +369,17 @@ public static function methodAccessible($class, $method, ?bool $accessible = nul
try {
$r = new ReflectionClass($class);
$method = $r->getMethod($method);

if ($accessible === null) {
return $method->isPublic();
}

$method->setAccessible($accessible);

return true;
} catch (ReflectionException) {
}

return null;
}

Expand All @@ -365,6 +394,7 @@ public static function methodParams($class, $method)
{
try {
$r = new ReflectionMethod($class, $method);

return $r->getParameters();
} catch (ReflectionException) {
return null;
Expand All @@ -381,6 +411,7 @@ public static function methodParams($class, $method)
public static function methodParamNames($class, $method)
{
$prams = static::methodParams($class, $method);

return $prams === null ? null : array_map(fn($param) => $param->getName(), $prams);
}

Expand All @@ -397,12 +428,14 @@ public static function methodOptionalParams($class, $method)
if ($params === null) {
return null;
}

$optionals = [];
foreach ($params as $param) {
if ($param->isOptional()) {
$optionals[] = $param;
}
}

return $optionals;
}

Expand All @@ -416,6 +449,74 @@ public static function methodOptionalParams($class, $method)
public static function methodOptionalParamNames($class, $method)
{
$params = static::methodOptionalParams($class, $method);

return $params === null ? null : array_map(fn($param) => $param->getName(), $params);
}

/**
* Returns the case of based on its name.
*
* @param string $enum Thee enum.
* @param string $name The case name.
* @return false|null
*/
public static function enumByName(string $enum, string $name)
{
if (!$enum || !function_exists('enum_exists') || !enum_exists($enum)) {
return false;
}

/** @noinspection PhpUndefinedMethodInspection */
foreach ($enum::cases() as $case) {
if ($case->name === $name) {
return $case;
}
}

return null;
}

/**
* Checks whether a name exists as a case in the enum or not.
*
* @param string $enum
* @param string $name
* @return bool
*/
public static function enumHasName(string $enum, string $name): bool
{
return static::enumByName($name, $enum) !== null;
}

/**
* Returns an array of the enum case names.
*
* @param string $enum
* @return array|null
*/
public static function enumNames(string $enum)
{
if (!$enum || !function_exists('enum_exists') || !enum_exists($enum)) {
return null;
}

/** @noinspection PhpUndefinedMethodInspection */
return array_column($enum::cases(), 'name');
}

/**
* Returns an array of the enum case values.
*
* @param string $enum
* @return array|null
*/
public static function enumValues(string $enum)
{
if (!$enum || !function_exists('enum_exists') || !enum_exists($enum)) {
return null;
}

/** @noinspection PhpUndefinedMethodInspection */
return array_column($enum::cases(), 'value');
}
}

0 comments on commit feeca97

Please sign in to comment.