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

Strategy   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A reduce() 0 4 1
A verified() 0 9 2
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