Completed
Push — develop ( d02000...7943c0 )
by Baptiste
01:44
created

Enum::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Config\Property;
5
6
use Innmind\Config\{
7
    Property,
8
    Properties,
9
    Exception\SchemaNotParseable,
10
    Exception\InvalidArgumentException,
11
};
12
use Innmind\Immutable\{
13
    Str,
14
    SetInterface,
15
    Set,
1 ignored issue
show
Bug introduced by
This use statement conflicts with another class in this namespace, Innmind\Config\Property\Set. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
};
17
18
final class Enum implements Property
19
{
20
    private const PATTERN = '~^\??enum\((?<values>.+)\)$~';
21
22
    private $values;
23
    private $optional = false;
24
25 13
    private function __construct(Set $values)
26
    {
27 13
        $this->values = $values;
28 13
    }
29
30 15
    public static function build(Str $schema, Properties $properties): Property
31
    {
32 15
        if (!$schema->matches(self::PATTERN)) {
33 2
            throw new SchemaNotParseable((string) $schema);
34
        }
35
36 13
        $self = new self(
37
            $schema
38 13
                ->capture(self::PATTERN)
39 13
                ->get('values')
40 13
                ->split('|')
41 13
                ->reduce(
42 13
                    Set::of('string'),
43 13
                    static function(SetInterface $values, Str $value): SetInterface {
44 13
                        return $values->add((string) $value);
45 13
                    }
46
                )
47
        );
48
49 13
        if ((string) $schema->substring(0, 1) === '?') {
50 6
            $self->optional = true;
51
        }
52
53 13
        return $self;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 7
    public function process($value)
60
    {
61 7
        if (is_null($value) && $this->optional) {
62 3
            return null;
63
        }
64
65 4
        if (!$this->values->contains($value)) {
66 1
            throw new InvalidArgumentException;
67
        }
68
69 3
        return $value;
70
    }
71
}
72