Passed
Push — master ( f8eb54...68901a )
by Kirill
04:07
created

Mapping   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 12 3
A has() 0 4 1
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\Compiler\Grammar\PP2;
11
12
/**
13
 * Class Mapping
14
 */
15
class Mapping
16
{
17
    /**
18
     * @var array
19
     */
20
    private $mappings = [];
21
22
    /**
23
     * @var int
24
     */
25
    private $lastId = 0;
26
27
    /**
28
     * @param string|null $rule
29
     * @return int
30
     */
31
    public function id(string $rule = null): int
32
    {
33
        if ($rule === null) {
34
            return $this->lastId++;
35
        }
36
37
        if (! $this->has($rule)) {
38
            $this->mappings[$rule] = $this->lastId++;
39
        }
40
41
        return $this->mappings[$rule];
42
    }
43
44
    /**
45
     * @param string $rule
46
     * @return bool
47
     */
48
    public function has(string $rule): bool
49
    {
50
        return \array_key_exists($rule, $this->mappings);
51
    }
52
}
53