Skip to content

Commit

Permalink
Make callback to be mandatory in the Conditionable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Aug 11, 2022
1 parent 48d96ab commit 25bc57b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Trying to invoke \\(callable\\(\\)\\: mixed\\)\\|null but it might not be a callable\\.$#"
count: 2
path: src/Builders/AbstractParameterizedQueryBuilder.php

-
message: "#^Method Elastic\\\\ScoutDriverPlus\\\\Builders\\\\SearchParametersBuilder\\:\\:resolveJoinedIndexName\\(\\) should return string but returns mixed\\.$#"
count: 1
path: src/Builders/SearchParametersBuilder.php

-
message: "#^Trying to invoke \\(callable\\(\\)\\: mixed\\)\\|null but it might not be a callable\\.$#"
count: 2
path: src/Builders/SearchParametersBuilder.php

-
message: "#^Parameter \\#1 \\$items of class Illuminate\\\\Database\\\\Eloquent\\\\Collection constructor expects Illuminate\\\\Contracts\\\\Support\\\\Arrayable\\<int, TModel of Illuminate\\\\Database\\\\Eloquent\\\\Model\\>\\|iterable\\<int, TModel of Illuminate\\\\Database\\\\Eloquent\\\\Model\\>\\|null, Illuminate\\\\Support\\\\Collection\\<int, Illuminate\\\\Database\\\\Eloquent\\\\Model\\|null\\> given\\.$#"
count: 1
Expand Down
27 changes: 10 additions & 17 deletions src/Support/Conditionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@
namespace Elastic\ScoutDriverPlus\Support;

use Closure;
use Illuminate\Support\HigherOrderWhenProxy;

/**
* This trait duplicates \Illuminate\Support\Traits\Conditionable,
* This trait is similar to \Illuminate\Support\Traits\Conditionable,
* which is available in Laravel since version 9.
*/
trait Conditionable
{
/**
* @param mixed $value
*
* @return $this|HigherOrderWhenProxy
*/
public function when($value, callable $callback = null, callable $default = null)
public function when($value, callable $callback, callable $default = null): self
{
$value = $value instanceof Closure ? $value($this) : $value;

if (func_num_args() === 1) {
return new HigherOrderWhenProxy($this, $value);
}

if ($value) {
return $callback($this, $value) ?? $this;
} elseif ($default) {
}

if ($default) {
return $default($this, $value) ?? $this;
}

Expand All @@ -36,19 +31,17 @@ public function when($value, callable $callback = null, callable $default = null
/**
* @param mixed $value
*
* @return $this|HigherOrderWhenProxy
* @return $this
*/
public function unless($value, callable $callback = null, callable $default = null)
public function unless($value, callable $callback, callable $default = null): self
{
$value = $value instanceof Closure ? $value($this) : $value;

if (func_num_args() === 1) {
return new HigherOrderWhenProxy($this, ! $value);
if (!$value) {
return $callback($this, $value) ?? $this;
}

if (! $value) {
return $callback($this, $value) ?? $this;
} elseif ($default) {
if ($default) {
return $default($this, $value) ?? $this;
}

Expand Down

0 comments on commit 25bc57b

Please sign in to comment.