Tuple   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A element() 0 3 2
A elementId() 0 5 2
A __get() 0 9 1
A __construct() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scalp;
6
7
use const Scalp\__;
0 ignored issues
show
Bug introduced by
The type Scalp\__ was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Scalp\Exception\NoSuchElementException;
9
use const Scalp\identity;
0 ignored issues
show
Bug introduced by
The type Scalp\identity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use const Scalp\inc;
0 ignored issues
show
Bug introduced by
The type Scalp\inc was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use function Scalp\None;
12
use function Scalp\papply;
0 ignored issues
show
Bug introduced by
The type Scalp\papply was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Scalp\PatternMatching\CaseClass;
14
use Scalp\PatternMatching\Deconstruction;
15
use function Scalp\Some;
16
use const Scalp\throwE;
0 ignored issues
show
Bug introduced by
The type Scalp\throwE was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use function Scalp\Utils\delay;
18
19
final class Tuple implements CaseClass
20
{
21
    use Deconstruction;
22
23
    private $elements;
24
25
    public function __construct(...$elements)
26
    {
27
        if (\count($elements) === 0) {
28
            throw new \InvalidArgumentException('Tuple must by construct with at least one element.');
29
        }
30
31
        $this->construct(...$elements);
32
33
        $this->elements = $elements;
34
    }
35
36
    public function __get($name)
37
    {
38
        return $this
39
            ->elementId($name)
40
            ->map(papply(inc, __, -1))
41
            ->flatMap(\Closure::fromCallable([$this, 'element']))
42
            ->fold(
43
                delay(throwE, NoSuchElementException::class, "Tuple->$name"),
44
                identity
45
            );
46
    }
47
48
    private function elementId(string $propertyName): Option
49
    {
50
        preg_match('/^_(\d+)$/', $propertyName, $matches);
51
52
        return isset($matches[1]) ? Some((int) $matches[1]) : None();
53
    }
54
55
    private function element(int $id): Option
56
    {
57
        return isset($this->elements[$id]) ? Some($this->elements[$id]) : None();
58
    }
59
}
60