Skip to content

Commit

Permalink
Fix phpdoc Helper classes. (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Mar 7, 2023
1 parent a237e0b commit 7dbdbc8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
68 changes: 36 additions & 32 deletions src/Helper/ArrayHelper.php
Expand Up @@ -62,19 +62,24 @@ static function (array|object $element) use ($name): mixed {
/**
* Retrieves the value of an array element or object property with the given key or property name.
*
* If the key does not exist in the array, the default value will be returned instead.
* If the key doesn't exist in the array, the default value will be returned instead.
*
* Not used when getting value from an object.
*
* The key may be specified in a dot format to retrieve the value of a sub-array or the property
* of an embedded object. In particular, if the key is `x.y.z`, then the returned value would
* be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']`
* or `$array->x` is neither an array nor an object, the default value will be returned.
* Note that if the array already has an element `x.y.z`, then its value will be returned
* instead of going through the sub-arrays. So it is better to be done specifying an array of key names
* like `['x', 'y', 'z']`.
* The key may be specified in a dot format to retrieve the value of a sub-array or the property of an embedded
* object.
*
* Below are some usage examples,
* In particular, if the key is `x.y.z`, then the returned value would be `$array['x']['y']['z']` or
* `$array->x->y->z` (if `$array` is an object).
*
* If `$array['x']` or `$array->x` is neither an array nor an object, the default value will be returned.
*
* Note that if the array already has an element `x.y.z`, then its value will be returned instead of going through
* the sub-arrays.
*
* So it's better to be done specifying an array of key names like `['x', 'y', 'z']`.
*
* Below are some usage examples.
*
* ```php
* // working with array
Expand All @@ -94,9 +99,8 @@ static function (array|object $element) use ($name): mixed {
* @param array|object $array Array or object to extract value from.
* @param Closure|string $key Key name of the array element, an array of keys or property name of the object, or an
* anonymous function returning the value. The anonymous function signature should be:
*
* `function($array, $defaultValue)`.
* @param mixed|null $default The default value to be returned if the specified array key does not exist. Not used
* @param mixed|null $default The default value to be returned if the specified array key doesn't exist. Not used
* when getting value from an object.
*
* @return mixed The value of the element if found, default value otherwise
Expand All @@ -122,8 +126,10 @@ public static function getValueByPath(object|array $array, Closure|string $key,
}

if (is_object($array)) {
// this is expected to fail if the property does not exist, or __get() is not implemented
// it is not reliably possible to check whether a property is accessible beforehand
/**
* This is expected to fail if the property doesn't exist, or __get() isn't implemented it isn't reliably
* possible to check whether a property is accessible beforehand
*/
return $array->$key;
}

Expand All @@ -137,10 +143,10 @@ public static function getValueByPath(object|array $array, Closure|string $key,
/**
* Indexes and/or groups the array according to a specified key.
*
* The input should be either multidimensional array or an array of objects.
* The input should be either a multidimensional array or an array of objects.
*
* The $key can be either a key name of the sub-array, a property name of object, or an anonymous function that must
* return the value that will be used as a key.
* The $key can be either a key name of the sub-array, a property name of an object, or an anonymous function that
* must return the value that will be used as a key.
*
* $groups is an array of keys, that will be used to group the input array into one or more sub-arrays based on keys
* specified.
Expand Down Expand Up @@ -213,17 +219,17 @@ public static function getValueByPath(object|array $array, Closure|string $key,
*
* @param array $array The array that needs to be indexed or grouped.
* @param string|null $key The column name or anonymous function which result will be used to index the array.
* @param array $groups The array of keys, that will be used to group the input array by one or more keys. If the
* $key attribute or its value for the particular element is null and $groups is not defined, the array element will
* be discarded. Otherwise, if $groups is specified, array element will be added to the result array without any
* key.
* @param array $groups The array of keys that will be used to group the input array by one or more keys. If the
* $key attribute or its value for the particular element is null and $groups aren't defined.
* The array element will be discarded.
* Otherwise, if $groups are specified, an array element will be added to the result array without any key.
*
* @throws Exception
*
* @return array The indexed and/or grouped array.
*
* @psalm-param array[] $array The array that needs to be indexed or grouped.
* @psalm-param string[] $groups The array of keys, that will be used to group the input array by one or more keys.
* @psalm-param string[] $groups The array of keys that will be used to group the input array by one or more keys.
*
* @psalm-suppress MixedArrayAssignment
*/
Expand Down Expand Up @@ -268,11 +274,11 @@ public static function index(array $array, string|null $key = null, array $group
*
* An array is associative if all its keys are strings.
*
* Note that an empty array will NOT be considered associative.
* Note that an empty array won't be considered associative.
*
* @param array $array The array being checked
* @param array $array The array being checked.
*
* @return bool Whether the array is associative
* @return bool Whether the array is associative.
*/
public static function isAssociative(array $array): bool
{
Expand Down Expand Up @@ -311,8 +317,8 @@ public static function multisort(
SORT_NUMERIC,

/**
* This fix is used for cases when main sorting specified by columns has equal values without it will lead
* to Fatal Error: Nesting level too deep - recursive dependency?
* This fix is used for cases when the main sorting specified by columns has equal values without it will
* lead to Fatal Error: Nesting level too deep - recursive dependency?
*/
range(1, count($array)),
SORT_ASC,
Expand All @@ -322,16 +328,14 @@ public static function multisort(
}

/**
* @todo - Write correct description
* Converts the array with data, using index-key if needed
* Returns the value of an array element or object property with the given path.
*
* This method is internally used to convert the data fetched from database into the format as required by this
* This method is internally used to convert the data fetched from a database into the format as required by this
* query.
*
* @param array $rows the raw query result from database.
* @psalm-suppress MixedArrayOffset
* @param array $rows The raw query result from a database.
*
* @return array the converted query result.
* @psalm-suppress MixedArrayOffset
*/
public static function populate(array $rows, Closure|string|null $indexBy = null): array
{
Expand Down
8 changes: 4 additions & 4 deletions src/Helper/StringHelper.php
Expand Up @@ -17,19 +17,19 @@
final class StringHelper
{
/**
* Returns the trailing name component of a path.
* Returns the trailing name part of a path.
*
* This method is similar to the php function `basename()` except that it will treat both \ and / as directory
* separators, independent of the operating system.
*
* This method was mainly created to work on php namespaces. When working with real file paths, PHP's `basename()`
* should work fine for you.
*
* Note: this method is not aware of the actual filesystem, or path components such as "..".
* Note: this method isn't aware of the actual filesystem, or path components such as "..".
*
* @param string $path A path string.
*
* @return string The trailing name component of the given path.
* @return string The trailing name part of the given path.
*
* @link http://www.php.net/manual/en/function.basename.php
*/
Expand All @@ -46,7 +46,7 @@ public static function baseName(string $path): string
}

/**
* Returns string representation of a number value without thousands separators and with dot as decimal separator.
* Returns string representation of a number value without a thousand separators and with dot as decimal separator.
*
* @param float|string $value The number value to be normalized.
*/
Expand Down

0 comments on commit 7dbdbc8

Please sign in to comment.