Test Failed
Push — master ( b7ab7b...94ee03 )
by Kirill
02:59
created

Strategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Naming;
11
12
use Railt\Reflection\Invocation\ArgumentInterface;
13
use Railt\SDL\Exception\SemanticException;
14
use Railt\SDL\IR\TypeNameInterface;
15
16
/**
17
 * Class Strategy
18
 */
19
class Strategy implements StrategyInterface
20
{
21
    /**
22
     * @var \Closure
23
     */
24
    private $callback;
25
26
    /**
27
     * Strategy constructor.
28
     * @param \Closure $callback
29
     */
30
    public function __construct(\Closure $callback)
31
    {
32
        $this->callback = $callback;
33
    }
34
35
    /**
36
     * @param TypeNameInterface $name
37
     * @param iterable|ArgumentInterface[] $arguments
38
     * @return string
39
     */
40
    public function reduce(TypeNameInterface $name, iterable $arguments): string
41
    {
42
        return $this->verified(($this->callback)($name, $arguments));
43
    }
44
45
    /**
46
     * @param string $name
47
     * @return string
48
     */
49
    protected function verified(string $name): string
50
    {
51
        if (! \preg_match('/^[_a-zA-Z][_a-zA-Z0-9]*$/', $name)) {
52
            $error = \sprintf('Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%s" does not.', $name);
53
            throw new SemanticException($error);
54
        }
55
56
        return $name;
57
    }
58
}
59