3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHPStanで配列を型としてチェックする新機能ができていた

Posted at

PHPStan 0.12.84 で Local type aliases の機能が実装されたようだ。

元々、型のエイリアスの機能はあったんだけど、グローバル設定だった。
それがクラス用に定義できるようになったみたいだ。

/**
 * @phpstan-type UserAddress array{street: string, city: string, zip: string}
 */
class User
{
	/**
	 * @var UserAddress
	 */
	private $address; // is of type array{street: string, city: string, zip: string}
}
/**
 * @phpstan-import-type UserAddress from User
 */
class Order
{
	/** @var UserAddress */
	private $deliveryAddress; // is of type array{street: string, city: string, zip: string}
}

今から新しく作るプログラムで使う必要はないけど、古いプログラムで配列を多用していて型チェックできないと困っている場合には有効そうだ。

3
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?