Completed
Push — aop-class-name ( dbcade )
by Akihito
17s queued 14s
created

Code   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A save() 0 13 4
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