WordPress / WordPress-Coding-Standards

PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions
MIT License
2.55k stars 483 forks source link

Add documentation for each sniff #1722

Open jrfnl opened 5 years ago

jrfnl commented 5 years ago

All sniffs should be accompanied by documentation in the form of a SniffNameStandard.xml file. A SniffNameStandard.xml file contains a (short) description of each rule(s) the sniff checks and a code comparison for each check.

Examples can be found in the PHP_CodeSniffer repository:

The sniff documentation for the complete standard can be shown using the following command:

vendor/bin/phpcs --standard=WordPress --generator=Text

The sniff documentation for an individual sniff can be shown using the following command:

vendor/bin/phpcs --standard=WordPress --generator=Text --sniffs=WordPress.Category.SniffName

Aside from the text generator, there are also generators available for HTML and Markdown, which will allow us to use these documentation files to auto-generate documentation about the sniffs to be placed in, for instance, the wiki.

At this time, none of the WPCS native sniffs have this kind of documentation available.

This issue is intended to track progress for adding this documentation.

Adding this documentation is a focus-task for the WCEU contributor day 2019. During the WCEU Contributor day Gary (@GaryJones ) and Juliette (@jrfnl) will be available to help contributors get set up and answer any questions they may have.

Process

Creating the documentation

Preparation

For each sniff:

Note: where in the below text it says Category, replace this with the category (folder name) of the sniff. And where it says SniffName, replace it with the name of the sniff.

To Do

Claimed

Finished

Not needed

jrfnl commented 5 years ago

@Mike-Hermans, @marconmartins, @Ipstenu, @NielsdeBlaauw, @fkeijzer, @GaryJones, @ckanitz Just wanted to drop you all a note that I'm sorry that I haven't managed to review your PRs yet, even though I'm so incredibly happy with them all. Too many interesting conversations during this #WCEU kept me away from my computer. I promise that I will get to them all within the week once I'm back home in a few days.

jrfnl commented 4 years ago

@marconmartins, @Ipstenu, @NielsdeBlaauw, @GaryJones, @ckanitz I'm planning to release WPCS 2.2.0 in the very near future and I'd love to include as many of the docs as possible in that release.

Aside from the PRs @NielsdeBlaauw send in today, are there any open docs PRs which have been updated after review, but haven't had attention since ?

If so: my apologies. Please do leave a comment either here or on the PR itself to draw attention to it, so we can try to get the PR merged before the release.

Also - if a PR does still need an update, but you either don't have time to do so yourself or have lost interest, please leave a comment on the PR and we'll try and have someone else finish it off.

I really appreciate the work you've done on this and would love to have it go out in the world and help people when they are working with WPCS.

janw-me commented 3 years ago

After about 2 hours of I figured out. How to make an exception for snake_case:

    <rule ref="WordPress.NamingConventions.ValidVariableName">
        <properties>
            <property name="customPropertiesWhitelist[]" value="hive_ID"/>
        </properties>
    </rule>

Now the question is where should this be documented. I did found the xml docs. I don't think the format matches, I might be wrong. Is there a place where this should be documented? Can someone nudge me in the right direction?

jrfnl commented 3 years ago

@janw-me Sorry it took you so long. Next time, you may want to have a look at the wiki (where it is already documented): https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#mixed-case-property-name-exceptions

Edit: Oh and please follow the format as per the example in the wiki. Your current format is not officially supported by PHPCS.

janw-me commented 3 years ago

With googling I didn't find it... And on the github pageI was looking for some "documentation" link but I should have looked for the wiki. I probably should call it a day....

And of course I'll follow the wiki format. the [] is fugly.

jrfnl commented 3 years ago

@janw-me Well, the readme also contains a link to the wiki and mentions customizing sniff behaviour: https://github.com/WordPress/WordPress-Coding-Standards#customizing-sniff-behaviour But if there are other ways in which you think this can be done which will help people find it more easily, I'd welcome a PR ;-)

jrfnl commented 2 years ago

@GaryJones @dingo-d

Opinions ? Anyone up for creating the XSD ?

dingo-d commented 2 years ago

I went and played around with PhpStorm's automatic schema definitions and I got this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="documentation" type="documentationType"/>
  <xs:complexType name="codeType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="title" use="optional"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="code_comparisonType">
    <xs:sequence>
      <xs:element type="codeType" name="code" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="documentationType">
    <xs:sequence>
      <xs:element type="xs:string" name="standard"/>
      <xs:element type="code_comparisonType" name="code_comparison"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="title"/>
  </xs:complexType>
</xs:schema>

In one case (where I used ArrayIndentationStandard.xml as a basis for xsd generation) I got a difference in the <xs:complexType name="documentationType"> part:

<xs:complexType name="documentationType">
  <xs:choice maxOccurs="unbounded" minOccurs="0">
    <xs:element type="xs:string" name="standard"/>
    <xs:element type="code_comparisonType" name="code_comparison"/>
  </xs:choice>
  <xs:attribute type="xs:string" name="title"/>
</xs:complexType>

The xs:choice vs xs:sequence.

I'm not all too familiar with how xds needs to be structured. Do we need to just add that part inside the <xs:complexType name="documentationType"> tag?

jrfnl commented 2 years ago

@dingo-d Nice starting point, though it can probably be abstracted a little more. An XSD basically is a definition of what is elements and attributes are allowed within an XML document, how they can be nested and if they are expected in a certain order etc etc.

The W3Schools information is not bad to get a general impression of the definition language: https://www.w3schools.com/xml/schema_intro.asp Alternatively, for a deep dive, there are the official specs: https://www.w3.org/TR/xmlschema11-1/ ;-)

jrfnl commented 2 years ago

Oh and for inspiration, you could also have a look at the XSD for PHPCS rulesets: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/phpcs.xsd