Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically format PHP annotations in PHPStorm

I'm working with a large number of files that have PHP block docs with Swagger PHP annotations on them, however they are not indented. Is there anyway to automatically format them with spaces?

Turning

/**
 * @SWG\Api(
 * path="/building/{buildingId}",
 * @SWG\Operation(
 * method="GET",
 * type="Building",
 * summary="Returns a Building object by ID",
 * nickname="building/getBuilding",
 * @SWG\Parameter(
 * name="buildingId",
 * description="ID of the building that needs to be fetched",
 * paramType="path",
 * required=false,
 * type="string",
 * defaultValue="1"
 * )
 * )
 * )
 */

Into

/**
 * @SWG\Api(
 *   path="/building/{buildingId}",
 *   @SWG\Operation(
 *     method="GET",
 *     type="Building",
 *     summary="Returns a Building object by ID",
 *     nickname="building/getBuilding",
 *     @SWG\Parameter(
 *       name="buildingId",
 *       description="ID of the building that needs to be fetched",
 *       paramType="path",
 *       required=false,
 *       type="string",
 *       defaultValue="1"
 *     )
 *   )
 * )
 */
like image 238
Ed Knowles Avatar asked Jun 11 '15 17:06

Ed Knowles


People also ask

How can I get beautify code in PhpStorm?

In the editor, select a code fragment you want to reformat. Before reformatting, you can take a look at the code style settings that are applied to the selected code: press Alt+Enter and click Adjust code style settings. From the main menu, select Code | Reformat Code or press Ctrl+Alt+L .

What is PHPDoc in PhpStorm?

In PHPDoc comments, PhpStorm supports formatting options in compliance with the ZEND, PEAR, and other coding standards. PHPDoc comments in your source code are available for Quick Documentation Lookup, which helps you get quick information for any documented symbol.

What are PHP annotations?

PHP annotations are basically metadata which can be included in the source code and also in between classes, functions, properties and methods. They are to be started with the prefix @ wherever they are declared and they indicate something specific.


1 Answers

Looking into this unfortunately there isn't anything built into PHPStorm to do this. I fixed this issue with a bit of node and have open sourced my work if anyone else needs it.

:)

https://github.com/eknowles/grunt-tidy-annotations

like image 78
Ed Knowles Avatar answered Sep 21 '22 15:09

Ed Knowles