1
0
mirror of https://github.com/danog/phpdoc.git synced 2024-11-29 20:19:03 +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": {
"php": ">=7.4.0",
"danog/class-finder": "^0.4",
"phpdocumentor/reflection-docblock": "^5.2"
"phpdocumentor/reflection-docblock": "^5.2",
"symfony/yaml": "^6.0"
},
"require-dev": {
"vimeo/psalm": "dev-master",

View File

@ -99,7 +99,7 @@ class ClassDoc extends GenericDoc
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;
}
$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 ReflectionClass;
use ReflectionFunction;
use Symfony\Component\Yaml\Escaper;
/**
* Generic documentation builder.
@ -179,10 +180,12 @@ abstract class GenericDoc
$count = \count(\explode('\\', $this->resolvedClassName)) - 2;
$index .= \str_repeat('../', $count);
$index .= 'index.md';
$titleEscaped = Escaper::escapeWithDoubleQuotes("{$this->name}: {$this->title}");
$descriptionEscaped = Escaper::escapeWithDoubleQuotes($this->description);
return <<<EOF
---
title: $this->name: $this->title
description: $this->description
title: $titleEscaped
description: $descriptionEscaped
$image
---
# `$this->name`
@ -214,7 +217,7 @@ abstract class GenericDoc
if ($type === $this->resolvedClassName) {
continue;
}
if (str_contains($type, ' ')) {
if (\str_contains($type, ' ')) {
continue;
}
try {

View File

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