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

Mapping::id()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 12
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