Skip to content
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

Automated type checking of code samples in docs #11494

Open
SamMousa opened this issue Jun 11, 2024 · 3 comments
Open

Automated type checking of code samples in docs #11494

SamMousa opened this issue Jun 11, 2024 · 3 comments

Comments

@SamMousa
Copy link
Contributor

          True. I don't know how to do that however.

Originally posted by @greg0ire in #11492 (comment)

I might be able to help set this up.
Started on a proof of concept extractor already.

<?php

$lines = file(__DIR__ . '/en/tutorials/composite-primary-keys.rst', FILE_IGNORE_NEW_LINES);

$indent = null;
$blockCounter = 0;
$block = [];
foreach($lines as $i => $line) {
    // Check if this is the start of a php block.
    if ($indent === null && preg_match('/^(\s*)\<\?php/', $line, $matches)) {
        echo "Start PHP block\n";
        $indent = strlen($matches[1]);
        $block[] = "<?php //Extracted from line $i\n";
    } elseif ($indent !== null && preg_match("/^\s{{$indent}}(.*)|^()$/", $line, $matches)) {
        $block[] = ($matches[1] ?? ''); // . "\t\t\t\t# Extracted from line $i";
    } elseif ($indent !== null) {
        $indent = null;
        print_r($block);
        file_put_contents("/tmp/check/block{$blockCounter}.php", implode("\n", $block));
        $blockCounter++;
        $block = [];

    }

}

The idea is simple:

  • Parse each .rst file crudely extracting PHP code blocks.
  • Add missing imports automatically (or decide that we want copy paste friendly docs and just add missing imports to the examples)
  • Run phpstan to analyse the code.

The extractor above also adds a comment to know where we got it from:

<?php //Extracted from line 112

namespace VehicleCatalogue\Model;

// $em is the EntityManager

$car = new Car("Audi A8", 2010);
$em->persist($car);
$em->flush();

I'll look into creating a github action that just does this for all RST files in a directory.

@greg0ire
Copy link
Member

Add missing imports automatically (or decide that we want copy paste friendly docs and just add missing imports to the examples)

I think we should have copy/paste friendly docs

@SamMousa
Copy link
Contributor Author

I'm ready to push the PR, what branch should I target?

@greg0ire
Copy link
Member

2.19.x please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants