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

Psalm error for referenceRepository #424

Closed
BigMichi1 opened this issue Jan 30, 2023 · 4 comments · Fixed by #425
Closed

Psalm error for referenceRepository #424

BigMichi1 opened this issue Jan 30, 2023 · 4 comments · Fixed by #425

Comments

@BigMichi1
Copy link

i followed the example given here to inject a service into the fixture.

now when i'm running psalm it prints out a warning that referenceRepository is not set in the constructor

ERROR: PropertyNotSetInConstructor - src/DataFixtures/UserFixtures.php:12:7 - Property App\DataFixtures\UserFixtures::$referenceRepository is not defined in constructor of App\DataFixtures\UserFixtures or in any methods called in the constructor (see https://psalm.dev/074)
class UserFixtures extends Fixture

my assumption is that it is because in AbstractFixture the type hint for referenceRepository only mentions ReferenceRepository but not null which could theoretically happen

@greg0ire
Copy link
Member

You should show us the code of UserFixtures.php. There is no occurrence of referenceRepository in the page you linked.

@BigMichi1
Copy link
Author

<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class UserFixtures extends Fixture
{
    public const USER_ADMIN = 'user-admin';

    public const USER_MEMBER = 'user-member';

    public function __construct(
        private readonly UserPasswordHasherInterface $encoder
    ) {
    }

    public function load(ObjectManager $manager): void
    {
        $userAdmin = new User();
        $userAdmin->setEmail('admin@test.de');
        $userAdmin->setRoles(['ROLE_SUPER_ADMIN']);
        $userAdmin->setLocale('de');
        $userAdmin->setActive(true);
        $userAdmin->setFullName('Test Admin User');
        $userAdmin->setCreatedBy('fixture');
        $userAdmin->setUpdatedBy('fixture');
        $userAdmin->setPassword($this->encoder->hashPassword($userAdmin, '12345'));
        $manager->persist($userAdmin);

        $userMemeber = new User();
        $userMemeber->setEmail('member@test.de');
        $userMemeber->setRoles([]);
        $userMemeber->setLocale('de');
        $userMemeber->setActive(true);
        $userMemeber->setFullName('Test Member User');
        $userMemeber->setCreatedBy('fixture');
        $userMemeber->setUpdatedBy('fixture');
        $userMemeber->setPassword($this->encoder->hashPassword($userAdmin, '67890'));
        $manager->persist($userMemeber);

        $manager->flush();

        $this->addReference(self::USER_ADMIN, $userAdmin);
        $this->addReference(self::USER_MEMBER, $userMemeber);
    }
}

@stof
Copy link
Member

stof commented Jan 30, 2023

The AbstractFixture of class doctrine/data-fixture indeed does not set the referenceRepository property in the constructor. However, it does set it through the setter before usage.

The issue here is that Psalm rejects valid PHP code in order to avoid having to model the uninitialized state of PHP properties (PHP requires a property to be initialized at the time you use it, not at the end of the constructor)

@greg0ire greg0ire transferred this issue from doctrine/DoctrineFixturesBundle Jan 30, 2023
@greg0ire
Copy link
Member

I think this should fix it: #425

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

Successfully merging a pull request may close this issue.

3 participants