VSCodeで快適なPHPの開発環境を整える:クラスのgetter/setter・型の自動生成

環境

  • VsCode 1.63.2
  • PHP 8.0
  • Windows 10

動作イメージ

返り値・引数の型も含めたgetter/setterを1コマンドで複数生成できます(複数選択は、WinならAlt + クリックで、Macならoption + クリックで可能)

本題

  • 前提として、vscodeの拡張機能 PHP Getters&Setters をインストールしておく
PHP Getters & Setters - Visual Studio Marketplace
Extension for Visual Studio Code - Create PHP getters and setters from class properties
  • また、型の情報も含める場合は、phpdocが必要なので、PHP DocBlockerをインストールしておく
PHP DocBlocker - Visual Studio Marketplace
Extension for Visual Studio Code - A simple, dependency free PHP specific DocBlocking package
  • インストールするだけでもある程度使えますが、使いやすいようにカスタマイズするために、設定ファイルを用意します
    • 設定ファイルはjsでgetter・settterそれぞれで用意します
    • OSごとのファイルの場所は以下の通り
      • Linux:~/.config/Code/User/phpGettersSetters
      • Mac:~/Library/Application Support/Code/User/phpGettersSetters
      • Windows:Users\{User}\AppData\Roaming\Code\User\phpGettersSetters
  • 本記事とは直接関係ないですが、phpdocのbooleanとかintegerとかを省略形で記述したいので、setting.jsonでphp-docblocker.useShortNamesというプロパティにチェックを付けています

getter

イメージ

not nullableなプロパティであれば、以下のようにgetterが生成される

<?php

...割愛

class Stock
{
    /**
     * @var int
     */
    private int $id;

    /**
     * @return int
     */
    public function id(): int
    {
        return $this->id;
    }

nullableなプロパティであれば、以下のようにgetterが生成される

<?php

...割愛

class Stock
{ 
    /**
     * @var StockType|null
     */
    private ?StockType $stockType = null;


    /**
     * @return  StockType|null
     */
    public function stockType(): ?StockType
    {
        return $this->stockType;
    }

設定方法

以下のようなjavascriptの設定ファイルを用意することで、getter生成時の内容をカスタマイズすることが可能です

  • propertyの中には、vscode上でgetter/setterを生成したいプロパティの情報がわたってきます
    • getType
      プロパティの型情報(phpdocの)
    • getName
      プロパティの名称
  • getterの名称は、デフォルトだと、get<PropertyName>~の形式ですが、個人的な好みとして、getの部分は削除しています(getName()とかではなく、name()のような感じ)
  • getterの返り値は、以下の通りです
    • nullableのプロパティであれば、型情報にprefixとして?を付与して表示し、それ以外の場合は型情報をそのまま返す

getter.js

module.exports = (property) => `
    /**
     * @return  ${property.getType()}
     */
    public function ${property.getName()}(): ${property.getType().includes("|null")
        ? "?" + property.getType().substr(0, property.getType().indexOf("|"))
        : property.getType()
    }
    {
        return $this->${property.getName()};
    }
`;

setter

イメージ

not nullableなプロパティであれば、以下のようにsetterが生成される

<?php
...割愛

class Stock
{
    /**
     * @var int
     */
    private int $id;

    /**
     * @param int  $id
     * @return self
     */
    public function setId(int $id): self
    {
        $this->id = $id;

        return $this;
    }
}

not nullableなプロパティであれば、以下のようにsetterが生成される

<?php
...割愛

class Stock
{
    /**
     * @var StockType|null
     */
    private ?StockType $stockType = null;
    /**
     * @param StockType|null  $stockType
     * @return self
     */
    public function setStockType(?StockType $stockType): self
    {
        $this->stockType = $stockType;

        return $this;
    }
}

以下のようなjavascriptの設定ファイルを用意することで、setter生成時の内容をカスタマイズすることが可能です

  • propertyの中には、vscode上でgetter/setterを生成したいプロパティの情報がわたってきます
    • getType
      プロパティの型情報(phpdocの)
    • setterName
      セッターの名称(nameであれば、setName()等)
  • setterの引数は、以下の通りです
    • nullableのプロパティであれば、型情報にprefixとして?を付与して表示し、それ以外の場合は型情報をそのまま返す
  • 返り値の型は、自分自身なのでself固定

setter.js

module.exports = (property) => `
    /**
     * @param ${property.getType()}  \$${property.getName()}
     * @return self
     */
    public function ${property.setterName()}( ${property.getType().includes("|null")
        ? "?" + property.getType().substr(0, property.getType().indexOf("|")) + " "
        : property.getType() + " "
    }\$${property.getName()}): self
    {
        $this->${property.getName()} = \$${property.getName()};

        return $this;
    }
`;

以上です。

VSCode学習におすすめの書籍

おすすめの記事

【PHP、Laravel編】VsCodeにインストールするべき必須の便利拡張機能
本記事では、vscodeを用いて行うPHP、Laravel開発を効率化させるvscodeの便利拡張機能を紹介します。効率的でミスのないコーディングを行うために必要な機能が備わった各種プラグインと開発のモチベーションを高めてくれるツールをご紹介しておりますので、まだ使用したことない方はぜひこの機会に使ってみてください。
タイトルとURLをコピーしました