Completed
Push — master ( 8a5799...ed0b0a )
by Kirill
07:58
created

IncludeDelegate::getPathname()   A

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