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
|
|||
36 | |||
37 | throw new NotWritableException(sprintf('swap: %s, file: %s', $tmpFile, $filename)); |
||
38 | } |
||
39 | } |
||
40 |
If you suppress an error, we recommend checking for the error condition explicitly: