Skip to content

Commit

Permalink
typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Smetannikov committed Jul 7, 2020
1 parent cd626f0 commit 923fa57
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 88 deletions.
18 changes: 9 additions & 9 deletions src/Arr.php
Expand Up @@ -158,10 +158,10 @@ public static function flat(array $array, bool $preserveKeys = true): array
array_walk_recursive(
$array,
/**
* @param string $value
* @param mixed $value
* @param string|int $key
*/
function ($value, $key) use (&$flattened, $preserveKeys) {
static function ($value, $key) use (&$flattened, $preserveKeys): void {
if ($preserveKeys && !is_int($key)) {
$flattened[$key] = $value;
} else {
Expand Down Expand Up @@ -227,13 +227,13 @@ public static function search(array $array, $search, ?string $field = null)
* Returns an array containing all the elements of arr1 after applying
* the callback function to each one.
*
* @param array $array An array to run through the callback function
* @param Closure $callback Callback function to run for each element in each array
* @param bool $onNoScalar Whether or not to call the callback function on non scalar values
* @param array $array An array to run through the callback function
* @param callable $callback Callback function to run for each element in each array
* @param bool $onNoScalar Whether or not to call the callback function on non scalar values
* (Objects, resources, etc)
* @return array
*/
public static function mapDeep(array $array, $callback, bool $onNoScalar = false): array
public static function mapDeep(array $array, callable $callback, bool $onNoScalar = false): array
{
foreach ($array as $key => $value) {
if (is_array($value)) {
Expand All @@ -253,7 +253,7 @@ public static function mapDeep(array $array, $callback, bool $onNoScalar = false
* @param array $haystack
* @return array
*/
public static function clean($haystack): array
public static function clean(array $haystack): array
{
return array_filter($haystack);
}
Expand Down Expand Up @@ -285,7 +285,7 @@ public static function cleanBeforeJson(array $array): array
* @param array $array
* @return bool
*/
public static function isAssoc($array): bool
public static function isAssoc(array $array): bool
{
return array_keys($array) !== range(0, count($array) - 1);
}
Expand Down Expand Up @@ -364,7 +364,7 @@ public static function groupByKey(array $arrayList, string $key = 'id'): array
* @param array $array
* @return array
*/
public static function map($function, $array): array
public static function map(Closure $function, array $array): array
{
$result = [];

Expand Down
2 changes: 2 additions & 0 deletions src/Cli.php
Expand Up @@ -61,6 +61,7 @@ public static function out(string $message, bool $addEol = true): bool
return true;
}

/** @phan-suppress-next-line PhanPluginRemoveDebugEcho */
echo $message;
return false;
}
Expand All @@ -83,6 +84,7 @@ public static function err(string $message, bool $addEol = true): bool
return true;
}

/** @phan-suppress-next-line PhanPluginRemoveDebugEcho */
echo $message;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dates.php
Expand Up @@ -113,7 +113,7 @@ public static function is(?string $date): bool
/**
* Convert time for sql format
*
* @param null|int $time
* @param string|int|null $time
* @return string
*/
public static function sql($time = null): string
Expand Down
2 changes: 1 addition & 1 deletion src/Email.php
Expand Up @@ -148,7 +148,7 @@ public static function getDomainSorted(array $emails): array
*/
public static function getGravatarUrl(string $email, int $size = 32, string $defaultImage = 'identicon'): ?string
{
if (empty($email) || self::isValid($email) === false) {
if (empty($email) || !self::isValid($email)) {
return null;
}

Expand Down
20 changes: 12 additions & 8 deletions src/FS.php
Expand Up @@ -303,7 +303,7 @@ public static function ls(string $dir): array
* @param int $decimals The number of decimal points to include
* @return string
*/
public static function format(int $bytes, $decimals = 2): string
public static function format(int $bytes, int $decimals = 2): string
{
$exp = 0;
$value = 0;
Expand Down Expand Up @@ -393,11 +393,15 @@ protected static function chmod(string $filename, int $perm, int $add): bool
}

/**
* @param string $path
* @param string|null $path
* @return string
*/
public static function ext($path): string
public static function ext(?string $path): string
{
if (!$path) {
return '';
}

if (strpos($path, '?') !== false) {
$path = (string)preg_replace('#\?(.*)#', '', $path);
}
Expand All @@ -412,7 +416,7 @@ public static function ext($path): string
* @param string|null $path
* @return string
*/
public static function base($path): string
public static function base(?string $path): string
{
return (string)pathinfo((string)$path, PATHINFO_BASENAME);
}
Expand All @@ -430,7 +434,7 @@ public static function filename(?string $path): string
* @param string|null $path
* @return string
*/
public static function dirName($path): string
public static function dirName(?string $path): string
{
return (string)pathinfo((string)$path, PATHINFO_DIRNAME);
}
Expand Down Expand Up @@ -495,7 +499,7 @@ public static function stripExt(string $path): string
* @param string $path
* @return bool
*/
public static function isDir($path): bool
public static function isDir(string $path): bool
{
if (!$path) {
return false;
Expand All @@ -511,7 +515,7 @@ public static function isDir($path): bool
* @param string $path
* @return bool
*/
public static function isFile($path): bool
public static function isFile(string $path): bool
{
$path = self::clean($path);
return file_exists($path) && is_file($path);
Expand Down Expand Up @@ -550,7 +554,7 @@ public static function getRelative(
* @param string|null $path
* @return bool
*/
public static function isReal($path): bool
public static function isReal(?string $path): bool
{
if (!$path) {
return false;
Expand Down
12 changes: 6 additions & 6 deletions src/Filter.php
Expand Up @@ -181,7 +181,7 @@ public static function int($value): int
* @param string|null $value
* @return string
*/
public static function digits($value): string
public static function digits(?string $value): string
{
// we need to remove - and + because they're allowed in the filter
$cleaned = str_replace(['-', '+'], '', (string)$value);
Expand All @@ -196,7 +196,7 @@ public static function digits($value): string
* @param string|null $value
* @return string
*/
public static function alpha($value): string
public static function alpha(?string $value): string
{
return (string)preg_replace('#[^[:alpha:]]#', '', (string)$value);
}
Expand All @@ -207,7 +207,7 @@ public static function alpha($value): string
* @param string|null $value
* @return string
*/
public static function alphanum($value): string
public static function alphanum(?string $value): string
{
return (string)preg_replace('#[^[:alnum:]]#', '', (string)$value);
}
Expand All @@ -229,7 +229,7 @@ public static function base64(string $value): string
* @param string $value
* @return string
*/
public static function path($value): string
public static function path(string $value): string
{
$pattern = '#^[A-Za-z0-9_\/-]+[A-Za-z0-9_\.-]*([\\\\\/][A-Za-z0-9_-]+[A-Za-z0-9_\.-]*)*$#';
preg_match($pattern, $value, $matches);
Expand Down Expand Up @@ -428,7 +428,7 @@ public static function raw($string)
* @param string $input
* @return string
*/
public static function ucFirst($input): string
public static function ucFirst(string $input): string
{
$string = Str::low($input);
$string = ucfirst($string);
Expand Down Expand Up @@ -462,7 +462,7 @@ public static function className(string $input): string
$output = (string)preg_replace(['#(?<=[^A-Z\s])([A-Z\s])#i'], ' $0', $input);
$output = explode(' ', $output);

$output = array_map(function ($item) {
$output = array_map(static function ($item) {
$item = (string)preg_replace('#[^a-z0-9]#i', '', $item);
$item = Filter::ucFirst($item);
return $item;
Expand Down
1 change: 1 addition & 0 deletions src/Http.php
Expand Up @@ -63,6 +63,7 @@ public static function download(string $filename): bool
fpassthru($handle);
fclose($handle);
} else {
/** @phan-suppress-next-line PhanPluginRemoveDebugEcho */
echo file_get_contents($filename);
}

Expand Down
9 changes: 6 additions & 3 deletions src/Ser.php
Expand Up @@ -132,12 +132,15 @@ public static function maybeUn(string $data)
* @param string $brokenSerializedData
* @return string
*/
public static function fix($brokenSerializedData): string
public static function fix(string $brokenSerializedData): string
{
$fixedSerializedData = preg_replace_callback(
'!s:(\d+):"(.*?)";!',
/** @psalm-suppress MissingClosureParamType */
function ($matches) {
/**
* @param array $matches
* @return string
*/
static function (array $matches): string {
$snip = $matches[2];
return 's:' . strlen($snip) . ':"' . $snip . '";';
},
Expand Down
2 changes: 1 addition & 1 deletion src/Slug.php
Expand Up @@ -728,7 +728,7 @@ private static function initLanguageMap(string $language = ''): void
* @param bool $cssMode Whether or not to generate strings safe for CSS classes/IDs (Default to false)
* @return string
*/
public static function filter($string, string $separator = '-', bool $cssMode = false): string
public static function filter(?string $string, string $separator = '-', bool $cssMode = false): string
{
$slug = (string)preg_replace('/([^a-z0-9]+)/', $separator, strtolower(self::removeAccents((string)$string)));
$slug = trim($slug, $separator);
Expand Down
28 changes: 16 additions & 12 deletions src/Stats.php
Expand Up @@ -45,7 +45,7 @@ public static function stdDev(array $values, bool $sample = false): float
*
* @return float
*/
public static function variance(array $values, bool $sample = false)
public static function variance(array $values, bool $sample = false): float
{
$average = self::mean($values);
$sum = 0;
Expand All @@ -65,10 +65,10 @@ public static function variance(array $values, bool $sample = false)
/**
* Return the mean (average) value of the given values.
*
* @param array $values
* @param array|null $values
* @return float
*/
public static function mean($values): float
public static function mean(?array $values): float
{
if (empty($values)) {
return 0;
Expand Down Expand Up @@ -110,7 +110,7 @@ public static function linSpace(float $min, float $max, int $num = 50, bool $end
$space[] = $value;
}

if ($endpoint === false) {
if (!$endpoint) {
array_pop($space);
}

Expand All @@ -126,17 +126,21 @@ public static function linSpace(float $min, float $max, int $num = 50, bool $end
* For a better implementation copy:
* http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.histogram.html
*
* @param array $values
* @param int $steps
* @param float $lowerBound
* @param float $upperBound
* @param array $values
* @param int $steps
* @param float|null $lowerBound
* @param float|null $upperBound
*
* @return array
*/
public static function histogram(array $values, int $steps = 10, $lowerBound = null, $upperBound = null): array
{
$min = $lowerBound ?: min($values);
$max = $upperBound ?: max($values);
public static function histogram(
array $values,
int $steps = 10,
?float $lowerBound = null,
?float $upperBound = null
): array {
$min = $lowerBound ?? min($values);
$max = $upperBound ?? max($values);

$range = $max - $min;

Expand Down

0 comments on commit 923fa57

Please sign in to comment.