From aab5cefbfd0edf17aa0dc50ee140ca575c432d30 Mon Sep 17 00:00:00 2001 From: Burak Hamza Date: Mon, 24 Jul 2023 16:57:45 +0200 Subject: [PATCH 1/3] feat: update php version and etc --- composer.json | 7 ++++--- spec/BuilderSpec.php | 9 +++++---- spec/HookableSpec.php | 11 ++++++----- spec/PipelineSpec.php | 6 +++--- src/Builder.php | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 942d7bf..7f1f347 100644 --- a/composer.json +++ b/composer.json @@ -16,11 +16,12 @@ } ], "require": { - "php": ">=7.0", - "illuminate/database": ">=5.4" + "php": "^8.2", + "illuminate/support": "^10.0", + "illuminate/database": "^10.0" }, "require-dev": { - "kahlan/kahlan": "~1.1" + "kahlan/kahlan": "5.2.3" }, "autoload": { "psr-4": { diff --git a/spec/BuilderSpec.php b/spec/BuilderSpec.php index 1b70787..7be769e 100644 --- a/spec/BuilderSpec.php +++ b/spec/BuilderSpec.php @@ -1,13 +1,14 @@ 'Illuminate\Database\Query\Builder']); - $this->eloquent = Stub::classname(['class' => 'Illuminate\Database\Eloquent\Builder']); + $query = Double::instance(['class' => 'Illuminate\Database\Query\Builder']); + $this->eloquent = Double::classname(['class' => 'Illuminate\Database\Eloquent\Builder']); $this->builder = new Builder(new $query); }); @@ -17,7 +18,7 @@ }); it('calls hook defined on the model', function () { - $model = Stub::create(); + $model = Double::instance(); expect($model)->toReceive('queryHook'); Stub::on($this->builder)->method('getModel', function () use ($model) {return $model;}); $this->builder->select(['column', 'value']); diff --git a/spec/HookableSpec.php b/spec/HookableSpec.php index 35207d1..b53a92e 100644 --- a/spec/HookableSpec.php +++ b/spec/HookableSpec.php @@ -1,15 +1,16 @@ method('getAttribute', function () { return 'value'; }); - $hookableClass = Stub::classname(['uses' => Hookable::class, 'extends' => $parent]); + $hookableClass = Double::classname(['uses' => Hookable::class, 'extends' => $parent]); $hookableClass::hook('getAttribute', function ($next, $value, $args) { $this->instanceMethod(); }); @@ -20,9 +21,9 @@ }); it('flushes all hooks with the flushHooks method', function () { - $parent = Stub::classname(); + $parent = Double::classname(); - $hookableClass = Stub::classname(['uses' => Hookable::class, 'extends' => $parent]); + $hookableClass = Double::classname(['uses' => Hookable::class, 'extends' => $parent]); $hookableClass::hook('method1', function ($next, $value, $args) {}); $hookableClass::hook('method2', function ($next, $value, $args) {}); diff --git a/spec/PipelineSpec.php b/spec/PipelineSpec.php index f274af6..ab7e877 100644 --- a/spec/PipelineSpec.php +++ b/spec/PipelineSpec.php @@ -1,6 +1,7 @@ ['Sofa\Hookable\Contracts\ArgumentBag']]); + $args = Double::instance(['implements' => ['Sofa\Hookable\Contracts\ArgumentBag']]); Stub::on($args)->method('get')->andReturn('bar', 'bar'); expect($args)->toReceive('get'); - expect($args)->toReceiveNext('get'); $result = $pipeline->send($payload)->with($args)->to($destination); expect($result)->toBe('start,pipe-bar,end-bar'); }); diff --git a/src/Builder.php b/src/Builder.php index a15bbd2..77c2ea6 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -50,7 +50,7 @@ class Builder extends EloquentBuilder */ public function callParent($method, array $args) { - return call_user_func_array("parent::{$method}", $args); + return parent::$method(...array_values($args)); } /** From b87f826e67bc44dcfea3d7402834822ce18ebc8d Mon Sep 17 00:00:00 2001 From: Burak Hamza Date: Tue, 25 Jul 2023 16:40:18 +0200 Subject: [PATCH 2/3] fix: hot fix --- src/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Builder.php b/src/Builder.php index 77c2ea6..cf55388 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -124,7 +124,7 @@ public function select($columns = ['*']) */ public function where($column, $operator = null, $value = null, $boolean = 'and') { - if (!in_array(strtolower($operator), $this->operators, true)) { + if (is_string($operator) && !in_array(strtolower($operator), $this->operators, true)) { list($value, $operator) = [$operator, '=']; } From cf6924f6239e552f09177f72f06df482fd4db36b Mon Sep 17 00:00:00 2001 From: PooyaRaki Date: Mon, 31 Jul 2023 15:54:23 +0200 Subject: [PATCH 3/3] feat: added a new unset method --- src/ArgumentBag.php | 29 ++++++++++++++++++----------- src/Contracts/ArgumentBag.php | 32 ++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/ArgumentBag.php b/src/ArgumentBag.php index c6dbd73..c1bd5b6 100644 --- a/src/ArgumentBag.php +++ b/src/ArgumentBag.php @@ -2,7 +2,7 @@ namespace Sofa\Hookable; -class ArgumentBag implements \Sofa\Hookable\Contracts\ArgumentBag +class ArgumentBag implements Contracts\ArgumentBag { /** * Arguments being passed. @@ -24,7 +24,7 @@ public function __construct(array $items) /** * @inheritdoc */ - public function all() + public function all(): array { return $this->items; } @@ -32,7 +32,7 @@ public function all() /** * @inheritdoc */ - public function first() + public function first(): mixed { $items = $this->items; @@ -42,7 +42,7 @@ public function first() /** * @inheritdoc */ - public function last() + public function last(): mixed { $items = array_reverse($this->items); @@ -52,26 +52,33 @@ public function last() /** * @inheritdoc */ - public function get($key, $default = null) + public function get(string $key, mixed $default = null): mixed { return array_key_exists($key, $this->items) ? $this->items[$key] : $default; } /** - * Set value at given key. - * - * @param string $key - * @param mixed $value + * @inheritDoc */ - public function set($key, $value) + public function set(string $key, mixed $value): void { $this->items[$key] = $value; } + /** + * @inheritDoc + */ + public function unset(string $key): void + { + if (isset($this->items[$key])) { + unset($this->items[$key]); + } + } + /** * @inheritdoc */ - public function isEmpty() + public function isEmpty(): bool { return ! count($this->items); } diff --git a/src/Contracts/ArgumentBag.php b/src/Contracts/ArgumentBag.php index 29a5903..dec7560 100644 --- a/src/Contracts/ArgumentBag.php +++ b/src/Contracts/ArgumentBag.php @@ -9,35 +9,55 @@ interface ArgumentBag * * @return array */ - public function all(); + public function all(): array; /** * Fetch first argument from the bag. * * @return mixed */ - public function first(); + public function first(): mixed; /** * Fetch last argument from the bag. * * @return mixed */ - public function last(); + public function last(): mixed; /** * Get argument with given key. * - * @param string|int $key + * @param string $key * @param mixed $default + * * @return mixed */ - public function get($key, $default = null); + public function get(string $key, mixed $default = null): mixed; + + /** + * Set value at given key. + * + * @param string $key + * @param mixed $value + * + * @return void + */ + public function set(string $key, mixed $value): void; + + /** + * Unset value at given key. + * + * @param string $key + * + * @return void + */ + public function unset(string $key): void; /** * Determine whether the bag is empty. * * @return boolean */ - public function isEmpty(); + public function isEmpty(): bool; }