Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require-extends implications on the interface #10538

Open
staabm opened this issue Jan 11, 2024 · 1 comment
Open

require-extends implications on the interface #10538

staabm opened this issue Jan 11, 2024 · 1 comment

Comments

@staabm
Copy link
Contributor

staabm commented Jan 11, 2024

recently I worked on adding psalm-require-extends semantics into phpstan.

in phpstan we allowed to access public properties and public methods on the interface type as an implication of using psalm-require-extends. It seems this is something missing in psalm

this issue is meant to discuss whether we could achieve feature parity in this regard.

see https://psalm.dev/r/8aefff36a3 vs https://phpstan.org/r/707a92dc-d8e7-4749-9eb4-06f796e5e432

you might be interessted more test-cases which have been added in the following PRs:

Copy link

I found these snippets:

https://psalm.dev/r/8aefff36a3
<?php

namespace Bug10302InterfaceExtends;

use function PHPStan\Testing\assertType;

/**
 * @psalm-require-extends SomeClass
 */
interface SampleInterface
{
}

class SomeClass {
	public int $x;
	protected string $y;
	private array $z = [];

	public function doFoo():int
	{
		return 1;
	}
}

function test(SampleInterface $test): void
{
	$test->x;
	$test->y;
	$test->z;

	$test->doFoo();
}

function testExtendedInterface(AnotherInterface $test): void
{
	$test->x;
	$test->y;
	$test->z;

	$test->doFoo();
}

interface AnotherInterface extends SampleInterface
{
}

class SomeSubClass extends SomeClass {}

class ValidClass extends SomeClass implements SampleInterface {}

class ValidSubClass extends SomeSubClass implements SampleInterface {}

class InvalidClass implements SampleInterface {}
Psalm output (using commit a75d26a):

ERROR: NoInterfaceProperties - 27:2 - Interfaces cannot have properties

ERROR: NoInterfaceProperties - 28:2 - Interfaces cannot have properties

ERROR: NoInterfaceProperties - 29:2 - Interfaces cannot have properties

ERROR: UndefinedInterfaceMethod - 31:9 - Method Bug10302InterfaceExtends\SampleInterface::doFoo does not exist

ERROR: NoInterfaceProperties - 36:2 - Interfaces cannot have properties

ERROR: NoInterfaceProperties - 37:2 - Interfaces cannot have properties

ERROR: NoInterfaceProperties - 38:2 - Interfaces cannot have properties

ERROR: UndefinedInterfaceMethod - 40:9 - Method Bug10302InterfaceExtends\AnotherInterface::doFoo does not exist

ERROR: MissingConstructor - 15:13 - Bug10302InterfaceExtends\SomeClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$x, but no constructor

ERROR: MissingConstructor - 16:19 - Bug10302InterfaceExtends\SomeClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$y, but no constructor

ERROR: MissingConstructor - 15:13 - Bug10302InterfaceExtends\SomeSubClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$x, but no constructor

ERROR: MissingConstructor - 16:19 - Bug10302InterfaceExtends\SomeSubClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$y, but no constructor

ERROR: MissingConstructor - 15:13 - Bug10302InterfaceExtends\ValidClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$x, but no constructor

ERROR: MissingConstructor - 16:19 - Bug10302InterfaceExtends\ValidClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$y, but no constructor

ERROR: MissingConstructor - 15:13 - Bug10302InterfaceExtends\ValidSubClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$x, but no constructor

ERROR: MissingConstructor - 16:19 - Bug10302InterfaceExtends\ValidSubClass has an uninitialized property Bug10302InterfaceExtends\SomeClass::$y, but no constructor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant