Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiaanluca committed Jun 22, 2021
1 parent ce4da19 commit 39fd6db
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 102 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

- Added support for PHP 8

### Changed

- Internal PHP 8 code updates

### Removed

- Removed support for PHP 7
Expand Down
48 changes: 7 additions & 41 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,23 @@

class Item
{
/**
* The current value being handled.
*
* @var mixed
*/
protected $value;
protected mixed $value;

/**
* @param mixed $value The value you want to process.
*/
public function __construct($value)
public function __construct(mixed $value)
{
$this->value = $value;

if (! defined('PIPED_VALUE')) {
define('PIPED_VALUE', 'PIPED_VALUE-' . uniqid());
define('PIPED_VALUE', 'PIPED_VALUE-'.uniqid('', true));
}
}

/**
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public function __call($name, $arguments)
public function __call(string $name, array $arguments): mixed
{
return $this->pipe($name, ...$arguments);
}

/**
* Perform an operation on the current value.
*
* @param callable|string|object $callback
* @param array ...$arguments
*
* @return \SebastiaanLuca\PipeOperator\Item|\SebastiaanLuca\PipeOperator\PipeProxy
*/
public function pipe($callback, ...$arguments)
public function pipe(callable|object|string $callback, mixed ...$arguments): Item|PipeProxy
{
if (! is_callable($callback)) {
return new PipeProxy($this, $callback);
Expand All @@ -55,14 +33,7 @@ public function pipe($callback, ...$arguments)
return $this;
}

/**
* Add the given value to the list of arguments.
*
* @param array $arguments
*
* @return array
*/
public function addValueToArguments(array $arguments) : array
public function addValueToArguments(array $arguments): array
{
// If the caller hasn't explicitly specified where they want the value
// to be added, we will add it as the first value. Otherwise we will
Expand All @@ -77,12 +48,7 @@ public function addValueToArguments(array $arguments) : array
}, $arguments);
}

/**
* Get the current value.
*
* @return mixed
*/
public function get()
public function get(): mixed
{
return $this->value;
}
Expand Down
25 changes: 4 additions & 21 deletions src/PipeProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,16 @@

class PipeProxy
{
/**
* @var \SebastiaanLuca\PipeOperator\Item
*/
protected $item;
protected Item $item;
protected object $object;

/**
* @var object
*/
protected $object;

/**
* @param \SebastiaanLuca\PipeOperator\Item $item
* @param object $object
*/
public function __construct(Item $item, $object)
public function __construct(Item $item, object $object)
{
$this->item = $item;
$this->object = $object;
}

/**
* @param string $method
* @param array $arguments
*
* @return \SebastiaanLuca\PipeOperator\Item
*/
public function __call($method, array $arguments)
public function __call(string $method, array $arguments): Item
{
$callback = Closure::bind(function (...$arguments) use ($method) {
return $this->{$method}(...$arguments);
Expand Down
6 changes: 1 addition & 5 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
if (! function_exists('take')) {
/**
* Create a new piped item from a given value.
*
* @param mixed $value The value you want to process.
*
* @return \SebastiaanLuca\PipeOperator\Item
*/
function take($value) : Item
function take(mixed $value): Item
{
return new Item($value);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/IdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IdentifierTest extends TestCase
/**
* @test
*/
public function it uses the identifier to replace the value() : void
public function it uses the identifier to replace the value(): void
{
$this->assertSame(
'key',
Expand All @@ -22,7 +22,7 @@ public function it uses the identifier to replace the value() : void
/**
* @test
*/
public function it uses the identifier to replace the value using the method directly() : void
public function it uses the identifier to replace the value using the method directly(): void
{
$this->assertSame(
'key',
Expand Down
54 changes: 26 additions & 28 deletions tests/Unit/MethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MethodsTest extends TestCase
/**
* @test
*/
public function it can transform a value using a callable string method() : void
public function it can transform a value using a callable string method(): void
{
$this->assertSame(
'STRING',
Expand All @@ -23,7 +23,7 @@ public function it can transform a value using a callable string method
/**
* @test
*/
public function it can transform a value using a callable string method using the method directly() : void
public function it can transform a value using a callable string method using the method directly(): void
{
$this->assertSame(
'STRING',
Expand All @@ -36,13 +36,13 @@ public function it can transform a value using a callable string method
/**
* @test
*/
public function it can transform a value using a closure() : void
public function it can transform a value using a closure(): void
{
$this->assertSame(
'prefixed-string',
take('string')
->pipe(function (string $value) {
return 'prefixed-' . $value;
return 'prefixed-'.$value;
})
->get()
);
Expand All @@ -51,7 +51,20 @@ public function it can transform a value using a closure() : void
/**
* @test
*/
public function it can transform a value using a public class method() : void
public function it can transform a value using a short closure(): void
{
$this->assertSame(
'prefixed-string',
take('string')
->pipe(fn (string $value): string => 'prefixed-'.$value)
->get()
);
}

/**
* @test
*/
public function it can transform a value using a public class method(): void
{
$this->assertSame(
'UPPERCASE',
Expand All @@ -64,7 +77,7 @@ public function it can transform a value using a public class method()
/**
* @test
*/
public function it can transform a value using a proxied public class method() : void
public function it can transform a value using a proxied public class method(): void
{
$this->assertSame(
'UPPERCASE',
Expand All @@ -77,7 +90,7 @@ public function it can transform a value using a proxied public class 
/**
* @test
*/
public function it can transform a value using a private class method() : void
public function it can transform a value using a private class method(): void
{
$this->assertSame(
'lowercase',
Expand All @@ -90,7 +103,7 @@ public function it can transform a value using a private class method()
/**
* @test
*/
public function it can transform a value using a proxied private class method() : void
public function it can transform a value using a proxied private class method(): void
{
$this->assertSame(
'start-add-this',
Expand All @@ -104,7 +117,7 @@ public function it can transform a value using a proxied private class
/**
* @test
*/
public function it can transform a value while accepting pipe parameters() : void
public function it can transform a value while accepting pipe parameters(): void
{
$this->assertSame(
['KEY' => 'value'],
Expand All @@ -117,7 +130,7 @@ public function it can transform a value while accepting pipe parameters
/**
* @test
*/
public function it can transform a value while accepting pipe parameters using the method directly() : void
public function it can transform a value while accepting pipe parameters using the method directly(): void
{
$this->assertSame(
['KEY' => 'value'],
Expand All @@ -127,32 +140,17 @@ public function it can transform a value while accepting pipe parameters
);
}

/**
* @param string $value
*
* @return string
*/
public function uppercase(string $value) : string
public function uppercase(string $value): string
{
return mb_strtoupper($value);
}

/**
* @param string $value
*
* @return string
*/
private function lowercase(string $value) : string
private function lowercase(string $value): string
{
return mb_strtolower($value);
}

/**
* @param array ...$values
*
* @return string
*/
private function join(...$values) : string
private function join(string ...$values): string
{
return implode('-', $values);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ObjectTest extends TestCase
/**
* @test
*/
public function it returns an item object when get has not been called yet() : void
public function it returns an item object when get has not been called yet(): void
{
$this->assertInstanceOf(
Item::class,
Expand All @@ -21,7 +21,7 @@ public function it returns an item object when get has not been called
/**
* @test
*/
public function it returns an item object when get has not been called yet using the method directly() : void
public function it returns an item object when get has not been called yet using the method directly(): void
{
$this->assertInstanceOf(
Item::class,
Expand All @@ -32,7 +32,7 @@ public function it returns an item object when get has not been called
/**
* @test
*/
public function it can transform a complex value in multiple steps() : void
public function it can transform a complex value in multiple steps(): void
{
$this->assertSame(
'blog',
Expand All @@ -48,7 +48,7 @@ public function it can transform a complex value in multiple steps() : v
/**
* @test
*/
public function it can transform a complex value in multiple steps using the method directly() : void
public function it can transform a complex value in multiple steps using the method directly(): void
{
$this->assertSame(
'blog',
Expand All @@ -63,7 +63,7 @@ public function it can transform a complex value in multiple steps usin
/**
* @test
*/
public function it can transform a complex value in multiple steps using different method calls() : void
public function it can transform a complex value in multiple steps using different method calls(): void
{
$this->assertSame(
'blog',
Expand Down

0 comments on commit 39fd6db

Please sign in to comment.