Test Failed
Pull Request — master (#124)
by Mirocow
16:46 queued 01:48
created

PhpReader::readRaw()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 44
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 44
rs 8.6186
ccs 1
cts 1
cp 1
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

54
                /** @scrutinizer ignore-unhandled */ @unlink($outputPathFile);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
55
                return $output;
56
57
            } catch (Error $e) {
58
                throw $e;
59
            }
60
        };
61
62
        return $result($params, $path);
63
    }
64
}
65