Skip to content

Commit 1363321

Browse files
committed
Initial commit
0 parents  commit 1363321

File tree

9 files changed

+253
-0
lines changed

9 files changed

+253
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2021 someniatko
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# someniatko/result-type
2+
3+
Library representing a generic Result type with Success and Error states,
4+
made for aiding this functional programming pattern in PHP.
5+
6+
It is generically typed using Psalm annotations, so you are expected to use Psalm if
7+
you want typechecking of your code.
8+
9+
**Why using this library if `graham-campbell/result-type` exists?**
10+
Unfortunately, there are several downsides of that library:
11+
- while it's also typed with Psalm annotations, actually using and checking against them might be painful.
12+
- it's coupled to the `phpoption/phpoption` library which unfortunately suffers from the same problem.
13+
- there is no convenience method in Result interface which would allow getting either value from it.
14+
15+
16+
## Installation
17+
This library requires PHP 7.4 or 8.0.
18+
You can install it via Composer:
19+
20+
```shell
21+
composer install someniatko/result-type
22+
```
23+
24+
25+
## Usage
26+
27+
See [`ResultInterface`](src/ResultInterface.php) for details.
28+
29+
Example:
30+
31+
```php
32+
<?php
33+
34+
use Someniatko\ResultType\Success;
35+
use Someniatko\ResultType\Error;
36+
37+
$value = (new Success('Let it be'))
38+
->map(fn (string $s) => substr_count($s, ' ')) // Success<2>
39+
->chain(fn (int $wordsCount) => $wordsCount > 3
40+
? new Success('Long text')
41+
: new Error('short text')
42+
) // Error<'short text'>
43+
->map(fn (string $s) => str_replace(' ', '', $s)) // will not be called because we're in Error result
44+
->mapError(fn (string $s) => strtoupper($s)) // Error<'SHORT TEXT'>
45+
->get(); // 'SHORT TEXT'
46+
```

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "someniatko/result-type",
3+
"license": "MIT",
4+
"description": "Functional-ish generic Result type: either Success or Error",
5+
"require": {
6+
"php": "~7.4 || ~8.0"
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "^9.5",
10+
"psalm/phar": "^4.7"
11+
},
12+
"autoload": {
13+
"psr-4": {
14+
"Someniatko\\ResultType\\": "src/"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"Test\\": "test/"
20+
}
21+
}
22+
}

phpcs.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<ruleset
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
name="PHP_CodeSniffer"
5+
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd"
6+
>
7+
<file>src</file>
8+
<file>test</file>
9+
10+
<rule ref="PSR12">
11+
</rule>
12+
</ruleset>

psalm.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
totallyTyped="true"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns="https://getpsalm.org/schema/config"
6+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
7+
>
8+
<projectFiles>
9+
<directory name="src" />
10+
<directory name="test" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
</ignoreFiles>
14+
</projectFiles>
15+
</psalm>

src/Error.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Someniatko\ResultType;
6+
7+
/**
8+
* @template-covariant TError
9+
* @template-implements ResultInterface<never-return, TError>
10+
* @psalm-immutable
11+
*/
12+
final class Error implements ResultInterface
13+
{
14+
/** @var TError */
15+
private $value;
16+
17+
/** @param TError $value */
18+
public function __construct($value)
19+
{
20+
$this->value = $value;
21+
}
22+
23+
public function map(callable $map): ResultInterface
24+
{
25+
// no success value, so nothing to map.
26+
return $this;
27+
}
28+
29+
public function mapError(callable $map): ResultInterface
30+
{
31+
return new self($map($this->value));
32+
}
33+
34+
public function chain(callable $map): ResultInterface
35+
{
36+
// no success value, so nothing to map.
37+
return $this;
38+
}
39+
40+
public function get()
41+
{
42+
return $this->value;
43+
}
44+
}

src/ResultInterface.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Someniatko\ResultType;
6+
7+
/**
8+
* @template-covariant TSuccess
9+
* @template-covariant TError
10+
* @psalm-immutable
11+
*/
12+
interface ResultInterface
13+
{
14+
/**
15+
* Takes a callable which maps **success** value to a new value, returns new ResultInterface.
16+
* The callable will be called only if this result is Success.
17+
*
18+
* If this result is Success, returns new Success with changed value.
19+
* If this result is Error, returns it as is.
20+
*
21+
* @template TNewSuccess
22+
*
23+
* @param callable(TSuccess):TNewSuccess $map
24+
* @return self<TNewSuccess, TError>
25+
*/
26+
public function map(callable $map): self;
27+
28+
/**
29+
* Takes a callable which maps **error** value to a new value, returns new ResultInterface.
30+
* The callable will be called only if this result is Error.
31+
*
32+
* If this result is Success, returns it as is.
33+
* If this result is Error, returns new Error with changed value.
34+
35+
* @template TNewError
36+
*
37+
* @param callable(TError):TNewError $map
38+
* @return self<TSuccess, TNewError>
39+
*/
40+
public function mapError(callable $map): self;
41+
42+
/**
43+
* Chains Success path processing. May either just change Success value, or change the result type to Error.
44+
* Takes a callable which takes **success** value and returns new ResultInterface.
45+
* The callable will be called only if this result is Success.
46+
*
47+
* @template TNewSuccess
48+
* @template TNewError
49+
*
50+
* @param callable(TSuccess):self<TNewSuccess, TNewError> $map
51+
* @return self<TNewSuccess, TError|TNewError>
52+
*/
53+
public function chain(callable $map): self;
54+
55+
/**
56+
* Returns the final value of this result.
57+
* The value will be returned for both Success and Error cases.
58+
*
59+
* @return TSuccess|TError
60+
*/
61+
public function get();
62+
}

src/Success.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Someniatko\ResultType;
6+
7+
/**
8+
* @template-covariant TSuccess
9+
* @template-implements ResultInterface<TSuccess, never-return>
10+
* @psalm-immutable
11+
*/
12+
final class Success implements ResultInterface
13+
{
14+
/** @var TSuccess */
15+
private $value;
16+
17+
/** @param TSuccess $value */
18+
public function __construct($value)
19+
{
20+
$this->value = $value;
21+
}
22+
23+
public function map(callable $map): ResultInterface
24+
{
25+
return new self($map($this->value));
26+
}
27+
28+
public function mapError(callable $map): ResultInterface
29+
{
30+
// no error, so nothing to map.
31+
return $this;
32+
}
33+
34+
public function chain(callable $map): ResultInterface
35+
{
36+
return $map($this->value);
37+
}
38+
39+
public function get()
40+
{
41+
return $this->value;
42+
}
43+
}

0 commit comments

Comments
 (0)