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

IncludeDelegate   A

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\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