| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace PHPStan\BetterReflection\Reflection; |
| 6: | |
| 7: | use PhpParser\Node\Expr; |
| 8: | use PHPStan\BetterReflection\BetterReflection; |
| 9: | |
| 10: | final class ExprCacheHelper |
| 11: | { |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | public static function export(Expr $expr): array |
| 17: | { |
| 18: | $br = new BetterReflection(); |
| 19: | |
| 20: | $attributes = []; |
| 21: | foreach (['startLine', 'endLine', 'startTokenPos', 'startFilePos', 'endTokenPos', 'endFilePos'] as $key) { |
| 22: | $attributes[$key] = $expr->getAttribute($key); |
| 23: | } |
| 24: | |
| 25: | return [ |
| 26: | 'code' => $br->printer()->prettyPrintExpr($expr), |
| 27: | 'attributes' => $attributes, |
| 28: | ]; |
| 29: | } |
| 30: | |
| 31: | public static function import(array $data): Expr |
| 32: | { |
| 33: | $code = $data['code']; |
| 34: | $attributes = $data['attributes']; |
| 35: | |
| 36: | $br = new BetterReflection(); |
| 37: | $expr = $br->phpParser()->parse('<?php ' . $code . ';')[0]->expr; |
| 38: | foreach ($attributes as $key => $value) { |
| 39: | $expr->setAttribute($key, $value); |
| 40: | } |
| 41: | |
| 42: | return $expr; |
| 43: | } |
| 44: | |
| 45: | } |
| 46: | |