Skip to content

Commit 1ca1f08

Browse files
Rename Item to Pipe
1 parent 17fdf02 commit 1ca1f08

File tree

6 files changed

+55
-55
lines changed

6 files changed

+55
-55
lines changed

README.md

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
- [Requirements](#requirements)
1919
- [How to install](#how-to-install)
20-
- [What does it solve?](#what-does-it-solve)
21-
- [A simple example](#a-simple-example)
22-
- [Another way of writing](#another-way-of-writing)
23-
- [More examples of the issue at hand](#more-examples-of-the-issue-at-hand)
2420
- [How to use](#how-to-use)
2521
- [The basics](#the-basics)
2622
- [Using closures](#using-closures)
2723
- [Using class methods](#using-class-methods)
24+
- [What does it solve?](#what-does-it-solve)
25+
- [A simple example](#a-simple-example)
26+
- [Another way of writing](#another-way-of-writing)
27+
- [More examples of the issue at hand](#more-examples-of-the-issue-at-hand)
2828
- [Notes](#notes)
2929
- [License](#license)
3030
- [Change log](#change-log)
@@ -46,45 +46,6 @@ Via Composer:
4646
composer require sebastiaanluca/php-pipe-operator
4747
```
4848

49-
## What does it solve?
50-
51-
This package is based on the [pipe operator RFC by Sara Golemon (2016)](https://wiki.php.net/rfc/pipe-operator), who explains the problem as:
52-
53-
>A common PHP OOP pattern is the use of method chaining, or what is also known as “Fluent Expressions”. [] This works well enough for OOP classes which were designed for fluent calling, however it is impossible, or at least unnecessarily arduous, to adapt non-fluent classes to this usage style, harder still for functional interfaces.
54-
55-
Coming across the proposal, I also [blogged about it](https://blog.sebastiaanluca.com/enabling-php-method-chaining-with-a-makeshift-pipe-operator).
56-
57-
### A simple example
58-
59-
Say you want to get the subdomain from a URL, you end up with something like this:
60-
61-
```php
62-
$subdomain = 'https://blog.sebastiaanluca.com/';
63-
$subdomain = parse_url($subdomain, PHP_URL_HOST);
64-
$subdomain = explode('.', $subdomain);
65-
$subdomain = reset($subdomain);
66-
67-
// "blog"
68-
```
69-
70-
This works, of course, but it's quite verbose and repetitive.
71-
72-
### Another way of writing
73-
74-
Same result, different style:
75-
76-
```php
77-
$subdomain = explode('.', parse_url('https://blog.sebastiaanluca.com/', PHP_URL_HOST))[0];
78-
79-
// "blog"
80-
```
81-
82-
This might be the worst of all solutions, as it requires you to start reading from the center, work your way towards the outer methods, and keep switching back and forth. The more methods and variants, the more difficult to get a sense of what's going on.
83-
84-
### More examples of the issue at hand
85-
86-
See [Sara's RFC](https://wiki.php.net/rfc/pipe-operator#introduction) for more complex and real-world examples.
87-
8849
## How to use
8950

9051
### The basics
@@ -230,6 +191,45 @@ class MyClass
230191
}
231192
```
232193

194+
## What does it solve?
195+
196+
This package is based on the [pipe operator RFC by Sara Golemon (2016)](https://wiki.php.net/rfc/pipe-operator), who explains the problem as:
197+
198+
>A common PHP OOP pattern is the use of method chaining, or what is also known as “Fluent Expressions”. [] This works well enough for OOP classes which were designed for fluent calling, however it is impossible, or at least unnecessarily arduous, to adapt non-fluent classes to this usage style, harder still for functional interfaces.
199+
200+
Coming across the proposal, I also [blogged about it](https://sebastiaanluca.com/blog/enabling-php-method-chaining-with-a-makeshift-pipe-operator).
201+
202+
### A simple example
203+
204+
Say you want to get the subdomain from a URL, you end up with something like this:
205+
206+
```php
207+
$subdomain = 'https://blog.sebastiaanluca.com/';
208+
$subdomain = parse_url($subdomain, PHP_URL_HOST);
209+
$subdomain = explode('.', $subdomain);
210+
$subdomain = reset($subdomain);
211+
212+
// "blog"
213+
```
214+
215+
This works, of course, but it's quite verbose and repetitive.
216+
217+
### Another way of writing
218+
219+
Same result, different style:
220+
221+
```php
222+
$subdomain = explode('.', parse_url('https://blog.sebastiaanluca.com/', PHP_URL_HOST))[0];
223+
224+
// "blog"
225+
```
226+
227+
This might be the worst of all solutions, as it requires you to start reading from the center, work your way towards the outer methods, and keep switching back and forth. The more methods and variants, the more difficult to get a sense of what's going on.
228+
229+
### More examples of the issue at hand
230+
231+
See [Sara's RFC](https://wiki.php.net/rfc/pipe-operator#introduction) for more complex and real-world examples.
232+
233233
## Notes
234234

235235
While this packages makes a good attempt at bringing the pipe operator to PHP, it unfortunately does not offer autocompletion on chained methods. For that to work we need the real deal, so make some noise and get the people in charge to vote *for* Sara's RFC!

src/Item.php renamed to src/Pipe.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace SebastiaanLuca\PipeOperator;
44

5-
class Item
5+
class Pipe
66
{
77
protected mixed $value;
88

src/PipeProxy.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
class PipeProxy
88
{
9-
protected Item $item;
9+
protected Pipe $item;
1010
protected object $object;
1111

12-
public function __construct(Item $item, object $object)
12+
public function __construct(Pipe $item, object $object)
1313
{
1414
$this->item = $item;
1515
$this->object = $object;
1616
}
1717

18-
public function __call(string $method, array $arguments): Item
18+
public function __call(string $method, array $arguments): Pipe
1919
{
2020
$callback = Closure::bind(function (...$arguments) use ($method) {
2121
return $this->{$method}(...$arguments);

src/functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

3-
use SebastiaanLuca\PipeOperator\Item;
3+
use SebastiaanLuca\PipeOperator\Pipe;
44

55
if (! function_exists('take')) {
66
/**
77
* Create a new piped item from a given value.
88
*/
9-
function take(mixed $value): Item
9+
function take(mixed $value): Pipe
1010
{
11-
return new Item($value);
11+
return new Pipe($value);
1212
}
1313
}

tests/Unit/MethodsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Closure;
66
use PHPUnit\Framework\TestCase;
7-
use SebastiaanLuca\PipeOperator\Item;
7+
use SebastiaanLuca\PipeOperator\Pipe;
88

99
class MethodsTest extends TestCase
1010
{
@@ -13,7 +13,7 @@ class MethodsTest extends TestCase
1313
*/
1414
public function it can transform using the static constructor(): void
1515
{
16-
$result = Item::from('https://blog.github.com')
16+
$result = Pipe::from('https://blog.github.com')
1717
->parse_url()
1818
->end()
1919
->explode('.', PIPED_VALUE)

tests/Unit/ObjectTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace SebastiaanLuca\PipeOperator\Tests\Unit\Classes;
44

55
use PHPUnit\Framework\TestCase;
6-
use SebastiaanLuca\PipeOperator\Item;
6+
use SebastiaanLuca\PipeOperator\Pipe;
77

88
class ObjectTest extends TestCase
99
{
@@ -13,7 +13,7 @@ class ObjectTest extends TestCase
1313
public function it returns an item object when get has not been called yet(): void
1414
{
1515
$this->assertInstanceOf(
16-
Item::class,
16+
Pipe::class,
1717
take('string')->pipe('strtoupper')
1818
);
1919
}
@@ -24,7 +24,7 @@ public function it returns an item object when get has not been called
2424
public function it returns an item object when get has not been called yet using the method directly(): void
2525
{
2626
$this->assertInstanceOf(
27-
Item::class,
27+
Pipe::class,
2828
take('string')->strtoupper()
2929
);
3030
}

0 commit comments

Comments
 (0)