1
0
mirror of https://github.com/danog/phpdoc.git synced 2024-11-26 12:04:47 +01:00

Improve yaml escaping

This commit is contained in:
Daniil Gentili 2021-12-20 13:46:30 +01:00
parent dfba7f3961
commit c40f3c8fca
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 22 additions and 14 deletions

View File

@ -8,7 +8,8 @@
"require": { "require": {
"php": ">=7.4.0", "php": ">=7.4.0",
"danog/class-finder": "^0.4", "danog/class-finder": "^0.4",
"phpdocumentor/reflection-docblock": "^5.2" "phpdocumentor/reflection-docblock": "^5.2",
"symfony/yaml": "^6.0"
}, },
"require-dev": { "require-dev": {
"vimeo/psalm": "dev-master", "vimeo/psalm": "dev-master",

View File

@ -99,7 +99,7 @@ class ClassDoc extends GenericDoc
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if (str_starts_with($method->getName(), '__') && $method !== '__construct') { if (\str_starts_with($method->getName(), '__') && $method !== '__construct') {
continue; continue;
} }
$this->methods[$method->getName()] = new MethodDoc($this->builder, $method); $this->methods[$method->getName()] = new MethodDoc($this->builder, $method);

View File

@ -13,6 +13,7 @@ use phpDocumentor\Reflection\DocBlock\Tags\See;
use phpDocumentor\Reflection\Fqsen as ReflectionFqsen; use phpDocumentor\Reflection\Fqsen as ReflectionFqsen;
use ReflectionClass; use ReflectionClass;
use ReflectionFunction; use ReflectionFunction;
use Symfony\Component\Yaml\Escaper;
/** /**
* Generic documentation builder. * Generic documentation builder.
@ -179,10 +180,12 @@ abstract class GenericDoc
$count = \count(\explode('\\', $this->resolvedClassName)) - 2; $count = \count(\explode('\\', $this->resolvedClassName)) - 2;
$index .= \str_repeat('../', $count); $index .= \str_repeat('../', $count);
$index .= 'index.md'; $index .= 'index.md';
$titleEscaped = Escaper::escapeWithDoubleQuotes("{$this->name}: {$this->title}");
$descriptionEscaped = Escaper::escapeWithDoubleQuotes($this->description);
return <<<EOF return <<<EOF
--- ---
title: $this->name: $this->title title: $titleEscaped
description: $this->description description: $descriptionEscaped
$image $image
--- ---
# `$this->name` # `$this->name`
@ -214,7 +217,7 @@ abstract class GenericDoc
if ($type === $this->resolvedClassName) { if ($type === $this->resolvedClassName) {
continue; continue;
} }
if (str_contains($type, ' ')) { if (\str_contains($type, ' ')) {
continue; continue;
} }
try { try {

View File

@ -23,6 +23,7 @@ use phpDocumentor\Reflection\DocBlock\Tags\Author;
use phpDocumentor\Reflection\DocBlockFactory; use phpDocumentor\Reflection\DocBlockFactory;
use ReflectionClass; use ReflectionClass;
use ReflectionFunction; use ReflectionFunction;
use Symfony\Component\Yaml\Escaper;
/** /**
* Documentation builder. * Documentation builder.
@ -240,14 +241,17 @@ class PhpDoc
$interfaces = $interfaces ? "## Interfaces\n$interfaces" : ''; $interfaces = $interfaces ? "## Interfaces\n$interfaces" : '';
$classes = $classes ? "## Classes\n$classes" : ''; $classes = $classes ? "## Classes\n$classes" : '';
$description = explode("\n", $this->description); $description = \explode("\n", $this->description);
$description = $description[0] ?? ''; $description = $description[0] ?? '';
$descriptionEscaped = Escaper::escapeWithDoubleQuotes($description);
$nameEscaped = Escaper::escapeWithDoubleQuotes($this->name);
$image = $this->getImage(); $image = $this->getImage();
$index = <<<EOF $index = <<<EOF
--- ---
title: $this->name title: $nameEscaped
description: $description$image description: $descriptionEscaped$image
--- ---
# `$this->name` # `$this->name`
@ -309,11 +313,11 @@ class PhpDoc
*/ */
public function resolveTypeAlias(string $fromClass, string $name, array &$resolved): string public function resolveTypeAlias(string $fromClass, string $name, array &$resolved): string
{ {
if (str_ends_with($name, '[]')) { if (\str_ends_with($name, '[]')) {
return $this->resolveTypeAlias($fromClass, \substr($name, 0, -2), $resolved)."[]"; return $this->resolveTypeAlias($fromClass, \substr($name, 0, -2), $resolved)."[]";
} }
if ($name[0] === '(' && $name[strlen($name) - 1] === ')') { if ($name[0] === '(' && $name[\strlen($name) - 1] === ')') {
$name = $this->resolveTypeAlias($fromClass, substr($name, 1, -1), $resolved); $name = $this->resolveTypeAlias($fromClass, \substr($name, 1, -1), $resolved);
return "($name)"; return "($name)";
} }
if (\count($split = self::splitOnWithoutParenthesis('|', $name)) > 1) { if (\count($split = self::splitOnWithoutParenthesis('|', $name)) > 1) {
@ -322,11 +326,11 @@ class PhpDoc
} }
return \implode('|', $split); return \implode('|', $split);
} }
if (str_starts_with($name, 'callable(')) { if (\str_starts_with($name, 'callable(')) {
$name = $this->resolveTypeAlias($fromClass, substr($name, 9, -1), $resolved); $name = $this->resolveTypeAlias($fromClass, \substr($name, 9, -1), $resolved);
return "callable($name)"; return "callable($name)";
} }
if (str_starts_with($name, 'array{')) { if (\str_starts_with($name, 'array{')) {
$new = ''; $new = '';
$split = self::splitOnWithoutParenthesis(',', \substr($name, 6, -1)); $split = self::splitOnWithoutParenthesis(',', \substr($name, 6, -1));
foreach ($split as $key => $var) { foreach ($split as $key => $var) {