Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring variable types up to speed with PHP 7.4 #118

Merged
merged 9 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"timeout": 2,
"timeout": 5,
"source": {
"directories": [
"src"
]
},
"mutators": {
"global-ignoreSourceCodeByRegex": [
"assert.*"
],
"@default": true,
"IdenticalEqual": false,
"NotIdenticalNotEqual": false
Expand Down
78 changes: 37 additions & 41 deletions src/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function __construct(?iterable $input = null)
$this->pipeline = $input;
}

private function empty(): bool
{
return [] === $this->pipeline || null === $this->pipeline;
}

/**
* Appends the contents of an interable to the end of the pipeline.
*/
Expand Down Expand Up @@ -143,7 +148,7 @@ private function willReplace(?iterable $values = null): bool
}

// No shortcuts are applicable if the pipeline was initialized.
if ([] !== $this->pipeline && null !== $this->pipeline) {
if (!$this->empty()) {
return false;
}

Expand Down Expand Up @@ -230,15 +235,13 @@ public function unpack(?callable $func = null): self
*/
public function chunk(int $length, bool $preserve_keys = false): self
{
if (null === $this->pipeline) {
// No-op: null.
// No-op: an empty array or null.
if ($this->empty()) {
return $this;
}

if ([] === $this->pipeline) {
// No-op: an empty array.
return $this;
}
// Static analyzer hint
assert(null !== $this->pipeline && [] !== $this->pipeline);

// Array shortcut
if (is_array($this->pipeline)) {
Expand Down Expand Up @@ -385,13 +388,8 @@ private static function applyOnce(iterable $previous, callable $func): iterable
*/
public function filter(?callable $func = null): self
{
if (null === $this->pipeline) {
// No-op: null.
return $this;
}

if ([] === $this->pipeline) {
// No-op: an empty array.
// No-op: an empty array or null.
if ($this->empty()) {
return $this;
}

Expand Down Expand Up @@ -504,13 +502,8 @@ public function getIterator(): Traversable
*/
public function toArray(bool $preserve_keys = false): array
{
if (null === $this->pipeline) {
// With non-primed pipeline just return an empty array.
return [];
}

if ([] === $this->pipeline) {
// Empty array is empty.
// No-op: an empty array or null.
if ($this->empty()) {
return [];
}

Expand All @@ -537,15 +530,13 @@ public function toArray(bool $preserve_keys = false): array
*/
public function count(): int
{
if (null === $this->pipeline) {
if ($this->empty()) {
// With non-primed pipeline just return zero.
return 0;
}

if ([] === $this->pipeline) {
// Empty array is empty.
return 0;
}
// Static analyzer hint
assert(null !== $this->pipeline && [] !== $this->pipeline);

$result = 0;

Expand Down Expand Up @@ -582,11 +573,14 @@ private static function generatorFromIterable(iterable $input): Generator
*/
public function slice(int $offset, ?int $length = null)
{
if (null === $this->pipeline) {
if ($this->empty()) {
// With non-primed pipeline just move along.
return $this;
}

// Static analyzer hint
assert(null !== $this->pipeline && [] !== $this->pipeline);

if (0 === $length) {
// We're not consuming anything assuming total laziness.
$this->pipeline = null;
Expand Down Expand Up @@ -773,11 +767,17 @@ private static function toIterators(iterable ...$inputs): array
*/
public function reservoir(int $size, ?callable $weightFunc = null): array
{
if (null === $this->pipeline) {
if ($this->empty()) {
return [];
}

// Static analyzer hint
assert(null !== $this->pipeline && [] !== $this->pipeline);

if ($size <= 0) {
// Discard the state to emulate a full consumption
sanmai marked this conversation as resolved.
Show resolved Hide resolved
$this->pipeline = null;

return [];
}

Expand Down Expand Up @@ -886,13 +886,12 @@ private static function random(): float
*/
public function min()
{
if (null === $this->pipeline) {
if ($this->empty()) {
return null;
}

if ([] === $this->pipeline) {
return null;
}
// Static analyzer hint
assert(null !== $this->pipeline && [] !== $this->pipeline);

if (is_array($this->pipeline)) {
/** @psalm-suppress ArgumentTypeCoercion */
Expand Down Expand Up @@ -928,13 +927,12 @@ public function min()
*/
public function max()
{
if (null === $this->pipeline) {
if ($this->empty()) {
return null;
}

if ([] === $this->pipeline) {
return null;
}
// Static analyzer hint
assert(null !== $this->pipeline && [] !== $this->pipeline);

if (is_array($this->pipeline)) {
/** @psalm-suppress ArgumentTypeCoercion */
Expand All @@ -960,15 +958,13 @@ public function max()
*/
public function flip()
{
if (null === $this->pipeline) {
if ($this->empty()) {
// No-op: null.
return $this;
}

if ([] === $this->pipeline) {
// No-op: an empty array.
return $this;
}
// Static analyzer hint
assert(null !== $this->pipeline && [] !== $this->pipeline);

if (is_array($this->pipeline)) {
$this->pipeline = array_flip($this->pipeline);
Expand Down