Skip to content

wrong type detection in reassignment #124

@Disservin

Description

@Disservin

PHPantom version

phpantom_lsp 0.8.0-2-g7c86d78c

Installation method

Pre-built binary from GitHub Releases

Operating system

Linux x86_64

Editor

Zed

Bug description

Wrong type detection

Argument 2 ($string) expects string, got array<string>

For this line:

$rule = explode(':', $rule);

This is wrong. The $rule passed to explode() should use the type from before the assignment. The return type of explode() should only apply to $rule after the RHS has been checked.

Expected:

$rule in the explode call should be mixed and the LHS $rule should be array<string>

$rule = explode(':', $rule)

Actual:

$rule = explode(':', $rule) // LSP treats both $rule as the same type (array<string>)

Steps to reproduce

<?php

class Validator {

	private array $rules;

	/**
	 * @return array
	 */
	public function getRules() {
		return $this->rules;
	}
}

function x(Validator $validator) {
	foreach ($validator->getRules() as $field => $rules)
	{
		foreach ($rules as $rule)
		{
			$rule = explode(':', $rule);
		}
	}
}

Error output or panic trace


.phpantom.toml

default

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions