Skip to content

date() is not typed even if the format is non-empty-string #10468

@zonuexe

Description

@zonuexe

Bug report

If the format is non-empty-string, the return value is always non-empty-string.

I have a use case like below. (Other processing is added to the actual code.)

<?php declare(strict_types = 1);

/**
 * @see http://php.net/date
 * @phpstan-param non-empty-string&literal-string $format
 * @phpstan-return non-empty-string
 */
function date_wrapper(string $format, int $timestamp = null): string
{
	if ($timestamp !== null) {
		return \date($format, $timestamp);
	}

	return \date($format);
}

Code snippet that reproduces the problem

https://phpstan.org/r/33a9afc0-e4cd-4bdb-9caa-5ea1b294ad03

Expected output

No errors.

Did PHPStan help you today? Did it make you happy in any way?

The other day my friend talked about PHPStan at a meetup.

The meetup was held the day before PHP Conference Hokkaido.

image
image
image

The last slide quotes 予防に勝る防御なし - 堅牢なコードを導く様々な設計のヒント / Growing Reliable Code PHPerKaigi 2022 and says, The longer a problem is discovered, the deeper the damage will be.

The slides of his presentation can be found at PHP における静的解析(あるいはそもそも静的解析とは) / #phpcondo_yasai static analysis for PHP - Speaker Deck.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions