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

Mapping::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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