Symfony-docs: Lint PHP code examples

Created on 16 Aug 2019  路  5Comments  路  Source: symfony/symfony-docs

When correcting a typo with missing semi colons, one thing got me thinking. Could we use github action to make sure all PHP code examples have the correct syntax?

We could also extend this to yaml and xml.

Something similar to @OskarStark's https://github.com/OskarStark/php-cs-fixer-ga

Most helpful comment

Yeah, this is one item that has been on my wish list for years. I think extracting the code examples and running them with https://www.php.net/php-check-syntax would be a good thing. We have to update some code examples then (because we not always use ... inside comments for instance), but I don't think that has a negative impact on the code examples.

All 5 comments

Hey Tobias 馃憢

I already tried this:

  • extract all code examples
  • lint them with php-cs-fixer

but it doesn't work as expected.

Lets assume we have the following example:

use Symfony\Component\Foo\Bar;

$bar = new Bar();

It looks valid, but the class Bar does not exist under the given namespace. Therefore we we need to use an exact version of the component (think of all the versionadded directives).
Maybe we could get this working, but we need alle external vendors like doctrine, Twig etc., too 馃憖

Maybe we can fix such simple semicolon issues or sth. else.

I will try to dive into this topic when I find some time. :)

Yeah. We cannot run phpstan or something that does a deep analysis of the code. I think php-cs-fixer may be too much. But just something that verifies the syntax.

Yeah, this is one item that has been on my wish list for years. I think extracting the code examples and running them with https://www.php.net/php-check-syntax would be a good thing. We have to update some code examples then (because we not always use ... inside comments for instance), but I don't think that has a negative impact on the code examples.

Thanks for proposing this. I agree that if we had something with zero maintenance and which worked perfectly for any code, that'd be an awesome addition to the docs.

However, if we can't do that, I don't think it's a big loss. Doc contributions (from the Docs Team or the community) very rarely contain syntax mistakes and if they do, they are quickly fixed by the community reviews. But this is very rare; I can't remember merging PRs to fix syntax errors.

Apparently, even PHP removed a function that existed to do this: php_check_syntax(). So, I'm not sure about the feasibility of automatic validation of only the PHP syntax of our incomplete code snippets.

Let's close this for now and we'll reconsider when we enable the new doc builder in the future (this builder is built with PHP, so it will simplify lots of things). Thanks!

Was this page helpful?
0 / 5 - 0 ratings