Skip to content

refactor: fix proxy system and introduce psalm extension#704

Merged
nikophil merged 2 commits into2.xfrom
refactor/fix-proxy-system
Oct 22, 2024
Merged

refactor: fix proxy system and introduce psalm extension#704
nikophil merged 2 commits into2.xfrom
refactor/fix-proxy-system

Conversation

@nikophil
Copy link
Member

@nikophil nikophil commented Oct 18, 2024

fixes #701 #696

I've decided to completely remove all methods in PersistenceProxyObjectFactory, so we do not override @final methods anymore. We still need to figure out a solution for PersistenceObjectFactory::create(), I'll do this in a further PR.

Psalm does not understand the following:

/**
 * @extends PersistentProxyObjectFactory<User>
 */
class UserFactory extends PersistentProxyObjectFactory{}

/**
 * @template T of object
 * @extends PersistentObjectFactory<T&Proxy<T>>
 */
abstract class PersistentProxyObjectFactory extends PersistentObjectFactory{}

/**
 * @template T of object
 */
abstract class PersistentObjectFactory{}

it always consider the template to be User and not User&Proxy<User>. I think because we're breaking covariance (T&Proxy<T> is wider than T)

I've finally decided to introduce a psalm extension, we will be able to fix nearly every typing problem in here.

@nikophil nikophil force-pushed the refactor/fix-proxy-system branch 3 times, most recently from e3a9d51 to cff5ae5 Compare October 19, 2024 15:28
@nikophil nikophil changed the title refactor: fix proxy system refactor: fix proxy system and introduce psalm extension Oct 19, 2024
Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I'm unfamiliar with psalm plugins, will this be auto-registered in projects using foundry and psalm?

@nikophil
Copy link
Member Author

I'm unfamiliar with psalm plugins, will this be auto-registered in projects using foundry and psalm?

yes, thanks to the change in composer.json

Copy link

@deluxetom deluxetom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great solution, thank you!

@nikophil nikophil force-pushed the refactor/fix-proxy-system branch from 7c764ab to 1576c3b Compare October 22, 2024 16:01
@nikophil nikophil force-pushed the refactor/fix-proxy-system branch from 828aa05 to c00bfaf Compare October 22, 2024 16:49
abstract class Factory
{
/** @var Attributes[] */
/** @phpstan-var Attributes[] */
Copy link
Member Author

@nikophil nikophil Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everytime we use phpstan pseudo types, we should use @phpstan- annotations because Psalm seems to not understand it

and moreover, see this comment from stof here, I think it is relevant

I'll make another PR where I fix all of the related docblocks


$parameters = $this->normalizeParameters($parameters);
$instantiator = $this->instantiator ?? Configuration::instance()->instantiator;
/** @var T $object */
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like PHPStorm does not only interprets methods signature (like PHPStan would do) but it events guesses stuff from the methods' body. I had really hard times to find but it basically fixes 90% of auto-complete problems 🎉

public static function first(string $sortBy = 'id'): object
{
return static::repository()->firstOrFail($sortBy);
/** @var T $object */
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, this is here to help auto-complete

@nikophil nikophil merged commit 0d570cc into 2.x Oct 22, 2024
@nikophil nikophil deleted the refactor/fix-proxy-system branch October 22, 2024 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Psalm errors with Attributes phpstan-type declaration

3 participants