静的にする
static にするリファクタリングは、インスタンスメソッドを静的メソッドに変換し、メソッドのすべての呼び出し、実装、オーバーライドを自動的に修正します。
構造 ビューでリファクタリングするメソッドを選択するか、エディターでメソッド名を右クリックします。 選択のメインメニューまたはコンテキストメニューから、 を選択します。 メソッドがパラメーターを必要とせず、このオブジェクトのプロパティまたはメソッドへのアクセスに依存しない場合は、ダイアログを表示せずに、静的メソッドがサイレントで作成されます。
class MyClass { private function getFormattedDate() { $format = getSettings()['dateFormat']; return time($format); } }class MyClass { private static function getFormattedDate() { $format = getSettings()['dateFormat']; return time($format); } }より複雑なケースでは、 メソッドを static にする ダイアログが開きます。
メソッドを static にする ダイアログで、次のいずれかの操作を行います。
現在、
$this経由で既存のオブジェクトを使用している場合は、 命名したパラメーターとしてオブジェクトを追加する チェックボックスをオンにして、オブジェクトのインスタンスをパラメーター経由で渡します。 下のフィールドに、生成するパラメーターの名前を指定します。class MyClass { // Invoke Make static refactoring on getFormattedDate() private function getFormattedDate() { $format = $this->getSettings()['dateFormat']; return time($format); } private function call() { $this->getFormattedDate(); } private function getSettings() { return array(); } }class MyClass { private static function getFormattedDate(MyClass $instance) { $format = $instance->getSettings()['dateFormat']; return time($format); } // Method call on the object gets refactored as well private function call() { MyClass::getFormattedDate($this); } private function getSettings() { return array(); } }クラスのプロパティにアクセスする場合は、新しく作成した静的メソッド内のオブジェクトにアクセスするのではなく、 プロパティのパラメーターを追加する 領域を使用してプロパティの値をパラメーターとして渡します。
プロパティのパラメーターを追加する チェックボックスを選択します。
可能性のあるすべてのパラメーターを示す パラメーター リストで、値を渡すものの横にあるチェックボックスを選択します。
class PriceCalculator { public float $productPrice = 1.0; public float $taxAmount = 0.3; // Invoke Make static refactoring on printTotalCost() public function printTotalCost(): void { $total = $this->productPrice + $this->taxAmount; echo $total; } } $calculator = new PriceCalculator(); $calculator->printTotalCost();class PriceCalculator { public float $productPrice = 1.0; public float $taxAmount = 0.3; // A PHPDoc block with documented parameters added /** * @param $productPrice * @param $taxAmount */ // Selected properties are passed as parameters to the refactored static method public static function printTotalCost($productPrice, $taxAmount): void { $total = $productPrice + $taxAmount; echo $total; } } // Method call on the object gets refactored as well $calculator = new PriceCalculator(); PriceCalculator::printTotalCost($calculator->productPrice, $calculator->taxAmount);
結果をプレビューするには、 プレビュー をクリックし、 Find ツールウィンドウでリファクタリングの結果を確認します。 問題が発生しない場合は、変更を適用します。
2026 年 5 月 22 日