Test Failed
Pull Request — master (#124)
by Andrii
12:17
created

PhpReader::parsePhp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Composer\Config\Reader;
6
7
use PhpParser\Node\Stmt;
8
use PhpParser\Node\Stmt\Use_;
9
use PhpParser\ParserFactory;
10
use Yiisoft\Composer\Config\Util\PhpPrinter;
11
12 1
/**
13
 * PhpReader - reads PHP files.
14 1
 */
15
class PhpReader extends AbstractReader
16 1
{
17 1
    protected function readRaw(string $path)
18 1
    {
19
        $ast = $this->parsePhp($path);
20 1
        $this->extractUses($ast);
21
        $newCode = $this->printPhp($ast, $path);
22
23
        $newFile = $this->builder->getOutputPath(strtr($path, '/', '.'));
24
        file_put_contents($newFile, $newCode);
25
        $output = $this->requireWithParams($newFile);
26
        @unlink($newFile);
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

26
        /** @scrutinizer ignore-unhandled */ @unlink($newFile);

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...
27
28
        return $output;
29
    }
30
31
    /**
32
     * @return mixed
33
     */
34
    protected function requireWithParams(string $path)
35
    {
36
        $params = $this->builder->getVars()['params'] ?? [];
37
38
        $result = static function (array $params) {
39
            return require func_get_arg(1);
40
        };
41
                                  
42
        return $result($params, $path);                                  
43
    }
44
45
46
    protected function parsePhp(string $path): array
47
    {
48
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
49
50
        return $parser->parse(file_get_contents($path));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $parser->parse(file_get_contents($path)) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
51
    }
52
53
    protected function extractUses($ast)
54
    {
55
        /** @var Stmt $node */
56
        foreach ($ast as $node){
57
            if (isset($node->type) && $node->type == Use_::TYPE_NORMAL and !empty($node->uses)) {
58
                foreach ($node->uses as $use) {
59
                    $className = end($use->name->parts);
60
                    $this->builder->uses[$className] = 'use ' . implode('\\', $use->name->parts) . ';';
61
                }
62
            }
63
        }
64
    }
65
66
    protected function printPhp(array $ast, string $path): string
67
    {
68
        $printer = new PhpPrinter([
69
            //'builder' => $this->builder,
70
            //'context' => $path,
71
            'THE_DIR' => dirname($path),
72
            'THE_FILE' => $path,
73
        ]);
74
75
        return $printer->prettyPrintFile($ast);
76
    }
77
}
78