Skip to content

Handling different type definitions accros multiple PHP versions #4060

@hrach

Description

@hrach

PHP 8.0 brings reworked type definitions and PhpStan utilize this new information.

Until now, when the PhpStan didn't know the type correctly, the code would handle it properly and we added @phpstan-ignore-line annotation, which had supressed the PhpStan's missing knowledge. The PHP8 definition support however breaks this, since PhpStan begins reporting that the ignore comment ignores nothing.

Current solution - stick with phpstan-ignore and also add reportUnmatchedIgnoredErrors: false configuration.

https://phpstan.org/r/417d57f1-4fe6-488a-91e8-7e7073f621eb


Possible solution: Introduce special ignore annotation that are checked only in the specific passed version:

  • @phpstan-ignore-line-until 8.0 - this ignore will be ignoring only in PHP versions < 8.0
  • @phpstan-ignore-line-since 8.0 - this ignore will be ignoring only in PHP versions >= 8.0
  • no unmatched error is reported on PHP versions which does not match

The example above would look like

/**
 * @param \PDOStatement<mixed> $statement
 */
function getType(\PDOStatement $statement): string
{
	$meta = $statement->getColumnMeta(0);	
	if ($meta === false) { // @phpstan-ignore-line-until 8.0
		throw new RuntimeException();
	}
	return $meta['type'] ?? '';
}

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