|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AutoMapper\Extractor; |
| 6 | + |
| 7 | +use AutoMapper\Exception\InvalidArgumentException; |
| 8 | +use AutoMapper\Exception\LogicException; |
| 9 | +use AutoMapper\Exception\RuntimeException; |
| 10 | +use PhpParser\Node\Arg; |
| 11 | +use PhpParser\Node\Expr; |
| 12 | +use PhpParser\Node\Identifier; |
| 13 | +use PhpParser\Node\Param; |
| 14 | +use PhpParser\Node\Stmt; |
| 15 | +use PhpParser\Parser; |
| 16 | +use PhpParser\ParserFactory; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Nicolas Philippe <[email protected]> |
| 20 | + * @author Baptiste Leduc <[email protected]> |
| 21 | + * |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +final readonly class AstExtractor |
| 25 | +{ |
| 26 | + private Parser $parser; |
| 27 | + |
| 28 | + public function __construct(?Parser $parser = null) |
| 29 | + { |
| 30 | + $this->parser = $parser ?? (new ParserFactory())->create(ParserFactory::PREFER_PHP7); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Extracts the code of the given method from a given class, and wraps it inside a closure, in order to inject it |
| 35 | + * in the generated mappers. |
| 36 | + * |
| 37 | + * @param class-string $class |
| 38 | + * @param Arg[] $inputParameters |
| 39 | + */ |
| 40 | + public function extract(string $class, string $method, array $inputParameters): Expr |
| 41 | + { |
| 42 | + $fileName = (new \ReflectionClass($class))->getFileName(); |
| 43 | + if (false === $fileName) { |
| 44 | + throw new RuntimeException("You cannot extract code from \"{$class}\" class."); |
| 45 | + } |
| 46 | + $fileContents = file_get_contents($fileName); |
| 47 | + if (false === $fileContents) { |
| 48 | + throw new RuntimeException("File \"{$fileName}\" for \"{$class}\" couldn't be read."); |
| 49 | + } |
| 50 | + |
| 51 | + $statements = $this->parser->parse($fileContents); |
| 52 | + if (null === $statements) { |
| 53 | + throw new RuntimeException("Couldn't parse file \"{$fileName}\" for class \"{$class}\"."); |
| 54 | + } |
| 55 | + |
| 56 | + $namespaceStatement = self::findUnique(Stmt\Namespace_::class, $statements, $fileName); |
| 57 | + /** @var Stmt\Class_ $classStatement */ |
| 58 | + $classStatement = self::findUnique(Stmt\Class_::class, $namespaceStatement->stmts, $fileName); |
| 59 | + |
| 60 | + $classMethod = $classStatement->getMethod($method) ?? throw new LogicException("Cannot find method \"{$method}()\" in class \"{$class}\"."); |
| 61 | + |
| 62 | + if (\count($inputParameters) !== \count($classMethod->getParams())) { |
| 63 | + throw new InvalidArgumentException("Input parameters and method parameters in class \"{$class}\" do not match."); |
| 64 | + } |
| 65 | + |
| 66 | + foreach ($classMethod->getParams() as $key => $parameter) { |
| 67 | + /** @var Expr\Variable $inputParameterValue */ |
| 68 | + $inputParameterValue = $inputParameters[$key]->value; |
| 69 | + |
| 70 | + if ($parameter->var instanceof Expr\Variable && $inputParameterValue->name !== $parameter->var->name) { |
| 71 | + $parameterName = \is_string($parameter->var->name) ? $parameter->var->name : 'N/A'; |
| 72 | + throw new InvalidArgumentException("Method parameter \"{$parameterName}\" does not match type \"{$inputParameters[$key]->getType()}\" from input parameter \"{$inputParameters[$key]->name}\" in \"{$class}::{$method}\" method."); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + $closureParameters = []; |
| 77 | + foreach ($classMethod->getParams() as $parameter) { |
| 78 | + if ($parameter->var instanceof Expr\Variable && $parameter->type instanceof Identifier) { |
| 79 | + $closureParameters[] = new Param(new Expr\Variable($parameter->var->name), type: $parameter->type->name); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return new Expr\FuncCall( |
| 84 | + new Expr\Closure([ |
| 85 | + 'stmts' => $classMethod->stmts, |
| 86 | + 'params' => $closureParameters, |
| 87 | + 'returnType' => $classMethod->returnType, |
| 88 | + ]), |
| 89 | + $inputParameters, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @template T of Stmt |
| 95 | + * |
| 96 | + * @param class-string<T> $searchedStatementClass |
| 97 | + * @param Stmt[] $statements |
| 98 | + * |
| 99 | + * @return T |
| 100 | + */ |
| 101 | + private static function findUnique(string $searchedStatementClass, array $statements, string $fileName): Stmt |
| 102 | + { |
| 103 | + $foundStatements = array_filter( |
| 104 | + $statements, |
| 105 | + static fn (Stmt $statement): bool => $statement instanceof $searchedStatementClass, |
| 106 | + ); |
| 107 | + |
| 108 | + if (\count($foundStatements) > 1) { |
| 109 | + throw new InvalidArgumentException("Multiple \"{$searchedStatementClass}\" found in file \"{$fileName}\"."); |
| 110 | + } |
| 111 | + |
| 112 | + return array_values($foundStatements)[0] ?? throw new InvalidArgumentException("No \"{$searchedStatementClass}\" found in file \"{$fileName}\"."); |
| 113 | + } |
| 114 | +} |
0 commit comments