squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.68k stars 1.48k forks source link

Improve PHPCS documentation #1926

Open Basje opened 6 years ago

Basje commented 6 years ago

In response to this message on Reddit.

I haven't got loads of time to help, but if more people join in then we could make some steps to proper documentation!

@gsherwood: you mentioned that you write documentation professionally. Could you set up some guidelines for us laymen on how to write quality documentation? Links are fine too.

I will post this on Reddit as well, see if we can get some people to join this effort.

jrfnl commented 6 years ago

Just a thought: if all sniffs had proper XML docs associated with them (example), would it not be possible to auto-generate a documentation site based on those ?

This will require the docs to be improved or even added for a lot of sniffs.

It may also require the docs XML standard to be extended and formalized.

One of the things I've been missing, for instance, would be to have a block per error code and to have those error codes as nodes or attributes in the XML. Another thing would be to have the ability to add links for each sniff/error code, which could link to the official standard, like PSR2 could link to the individual paragraphs in the document on the FIG website.

gsherwood commented 6 years ago

The XML format is certainly one place to start.

My intention is to eventually bring up a PHPCS site with hand-written documentation for each error message, with code samples. I could copy/paste from the XML and then re-write anything a bit old or informal.

you mentioned that you write documentation professionally

I do write a lot documentation as part of my job, but I'm not a professional writer. Part of what I do is write from scratch. Another part is taking existing rough notes and turning it into a more complete document. That's really all I think is needed in this specific case. It's a bit of grunt work to start with, followed by a lot of polish.

I thought that the first thing I'd need to do is actually collect a list of all the error messages that PHPCS produces by going through the code of all the sniffs. For each, I wanted to gather:

And if I had time, I'd also produce a snippet of good and bad sample code for each one showing the condition that could cause the error message to be generated.

My next step would be to take a look at that data and try to figure out an easy-to-read format for displaying it all, before re-formatting and adding custom descriptions for each message.

So for me, it's not really about producing super clean and clear documentation right from the get go. My first step is just about gathering raw data. If I have that, I think it could be prettied up enough to put it into the wiki and at least have a list of available error messages. If that's generated from the XML files, that's great. If it's manually collected, I think that's fine too.

Basje commented 6 years ago

I thought that the first thing I'd need to do is actually collect a list of all the error messages that PHPCS produces by going through the code of all the sniffs. For each, I wanted to gather:

  • the error code
  • the error message
  • the severity (error or warning)
  • the error message data (e.g., a class name, a method name, whatever helps customise messages)

And if I had time, I'd also produce a snippet of good and bad sample code for each one showing the condition that could cause the error message to be generated.

Got a preferred location where we can collect this data?

gsherwood commented 6 years ago

Got a preferred location where we can collect this data?

I'll end up throwing it in the wiki, but I know you can't send PRs for Github wiki repos, which is a pain. So anywhere is fine. A gist might be a good option.

GaryJones commented 6 years ago

Manually compiled, but would the data in this format be useful?

```xml Generic.Arrays.DisallowLongArraySyntax.Found Short array syntax must be used to define arrays error 1 Also seen as a non-fixable error for live coding/parse error Generic.Arrays.DisallowShortArraySyntax.Found Short array syntax is not allowed error 1 Generic.Arrays.ArrayIndent.KeyIncorrect Array key not indented correctly; expected %s spaces but found %s error expectedIndent, foundIndent 1 Generic.Arrays.ArrayIndent.CloseBraceNotNewLine Closing brace of array declaration must be on a new line error 1 Generic.Arrays.ArrayIndent.CloseBraceIncorrect Array close brace not indented correctly; expected %s spaces but found %s error expectedIndent, foundIndent 1 Generic.Classes.OpeningBraceSameLine.MissingBrace Possible parse error: %s missing opening or closing brace warning Concatenated tokens content 0 Generic.Classes.OpeningBraceSameLine.BraceOnNewLine Opening brace should be on the same line as the declaration for %s error Concatenated tokens content 1 Generic.Classes.OpeningBraceSameLine.ContentAfterBrace Opening brace must be the last content on the line error 1 Generic.Classes.OpeningBraceSameLine.SpaceBeforeBrace Expected 1 space before opening brace; found %s length 1 Generic.Classes.DuplicateClassName.Found Duplicate %s name "%s" found; first defined in %s on line %s warning type, name, file, line 0 Generic.PHP.ClosingPHPTag.NotFound The PHP open tag does not have a corresponding PHP close tag error 0 Generic.PHP.DiscouragedGoto.Found Use of the GOTO language construct is discouraged warning 0 ```
gsherwood commented 6 years ago

Manually compiled, but would the data in this format be useful?

That format would certainly work.

I did notice some errors codes have type, name, file, line listed as data but I'm assuming that was a copy/paste thing instead of having special meaning. Unless it does have meaning?

GaryJones commented 6 years ago

I did notice some errors codes have type, name, file, line listed as data

Yeah, copy-pasta. Those pieces of data are correct for Generic.Classes.DuplicateClassName.Found, but shouldn't have been there for Generic.PHP.DiscouragedGoto.Found. Previous snippet updated.

There should obviously be one piece of data per placeholder in the message.