Search code examples
phpannotationsphpstormswagger-php

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"
 *     )
 *   )
 * )
 */

Solution

  • 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