This class seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate
the same code in three or more different places, we strongly encourage you to
look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
25
{
26
private $manager;
27
28
public function __construct(EntityManagerInterface $manager)
It seems like you code against a concrete implementation and not the interface Kreta\SharedKernel\Domain\Model\DomainEvent as the method userId() does only exist in the following implementations of said interface: Kreta\Notifier\Domain\Mo...ox\UserReadNotification, Kreta\Notifier\Domain\Mo...serReceivedNotification, Kreta\Notifier\Domain\Model\Inbox\UserSignedUp, Kreta\Notifier\Domain\Mo...\UserUnreadNotification, Kreta\TaskManager\Domain...OrganizationMemberAdded, Kreta\TaskManager\Domain...ganizationMemberRemoved, Kreta\TaskManager\Domain...Organization\OwnerAdded, Kreta\TaskManager\Domain...ganization\OwnerRemoved.
Let’s take a look at an example:
interfaceUser{/** @return string */publicfunctiongetPassword();}classMyUserimplementsUser{publicfunctiongetPassword(){// return something}publicfunctiongetDisplayName(){// return some name.}}classAuthSystem{publicfunctionauthenticate(User$user){$this->logger->info(sprintf('Authenticating %s.',$user->getDisplayName()));// do something.}}
In the above example, the authenticate() method works fine as long as you just pass
instances of MyUser. However, if you now also want to pass a different implementation
of User which does not have a getDisplayName() method, the code will break.
classAuthSystem{publicfunctionauthenticate(User$user){if($userinstanceofMyUser){$this->logger->info(/** ... */);}// or alternativelyif(!$userinstanceofMyUser){thrownew\LogicException('$user must be an instance of MyUser, '.'other instances are not supported.');}}}
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
It seems like $user defined by $this->manager->getRepos...>find($event->userId()) on line 40 can also be of type null; however, Doctrine\Common\Persiste...bjectManager::persist() does only seem to accept object, maybe add an additional type check?
If a method or function can return multiple different values and unless you are
sure that you only can receive a single value in this context, we recommend
to add an additional type check:
/** * @return array|string */functionreturnsDifferentValues($x){if($x){return'foo';}returnarray();}$x=returnsDifferentValues($y);if(is_array($x)){// $x is an array.}
If this a common case that PHP Analyzer should handle natively, please let us
know by opening an issue.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.