Completed
Push — master ( 351bd6...f43dc3 )
by Hugues
02:12
created

handleCommandAndAssertTraced()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 12
nc 2
nop 3
1
<?php
2
3
namespace HMLB\UserBundle\Tests\Command;
4
5
use HMLB\DDD\Entity\Identity;
6
use HMLB\DDD\Persistence\PersistentMessage;
7
use HMLB\DDDBundle\Repository\PersistentCommandRepository;
8
use HMLB\UserBundle\Message\Trace\Context;
9
use HMLB\UserBundle\Message\Trace\Trace;
10
use HMLB\UserBundle\Message\TraceableCommand;
11
use SimpleBus\Message\Bus\MessageBus;
12
13
/**
14
 * Trait TraceableCommandTestingCapabilities.
15
 *
16
 * @author Hugues Maignol <[email protected]>
17
 */
18
trait TraceableCommandTestingCapabilities
19
{
20
    protected function handleCommandAndAssertTraced(
21
        MessageBus $commandBus,
22
        PersistentMessage $command,
23
        PersistentCommandRepository $commandRepository
24
    ) {
25
        $commandBus->handle($command);
26
27
        $this->assertInstanceOf(Identity::class, $command->getId());
0 ignored issues
show
Bug introduced by
It seems like assertInstanceOf() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
29
        $foundCommand = $commandRepository->get($command->getId());
30
        $this->assertSame($command, $foundCommand);
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
31
32
        if (!$command instanceof TraceableCommand) {
33
            $this->fail(sprintf('The message %s is not Traceable', get_class($command)));
0 ignored issues
show
Bug introduced by
It seems like fail() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34
        }
35
        $this->assertInstanceOf(Trace::class, $trace = $command->getTrace());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface HMLB\DDD\Persistence\PersistentMessage as the method getTrace() does only exist in the following implementations of said interface: HMLB\UserBundle\Command\ChangeEmail, HMLB\UserBundle\Command\ChangePassword, HMLB\UserBundle\Command\RegisterUser, HMLB\UserBundle\Command\UserCommand, HMLB\UserBundle\Event\EmailChanged, HMLB\UserBundle\Event\PasswordChanged, HMLB\UserBundle\Event\UserDisabled, HMLB\UserBundle\Event\UserEnabled, HMLB\UserBundle\Event\UserEvent, HMLB\UserBundle\Event\UserRegistered.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(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.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \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.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
Bug introduced by
It seems like assertInstanceOf() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
        $this->assertInstanceOf(Context::class, $trace->getContext());
0 ignored issues
show
Bug introduced by
It seems like assertInstanceOf() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
    }
38
}
39