Ecodev /
graphql-doctrine
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace GraphQL\Doctrine; |
||
| 6 | |||
| 7 | use ReflectionMethod; |
||
| 8 | use ReflectionParameter; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * A basic doc block reader to extract. |
||
| 12 | */ |
||
| 13 | final class DocBlockReader |
||
| 14 | { |
||
| 15 | private readonly string $comment; |
||
| 16 | |||
| 17 | 43 | public function __construct(ReflectionMethod $method) |
|
| 18 | { |
||
| 19 | 43 | $this->comment = $method->getDocComment() ?: ''; |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get the description of a method from the doc block. |
||
| 24 | */ |
||
| 25 | 27 | public function getMethodDescription(): ?string |
|
| 26 | { |
||
| 27 | // Remove the comment markers |
||
| 28 | 27 | $description = preg_replace('~\*/$~', '', $this->comment); |
|
| 29 | 27 | $description = preg_replace('~^\s*(/\*\*|\* ?|\*/)~m', '', $description); |
|
| 30 | |||
| 31 | // Keep everything before the first annotation |
||
| 32 | 27 | $description = trim(explode('@', $description ?? '')[0]); |
|
| 33 | |||
| 34 | // Drop common "Get" or "Return" in front of comment |
||
| 35 | 27 | $description = ucfirst(preg_replace('~^(set|get|return)s? ~i', '', $description) ?? ''); |
|
| 36 | |||
| 37 | 27 | return $description ?: null; |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Get the parameter description. |
||
| 42 | */ |
||
| 43 | 15 | public function getParameterDescription(ReflectionParameter $param): ?string |
|
| 44 | { |
||
| 45 | 15 | $name = preg_quote($param->getName()); |
|
| 46 | |||
| 47 | 15 | if (preg_match('~@param\h+\H+\h+\$' . $name . '\h+(.*)~', $this->comment, $m)) { |
|
| 48 | 3 | return ucfirst(trim($m[1])); |
|
| 49 | } |
||
| 50 | |||
| 51 | 14 | return null; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get the parameter type. |
||
| 56 | */ |
||
| 57 | 23 | public function getParameterType(ReflectionParameter $param): ?string |
|
| 58 | { |
||
| 59 | 23 | $name = preg_quote($param->getName()); |
|
| 60 | |||
| 61 | 23 | if (preg_match('~@param\h+(\H+)\h+\$' . $name . '(\h|\n)~', $this->comment, $m)) { |
|
| 62 | 11 | return trim($m[1]); |
|
| 63 | } |
||
| 64 | |||
| 65 | 18 | return null; |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the return type. |
||
| 70 | */ |
||
| 71 | 17 | public function getReturnType(): ?string |
|
| 72 | { |
||
| 73 | 17 | if (preg_match('~@return\h+([^<]+<.*>|\H+)(\h|\n)~', $this->comment, $m)) { |
|
| 74 | 9 | return trim($m[1]); |
|
| 75 | } |
||
| 76 | |||
| 77 | 13 | return null; |
|
| 78 | } |
||
| 79 | } |
||
| 80 |