|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace Gettext\Scanner; |
| 5 | + |
| 6 | +use PhpParser\NodeVisitor; |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\FuncCall; |
| 9 | +use PhpParser\Node\Stmt\Nop; |
| 10 | +use PhpParser\Comment; |
| 11 | + |
| 12 | +class PhpNodeVisitor implements NodeVisitor |
| 13 | +{ |
| 14 | + protected $validFunctions; |
| 15 | + protected $filename; |
| 16 | + protected $functions = []; |
| 17 | + |
| 18 | + public function __construct(string $filename = null, array $validFunctions = null) |
| 19 | + { |
| 20 | + $this->filename = $filename; |
| 21 | + $this->validFunctions = $validFunctions; |
| 22 | + } |
| 23 | + |
| 24 | + public function beforeTraverse(array $nodes) |
| 25 | + { |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + public function enterNode(Node $node) |
| 30 | + { |
| 31 | + if ($node instanceof FuncCall) { |
| 32 | + $name = $node->name->getLast(); |
| 33 | + |
| 34 | + if ($this->validFunctions === null || in_array($name, $this->validFunctions)) { |
| 35 | + $this->functions[] = $this->createFunction($node); |
| 36 | + } |
| 37 | + |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + public function leaveNode(Node $node) |
| 45 | + { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + public function afterTraverse(array $nodes) |
| 50 | + { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + public function getFunctions(): array |
| 55 | + { |
| 56 | + return $this->functions; |
| 57 | + } |
| 58 | + |
| 59 | + protected function createFunction(FuncCall $node): ParsedFunction |
| 60 | + { |
| 61 | + $arguments = []; |
| 62 | + $function = new ParsedFunction( |
| 63 | + $node->name->getLast(), |
| 64 | + $this->filename, |
| 65 | + $node->getStartLine(), |
| 66 | + $node->getEndLine() |
| 67 | + ); |
| 68 | + |
| 69 | + foreach($node->getComments() as $comment) { |
| 70 | + $function->addComment(static::getComment($comment)); |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($node->args as $argument) { |
| 74 | + $value = $argument->value; |
| 75 | + |
| 76 | + foreach ($argument->getComments() as $comment) { |
| 77 | + $function->addComment(static::getComment($comment)); |
| 78 | + } |
| 79 | + |
| 80 | + switch ($value->getType()) { |
| 81 | + case 'Scalar_String': |
| 82 | + case 'Scalar_LNumber': |
| 83 | + case 'Scalar_DNumber': |
| 84 | + $function->addArgument($value->value); |
| 85 | + break; |
| 86 | + |
| 87 | + default: |
| 88 | + $function->addArgument(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return $function; |
| 93 | + } |
| 94 | + |
| 95 | + protected static function getComment(Comment $comment): string |
| 96 | + { |
| 97 | + $text = $comment->getReformattedText(); |
| 98 | + |
| 99 | + $lines = array_map(function ($line) { |
| 100 | + $line = ltrim($line, "#*/ \t"); |
| 101 | + $line = rtrim($line, "#*/ \t"); |
| 102 | + return trim($line); |
| 103 | + }, explode("\n", $text)); |
| 104 | + |
| 105 | + return trim(implode("\n", $lines)); |
| 106 | + } |
| 107 | +} |
0 commit comments