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

DocCommentAlignmentSniff raise error with zircote/swagger-php doc comment tags. #2542

Open eralumin opened 5 years ago

eralumin commented 5 years ago

Hi,

I use zircote/swagger-php for a project, and i notice that PHP Code Sniffer raise Expected 1 space after asterisk; x found error for each line of comment blocks that start with *[2 or more space]@SWG\.

Example of comment blocks that don't raise error with PHP Code Sniffer:

/**
 * @SWG\Parameter(
 *   name="limit",
 *   in="query",
 *   description="Example",
 *   required=false,
 *   type="number",
 *   format="int32",
 *   default=10
 * )

Example of comment blocks that raise error with PHP Code Sniffer:

/**
 * @SWG\Definition(
 *   definition="error",
 *   required={"success", "data"},
 *   @SWG\Xml(name="error"),                
 *       @SWG\Property(
 *           property="success",
 *           format="boolean",
 *           description="Example",
 *           example=false,
 *           default=false
 *       ),
 *       @SWG\Property(
 *           property="data",
 *           format="object",
 *           required={"message", "code"},
 *           @SWG\Property(property="message", format="string", example=400),
 *           @SWG\Property(property="code", format="int32")
 *       )
 * )
 */

Linter Result:

4 | ERROR | [x] Expected 1 space after asterisk; 3 found
5 | ERROR | [x] Expected 1 space after asterisk; 7 found
12 | ERROR | [x] Expected 1 space after asterisk; 7 found
16 | ERROR | [x] Expected 1 space after asterisk; 11 found
17 | ERROR | [x] Expected 1 space after asterisk; 11 found

I think all tags that are not included in this list should be ignored.

gsherwood commented 5 years ago

I think all tags that are not included in this list should be ignored.

Several code bases use custom tags, so limiting to the ones that phpdoc supports would stop reporting errors for all custom tags, which would be a big regression.

I'm not really sure what to do here as I don't see how PHPCS can tell that this is sample code and not a list of documentation tags. Open to ideas.

VincentLanglet commented 5 years ago

I think the correct indentation should be

/**
 * @SWG\Definition(
 *   definition="error",
 *   required={"success", "data"},
 *   @SWG\Xml(name="error"),                
 *   @SWG\Property(
 *       property="success",
 *       format="boolean",
 *       description="Example",
 *       example=false,
 *       default=false
 *   ),
 *   @SWG\Property(
 *       property="data",
 *       format="object",
 *       required={"message", "code"},
 *       @SWG\Property(property="message", format="string", example=400),
 *       @SWG\Property(property="code", format="int32")
 *    )
 * )
 */

@gsherwood If there is a way to detect nested tag and how deep they are nested, it solve the problem. These tags are sort of scope openers.