2

I am trying to get type hinting to work with a custom User class in the Symfony 5.4 Security bundle and PHP 7.4. So I have this User class:

// src/Security/User.php
<?php

namespace App\Security;

class User implements JWTUserInterface
{
    private int $customProp
    
    public function getCustomProp() {
        return $this->customProp;
    }
    ...
}

But whenever I try to access the user in some service I do not get type hints for the customProp

// src/Service/MyService.php
<?php

namespace App\Service;

use Symfony\Component\Security\Core\Security;

class MyService
{
    private int $customProp;

    public function __construct(Security $security) {
        $this->myCustomProp = $security->getUser()->getCustomProp();
    }                                               ~~~~~~~~~~~~~

}

The message I get is

Undefined method 'getCustomProp'.

But the value is there and the code works, I simply can not get the IDE to understand that my custom User class offers this method.

2
  • What is your IDE ? Commented Apr 15, 2024 at 15:22
  • VSCode with PHP Intelephense Commented Apr 16, 2024 at 10:03

1 Answer 1

1

You're getting this issue because Security::getUser() is a native Symfony function that returns the native Symfony UserInterface, and that UserInterface does not define getCustomProp(). The IDE does not know that the object being returned is an instance of your custom User class. You could explicitly tell it so like this:

public function __construct(Security $security) {
    /** @var \App\Security\User $user */
    $user = $security->getUser();
    $this->myCustomProp = $user->getCustomProp();
}

Alternatively, instead of initializing a copy of the variable in the constructor, you might make a method with a typehinted return:

class MyService {
    protected Security $security;

    public function __construct(Security $security) {
        $this->security = $security;
    }

    protected function getUser(): \App\Security\User {
        return $this->security->getUser();
    }

    public function doSomething() {
        // Now your IDE knows that getUser() is an instance of your custom class.
        $this->getUser()->getCustomProp();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.