Test Failed
Pull Request — master (#124)
by Andrii
11:47
created

PhpReader::extractUses()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.2222
cc 6
nc 4
nop 1
crap 42
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);
22
23
        //$newFile = $this->builder->getOutputPath(basename($path) . '.' .md5($newCode) . '.ast');
24
        $newFile = $path . '.new.php';
25
        file_put_contents($newFile, $newCode);
26
        $output = $this->requireWithParams($newFile);
27
        @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

27
        /** @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...
28
29
        return $output;
30
    }
31
32
    /**
33
     * @return mixed
34
     */
35
    protected function requireWithParams(string $path)
36
    {
37
        $params = $this->builder->getVars()['params'] ?? [];
38
39
        $result = static function (array $params) {
40
            return require func_get_arg(1);
41
        };
42
                                  
43
        return $result($params, $path);                                  
44
    }
45
46
47
    protected function parsePhp(string $path): array
48
    {
49
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
50
51
        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...
52
    }
53
54
    protected function extractUses($ast)
55
    {
56
        /** @var Stmt $node */
57
        foreach ($ast as $node){
58
            if (isset($node->type) && $node->type == Use_::TYPE_NORMAL and !empty($node->uses)) {
59
                foreach ($node->uses as $use) {
60
                    $className = end($use->name->parts);
61
                    $this->builder->uses[$className] = 'use ' . implode('\\', $use->name->parts) . ';';
62
                }
63
            }
64
        }
65
    }
66
67
    protected function printPhp(array $ast): string
68
    {
69
        $printer = new PhpPrinter([
70
            //'builder' => $this->builder,
71
            //'context' => $path,
72
        ]);
73
74
        return $printer->prettyPrintFile($ast);
75
    }
76
}
77