Passed
Pull Request — master (#124)
by Mirocow
12:46
created

PhpReader::readRaw()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 46
ccs 1
cts 1
cp 1
rs 8.6346
c 2
b 0
f 0
cc 7
nc 1
nop 1
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Composer\Config\Reader;
6
7
use PhpParser\Error;
8
use PhpParser\Node\Stmt;
9
use PhpParser\Node\Stmt\Use_;
10
use PhpParser\ParserFactory;
11
use Yiisoft\Composer\Config\Builder;
12 1
use Yiisoft\Composer\Config\Util\PhpRender;
13
14 1
/**
15
 * PhpReader - reads PHP files.
16 1
 */
17 1
class PhpReader extends AbstractReader
18 1
{
19
    protected function readRaw(string $path)
20 1
    {
21
        $params = $this->builder->getVars()['params'] ?? [];
22
23
        $builder = $this->builder;
24
25
        /** @var Builder $result */
26
        $result = static function (array $params) use ($builder) {
27
            $fileName = func_get_arg(1);
28
29
            $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
30
            try {
31
32
                $code = file_get_contents($fileName);
33
                $ast = $parser->parse($code);
34
35
                /** @var Stmt $node */
36
                foreach ($ast as $node){
37
                    if(isset($node->type) && $node->type == Use_::TYPE_NORMAL and !empty($node->uses)){
38
                        foreach ($node->uses as $use) {
39
                            $className = end($use->name->parts);
40
                            $builder->uses[ $className ] = 'use ' . implode('\\', $use->name->parts) . ';';
41
                        }
42
                    }
43
                }
44
45
                /**
46
                 * Everything is not evaluated at compile time by default except Buildtime::* calls.
47
                 * @see https://gist.github.com/samdark/86f2b9ff01a96892efbbf254eca8482d
48
                 */
49
                $prettyPrinter = new PhpRender(['builder' => $builder, 'context' => $fileName]);
50
                $newCode = $prettyPrinter->prettyPrintFile($ast);
0 ignored issues
show
Bug introduced by
It seems like $ast can also be of type null; however, parameter $stmts of PhpParser\PrettyPrinterAbstract::prettyPrintFile() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
                $newCode = $prettyPrinter->prettyPrintFile(/** @scrutinizer ignore-type */ $ast);
Loading history...
51
                $outputPathFile = $builder->getOutputPath(basename($fileName) . '.' .md5($newCode) . '.ast');
52
                file_put_contents($outputPathFile, $newCode);
53
                $output = require $outputPathFile;
54
                //@unlink($outputPathFile);
55
                return $output;
56
57
            } catch (Error $e) {
58
                throw $e;
59
                //echo "Parse error: {$error->getMessage()}\n";
60
                //return;
61
            }
62
        };
63
64
        return $result($params, $path);
65
    }
66
}
67