2
2

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を用いた際のエラー解消

Last updated at Posted at 2021-08-25

##~ no value type specified in iterable type array のエラー
以下のエラーに対する改善方法
自分的な解釈では"配列の中身の値に対しても型指定してください"みたいなイメージ

  5      Function saleByTime() return type has no value type specified in iterable type array.                       
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type

解消後のコードは以下の通り

// 以下の*部分3行の記述で返り値である$priceの連想配列の中身のキー、値に対する型を指定することで上記のエラーは解消される
/**
 * @return array<int, int> $unitSoldNumber
 */
function saleByTime(string $time): array
{
    $price = [
        1 => 100,
        2 => 150,
        3 => 200,
        4 => 350,
        5 => 180,
        6 => 220,
        7 => 440,
        8 => 380,
        9 => 80,
        10 => 100,
    ];

    // タイムセール割引
    $hourNumber = explode(':', $time);
    if ($hourNumber[0] >= 20 && $hourNumber[0] <= 22) {
        $price[7] /= 2;
        $price[8] /= 2;
    }

    return $price;
}
2
2
0

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?