I've found a very odd Symfony behavior and I got a feeling, that I must share with it. Let's look at this example:
- Code: Select all
$user = $this->get('security.context')->getToken()->getUser();
Everything looks ok, BUT.. it has at least three diffrent cases:
1. The method returns User Entity Object when user authenticated with role - fine
2. The method returns NULL if the user is not authenticated (not logged, not anonymous) - fine
3. The method returns A STRING "anon.", when user is authenticated annonymously, but not logged - WTF?!
How can one method return an object, a string or NULL depending on user authentication? I believe this is very bad solution, it's confusing and it's againt good practicies and principles.
I believe the method getUser() should always return NULL or Object only, never a string. In the 3. case it could return for e.g. Annonymous User Object or something like that, but not a string!