IncludeDelegate::getPathname()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 9.7666
c 0
b 0
f 0
cc 3
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\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