Completed
Push — 2.x ( 8c3362...d6db4d )
by Akihito
13s queued 11s
created

src/Code.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use PhpParser\Node;
8
use PhpParser\PrettyPrinter\Standard;
9
use Ray\Aop\Exception\NotWritableException;
10
11
final class Code
12
{
13
    /**
14
     * @var string
15
     */
16
    public $code;
17
18
    /**
19
     * @param array<Node> $stmt
20
     */
21
    public function __construct(array $stmt)
22
    {
23
        $this->code = (new Standard(['shortArraySyntax' => true]))->prettyPrintFile($stmt) . PHP_EOL;
24
    }
25
26
    public function save(string $classDir, string $aopClassName) : string
27
    {
28
        class_exists($aopClassName);
29
        $flatName = str_replace('\\', '_', $aopClassName);
30
        $filename = sprintf('%s/%s.php', $classDir, $flatName);
31
        $tmpFile = tempnam(dirname($filename), 'swap');
32
        if (is_string($tmpFile) && file_put_contents($tmpFile, $this->code) && rename($tmpFile, $filename)) {
33
            return $filename;
34
        }
35
        @unlink((string) $tmpFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
36
37
        throw new NotWritableException(sprintf('swap: %s, file: %s', $tmpFile, $filename));
38
    }
39
}
40