Skip to content

Validate code blocks #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"symfony/css-selector": "^5.2",
"symfony/console": "^5.2",
"twig/twig": "^2.14 || ^3.3",
"symfony/http-client": "^5.2"
"symfony/http-client": "^5.2",
"symfony/yaml": "^5.2"
},
"require-dev": {
"gajus/dindent": "^2.0",
Expand Down
144 changes: 143 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/Command/BuildDocsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->io->warning($message);
}

$errorCount = \count($buildErrors);
if ($logPath = $input->getOption('save-errors')) {
if (\count($buildErrors) > 0) {
if ($errorCount > 0) {
array_unshift($buildErrors, sprintf('Build errors from "%s"', date('Y-m-d h:i:s')));
}

Expand All @@ -171,8 +172,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->io->newLine(2);

if (\count($buildErrors) > 0) {
$this->io->success('Build completed with warnings');
if ($errorCount > 0) {
$this->io->success(sprintf('Build completed with %s errors', $errorCount));

if ($input->getOption('fail-on-errors')) {
return 1;
Expand Down
5 changes: 5 additions & 0 deletions src/DocsKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Doctrine\RST\Kernel;
use SymfonyDocsBuilder\Listener\AssetsCopyListener;
use SymfonyDocsBuilder\Listener\CopyImagesListener;
use SymfonyDocsBuilder\Listener\ValidCodeNodeListener;

class DocsKernel extends Kernel
{
Expand Down Expand Up @@ -46,6 +47,10 @@ private function initializeListeners(EventManager $eventManager, ErrorManager $e
PreNodeRenderEvent::PRE_NODE_RENDER,
new CopyImagesListener($this->buildConfig, $errorManager)
);
$eventManager->addEventListener(
PreNodeRenderEvent::PRE_NODE_RENDER,
new ValidCodeNodeListener($errorManager)
);

if (!$this->buildConfig->getSubdirectoryToBuild()) {
$eventManager->addEventListener(
Expand Down
161 changes: 161 additions & 0 deletions src/Listener/ValidCodeNodeListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Docs Builder package.
* (c) Ryan Weaver <ryan@symfonycasts.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SymfonyDocsBuilder\Listener;

use Doctrine\RST\ErrorManager;
use Doctrine\RST\Event\PreNodeRenderEvent;
use Doctrine\RST\Nodes\CodeNode;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use Twig\Environment;
use Twig\Error\SyntaxError;
use Twig\Loader\ArrayLoader;
use Twig\Source;

/**
* Verify that all code nodes has the correct syntax.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ValidCodeNodeListener
{
private $errorManager;
private $twig;

public function __construct(ErrorManager $errorManager)
{
$this->errorManager = $errorManager;
}

public function preNodeRender(PreNodeRenderEvent $event)
{
$node = $event->getNode();
if (!$node instanceof CodeNode) {
return;
}

$language = $node->getLanguage() ?? ($node->isRaw() ? null : 'php');
if (in_array($language, ['php', 'php-symfony', 'php-standalone', 'php-annotations'])) {
$this->validatePhp($node);
} elseif ('yaml' === $language) {
$this->validateYaml($node);
} elseif ('xml' === $language) {
$this->validateXml($node);
} elseif ('json' === $language) {
$this->validateJson($node);
} elseif (in_array($language, ['twig', 'html+twig'])) {
$this->validateTwig($node);
}
}

private function validatePhp(CodeNode $node)
{
$file = sys_get_temp_dir().'/'.uniqid('doc_builder', true).'.php';
$contents = $node->getValue();
if (!preg_match('#class [a-zA-Z]+#s', $contents) && preg_match('#(public|protected|private) (\$[a-z]+|function)#s', $contents)) {
$contents = 'class Foobar {'.$contents.'}';
}

// Allow us to use "..." as a placeholder
$contents = str_replace('...', 'null', $contents);

file_put_contents($file, '<?php' .PHP_EOL. $contents);

$process = new Process(['php', '-l', $file]);
$process->run();
$process->wait();
if ($process->isSuccessful()) {
return;
}

$this->errorManager->error(sprintf(
'Invalid PHP syntax in "%s": %s',
$node->getEnvironment()->getCurrentFileName(),
str_replace($file, 'example', $process->getErrorOutput())
));
}

private function validateXml(CodeNode $node)
{
try {
set_error_handler(static function ($errno, $errstr) {
throw new \RuntimeException($errstr, $errno);
});

try {
// Remove first comment only. (No multiline)
$xml = preg_replace('#^<!-- .* -->\n#', '', $node->getValue());
if ('' !== $xml) {
$xmlObject = new \SimpleXMLElement($xml);
}
} finally {
restore_error_handler();
}
} catch (\Throwable $e) {
if ('SimpleXMLElement::__construct(): namespace error : Namespace prefix' === substr($e->getMessage(), 0, 67)) {
return;
}
$this->errorManager->error(sprintf(
'Invalid Xml in "%s": %s',
$node->getEnvironment()->getCurrentFileName(),
$e->getMessage()
));
}
}

private function validateYaml(CodeNode $node)
{
// Allow us to use "..." as a placeholder
$contents = str_replace('...', 'null', $node->getValue());
try {
Yaml::parse($contents, Yaml::PARSE_CUSTOM_TAGS);
} catch (ParseException $e) {
if ('Duplicate key' === substr($e->getMessage(), 0, 13)) {
return;
}

$this->errorManager->error(sprintf(
'Invalid Yaml in "%s": %s',
$node->getEnvironment()->getCurrentFileName(),
$e->getMessage()
));
}
}

private function validateTwig(CodeNode $node)
{
$twig = $this->twig ?? new Environment(new ArrayLoader());

try {
$tokens = $twig->tokenize(new Source($node->getValue(), $node->getEnvironment()->getCurrentFileName()));
// We cannot parse the TokenStream because we dont have all extensions loaded.
// $twig->parse($tokens);
} catch (SyntaxError $e) {
$this->errorManager->error(sprintf(
'Invalid Twig syntax: %s',
$e->getMessage()
));
}
}

private function validateJson(CodeNode $node)
{
$data = json_decode($node->getValue(), true);
if (null === $data) {
$this->errorManager->error(sprintf(
'Invalid Json in "%s"',
$node->getEnvironment()->getCurrentFileName()
));
}
}
}