Skip to content

Array offset in OR condition can not be resolved. #6379

Closed
phpstan/phpstan-src
#6382

Description

@janbarasek

Bug report

In case of code:

<?php declare(strict_types = 1);

class HelloWorld
{
	/**
	 * @param array{
	 *    cr?: string,
	 *    c?: string
	 * } $params
	 */
	public static function paramsToString(array $params): string
	{
		if (isset($params['cr']) === true || isset($params['c']) === true) {
			return sprintf('-c%s', $params['cr'] ?? $params['c']);
		}

		return '';
	}
}

PhpStan will report:

Offset 'c' does not exist on array{cr?: string, c?: string}.

Live example: https://phpstan.org/r/f10dbfb6-e501-41e8-8a37-aa9e9a61489b

Related to: #6377

Thanks.

Activity

  1. added this to the Dependent types milestone on Jan 17, 2022
  2. phpstan-bot commented on Apr 2, 2022

    @phpstan-bot
    Contributor

    @janbarasek After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -14: Offset 'c' does not exist on array{cr?: string, c?: string}.
    +No errors
  3. github-actions commented on May 4, 2022

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  4. locked as resolved and limited conversation to collaborators on May 4, 2022
  5. ondrejmirtes commented on Feb 26, 2026

    @ondrejmirtes
    Member

    Reopening, this was not truly fixed. See phpstan/phpstan-src#4399

  6. unlocked this conversation on Feb 26, 2026
  7. phpstan-bot commented on Feb 26, 2026

    @phpstan-bot
    Contributor

    @janbarasek After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -14: Offset 'c' does not exist on array{cr?: string, c?: string}.
    +14: Offset 'c' might not exist on non-empty-array{cr?: string, c?: string}.
    Full report
    Line Error
    14 Offset 'c' might not exist on non-empty-array{cr?: string, c?: string}.
  8. github-actions commented on Oct 14, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  9. locked as resolved and limited conversation to collaborators on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions