Skip to content

Commit

Permalink
cleanup (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz authored Oct 7, 2020
1 parent 05c4ce6 commit f60c106
Show file tree
Hide file tree
Showing 55 changed files with 228 additions and 140 deletions.
14 changes: 8 additions & 6 deletions src/Psl/Arr/contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
namespace Psl\Arr;

/**
* Returns true if the given iterable contains the value. Strict equality is
* Returns true if the given array contains the value. Strict equality is
* used.
*
* @psalm-template Tk
* @psalm-template Tk of array-key
* @psalm-template Tv
*
* @psalm-param iterable<Tk, Tv> $iterable
* @psalm-param Tk $value
* @psalm-param array<Tk, Tv> $array
* @psalm-param Tv $value
*
* @psalm-pure
*/
function contains(iterable $iterable, $value): bool
function contains(array $array, $value): bool
{
foreach ($iterable as $v) {
foreach ($array as $v) {
if ($value === $v) {
return true;
}
Expand Down
29 changes: 14 additions & 15 deletions src/Psl/Fun/after.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@
*
* Example:
*
* $runExpression = Fun\after(
* fn($i) => $i + 1,
* fn(int $i) => $i * 5
* $run = Fun\after(
* static fn(int $i): int => $i + 1,
* static fn(int $i): int => $i * 5,
* );
* => $runExpression(1) === 10;
* => $runExpression(2) === 15;
* => $runExpression(3) === 20;
*
* $greet = Fun\after(
* fn($value) => 'Hello' . $value,
* fn($value) => $value . '!'
* );
* => $greet('World') === 'Hello World!';
* => $greet('Jim') === 'Hello Jim!';
* $run(1)
* => Int(10)
*
* $run(2)
* => Int(15)
*
* $run(3)
* => Int(20)
*
* @template I
* @template O
* @template R
*
* @psalm-param callable(I): O $first
* @psalm-param callable(O): R $next
* @psalm-param (callable(I): O) $first
* @psalm-param (callable(O): R) $next
*
* @psalm-return callable(I): R
* @psalm-return (callable(I): R)
*
* @psalm-pure
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Psl/Fun/passthrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* It can e.g. be used as a success callback.
*
* @template T
*
* @psalm-return callable(T): T
*
* @psalm-pure
*/
function passthrough(): callable
Expand Down
26 changes: 16 additions & 10 deletions src/Psl/Fun/pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@

/**
* Performs left-to-right function composition.
* Example
* $pipe = Fun\pipe(
* fn($value) => 'Hello' . $value,
* fn($value) => $value . '!',
* fn($value) => '¡' . $value
*
* Example:
*
* $greet = Fun\pipe(
* static fn(string $value): string => 'Hello' . $value,
* static fn(string $value): string => $value . '!',
* static fn(string $value): string => '¡' . $value
* );
* => $greet('World') === '¡Hello World!';
* => $greet('Jim') === '¡Hello Jim!';
*
* $greet('World')
* => Str('¡Hello World!');
*
* $greet('Jim')
* => Str('¡Hello Jim!');
*
* @template T
*
Expand All @@ -32,10 +38,10 @@ function pipe(callable ...$stages): callable
/**
* @template IO
*
* @param IO $input
* @param callable(IO): IO $next
* @psalm-param IO $input
* @psalm-param (callable(IO): IO) $next
*
* @return IO
* @psalm-return IO
*/
static fn ($input, int $key, callable $next) => $next($input),
$input
Expand Down
3 changes: 2 additions & 1 deletion src/Psl/Fun/rethrow.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* This method creates a callback that throws the exception passed as argument.
* It can e.g. be used as a failure callback.
*
* @psalm-return callable(Exception): no-return
* @psalm-return (callable(Exception): no-return)
*
* @psalm-pure
*/
function rethrow(): callable
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Math/div.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
*
* @psalm-pure
*
* @throws Exception\ArithmeticException If the numerator is Math\INT64_MIN and the denominator is -1.
* @throws Exception\DivisionByZeroException If the denominator is 0.
* @throws Exception\ArithmeticException If the $numerator is Math\INT64_MIN and the $denominator is -1.
* @throws Exception\DivisionByZeroException If the $denominator is 0.
*/
function div(int $numerator, int $denominator): int
{
Expand Down
4 changes: 3 additions & 1 deletion src/Psl/Str/Byte/capitalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Psl\Str\Byte;

use function ucfirst;

/**
* Returns the string with the first character capitalized.
*
Expand All @@ -14,5 +16,5 @@
*/
function capitalize(string $string): string
{
return \ucfirst($string);
return ucfirst($string);
}
4 changes: 3 additions & 1 deletion src/Psl/Str/Byte/capitalize_words.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Psl\Str\Byte;

use function ucwords;

/**
* Returns the string with all words capitalized.
*
Expand All @@ -14,5 +16,5 @@
*/
function capitalize_words(string $string, string $delimiters = " \t\r\n\f\v"): string
{
return \ucwords($string, $delimiters);
return ucwords($string, $delimiters);
}
4 changes: 3 additions & 1 deletion src/Psl/Str/Byte/chr.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Psl\Str\Byte;

use function chr as php_chr;

/**
* Return a specific character.
*
* @psalm-pure
*/
function chr(int $ascii): string
{
return \chr($ascii);
return php_chr($ascii);
}
18 changes: 10 additions & 8 deletions src/Psl/Str/Byte/chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@

use Psl;

use function str_split;

/**
* Returns an array containing the string split into chunks of the given size.
*
* @psalm-param int $chunk_size maximum length of the chunk
* @psalm-param int $chunk_length maximum length of the chunk
*
* @psalm-return list<string> if $chunk_size parameter is specified, the returned array will be broken down
* into chunks with each being $chunk_size in length, otherwise each chunk
* @psalm-return list<string> if $chunk_length parameter is specified, the returned array will be broken down
* into chunks with each being $chunk_length in length, otherwise each chunk
* will be one character in length.
* If the $chunk_size length exceeds the length of string, the entire string is returned
* If the $chunk_length length exceeds the length of string, the entire string is returned
* as the first (and only) array element.
* If the given string is empty, and empty array will be returned.
*
* @psalm-pure
*
* @throws Psl\Exception\InvariantViolationException If $chunk_size is negative.
* @throws Psl\Exception\InvariantViolationException If $chunk_length is negative.
*/
function chunk(string $string, int $chunk_size = 1): array
function chunk(string $string, int $chunk_length = 1): array
{
Psl\invariant($chunk_size >= 1, 'Expected a non-negative chunk size.');
Psl\invariant($chunk_length >= 1, 'Expected a non-negative chunk size.');
if ('' === $string) {
return [];
}

return \str_split($string, $chunk_size);
return str_split($string, $chunk_length);
}
7 changes: 5 additions & 2 deletions src/Psl/Str/Byte/compare.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Psl\Str\Byte;

use function strcmp;
use function strncmp;

/**
* Returns < 0 if `$string1` is less than `$string2`, > 0 if `$string1` is
* greater than `$string2`, and 0 if they are equal.
Expand All @@ -16,6 +19,6 @@
function compare(string $string, string $other, ?int $length = null): int
{
return null === $length ?
\strcmp($string, $other) :
\strncmp($string, $other, $length);
strcmp($string, $other) :
strncmp($string, $other, $length);
}
7 changes: 5 additions & 2 deletions src/Psl/Str/Byte/compare_ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Psl\Str\Byte;

use function strcasecmp;
use function strncasecmp;

/**
* Returns < 0 if `$string1` is less than `$string2`, > 0 if `$string1` is
* greater than `$string2`, and 0 if they are equal (case-insensitive).
Expand All @@ -16,6 +19,6 @@
function compare_ci(string $string, string $other, ?int $length = null): int
{
return null === $length ?
\strcasecmp($string, $other) :
\strncasecmp($string, $other, $length);
strcasecmp($string, $other) :
strncasecmp($string, $other, $length);
}
4 changes: 3 additions & 1 deletion src/Psl/Str/Byte/ends_with_ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Psl\Str\Byte;

use function substr_compare;

/**
* Returns whether the string ends with the given suffix (case-insensitive).
*
Expand All @@ -19,5 +21,5 @@ function ends_with_ci(string $string, string $suffix): bool
$suffix_length = length($suffix);

return length($string) >= $suffix_length &&
0 === \substr_compare($string, $suffix, -$suffix_length, $suffix_length, true);
0 === substr_compare($string, $suffix, -$suffix_length, $suffix_length, true);
}
6 changes: 4 additions & 2 deletions src/Psl/Str/Byte/length.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Psl\Str\Byte;

use function strlen;

/**
* Returns the length of the given string, i.e. the number of bytes.
*
* @psalm-pure
*/
function length(string $str): int
function length(string $string): int
{
return \strlen($str);
return strlen($string);
}
6 changes: 4 additions & 2 deletions src/Psl/Str/Byte/lowercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Psl\Str\Byte;

use function strtolower;

/**
* Returns the string with all alphabetic characters converted to lowercase.
*
* @psalm-pure
*/
function lowercase(string $lowercase): string
function lowercase(string $string): string
{
return \strtolower($lowercase);
return strtolower($string);
}
4 changes: 2 additions & 2 deletions src/Psl/Str/Byte/ord.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @psalm-pure
*/
function ord(string $char): int
function ord(string $character): int
{
return \ord($char);
return \ord($character);
}
6 changes: 5 additions & 1 deletion src/Psl/Str/Byte/pad_left.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use Psl;

use function str_pad;

use const STR_PAD_LEFT;

/**
* Returns the string padded to the total length by appending the `$pad_string`
* to the left.
Expand All @@ -23,5 +27,5 @@ function pad_left(string $string, int $total_length, string $pad_string = ' '):
Psl\invariant('' !== $pad_string, 'Expected a non-empty pad string.');
Psl\invariant($total_length >= 0, 'Expected a non-negative total length.');

return \str_pad($string, $total_length, $pad_string, \STR_PAD_LEFT);
return str_pad($string, $total_length, $pad_string, STR_PAD_LEFT);
}
6 changes: 5 additions & 1 deletion src/Psl/Str/Byte/pad_right.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use Psl;

use function str_pad;

use const STR_PAD_RIGHT;

/**
* Returns the string padded to the total length by appending the `$pad_string`
* to the right.
Expand All @@ -23,5 +27,5 @@ function pad_right(string $string, int $total_length, string $pad_string = ' '):
Psl\invariant('' !== $pad_string, 'Expected a non-empty pad string.');
Psl\invariant($total_length >= 0, 'Expected a non-negative total length.');

return \str_pad($string, $total_length, $pad_string, \STR_PAD_RIGHT);
return str_pad($string, $total_length, $pad_string, STR_PAD_RIGHT);
}
4 changes: 3 additions & 1 deletion src/Psl/Str/Byte/replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Psl\Str\Byte;

use function str_replace;

/**
* Returns the 'haystack' string with all occurrences of `$needle` replaced by
* `$replacement`.
Expand All @@ -12,5 +14,5 @@
*/
function replace(string $haystack, string $needle, string $replacement): string
{
return \str_replace($needle, $replacement, $haystack);
return str_replace($needle, $replacement, $haystack);
}
4 changes: 3 additions & 1 deletion src/Psl/Str/Byte/replace_ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Psl\Str\Byte;

use function str_ireplace;

/**
* Returns the 'haystack' string with all occurrences of `$needle` replaced by
* `$replacement` (case-insensitive).
Expand All @@ -12,5 +14,5 @@
*/
function replace_ci(string $haystack, string $needle, string $replacement): string
{
return \str_ireplace($needle, $replacement, $haystack);
return str_ireplace($needle, $replacement, $haystack);
}
Loading

0 comments on commit f60c106

Please sign in to comment.