IncludeDelegate   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 24
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getPathname() 0 15 3
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\Delegate;
11
12
use Railt\Compiler\Exception\IncludeNotFoundException;
13
use Railt\Io\File;
14
use Railt\Io\Readable;
15
use Railt\Parser\Ast\Rule;
16
17
/**
18
 * Class IncludeDelegate
19
 */
20
class IncludeDelegate extends Rule
21
{
22
    /**
23
     * @param Readable $from
24
     * @return Readable
25
     * @throws \Railt\Io\Exception\NotReadableException
26
     * @throws \Railt\Io\Exception\ExternalFileException
27
     */
28
    public function getPathname(Readable $from): Readable
29
    {
30
        $file = $this->getValue(1);
31
32
        foreach (['', '.pp', '.pp2'] as $ext) {
33
            $path = \dirname($from->getPathname()) . '/' . $file . $ext;
34
35
            if (\is_file($path)) {
36
                return File::fromPathname($path);
37
            }
38
        }
39
40
        throw (new IncludeNotFoundException(\sprintf('Grammar "%s" not found', $file)))
41
            ->throwsIn($from, $this->getOffset());
42
    }
43
}
44