fetchSingle returning mixed, how to approach?

Open
#31 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
20/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
php
Domain
tooling

Research direction

Start by locating the fetchSingle entry point and the extension code that describes its return type. Compare the declared mixed result with the documented scalar and null behavior, then define the expected typing for different queries and add regression coverage showing that valid calls no longer require the reported workaround.

Written by the indexing model from the issue text.

Description

How to properly solve those?
I know it will allways be ?int, but since fetchSingle is marked as returning mixed I can't solve this. I wouldn't want to just skip those checks....

	public function getIdByKey(string $key): ?int
	{
		return $this->db->select('id')->from($this->table)->where('%n = %s', 'key', $key)->fetchSingle();
	}

ofcours, here is integer in some other query will be something else...

only thing I think off as correct would maybe be:

	public function getIdByKey(string $key): ?int
	{
		/** @var int|null $id */
		$id = $this->db->select('id')->from($this->table)->where('%n = %s', 'key', $key)->fetchSingle();
	}
Dominant language
PHP
Stars
16
Forks
11
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-dibi

All issues in phpstan/phpstan-dibi

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.