CommandPipe   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 1
c 4
b 3
f 0
lcom 0
cbo 1
dl 0
loc 16
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A replyMessage() 0 5 1
1
<?php
2
3
namespace PEIP\Pipe;
4
5
/*
6
 * This file is part of the PEIP package.
7
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
/**
14
 * CommandPipe
15
 * Class to call commands on an arbitrary pipe by passing messages from the input
16
 * to the 'command' method of the commanded pipe.
17
 *
18
 * @author Timo Michna <timomichna/yahoo.de>
19
 * @extends \PEIP\Pipe\EventPipe
20
 * @implements \PEIP\INF\Event\Listener, \PEIP\INF\Event\Connectable, \PEIP\INF\Channel\SubscribableChannel, \PEIP\INF\Channel\Channel, \PEIP\INF\Handler\Handler, \PEIP\INF\Message\MessageBuilder
21
 */
22
class CommandPipe extends \PEIP\Pipe\EventPipe
23
{
24
    /**
25
     * Does the reply logic. Calls the 'command' method of the registered output-channel
26
     * with request-message as argument.
27
     *
28
     * @param $content
29
     *
30
     * @return
31
     */
32
    protected function replyMessage($content)
33
    {
34
        $message = $this->ensureMessage($content);
35
        $this->getOutputChannel()->command($message);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PEIP\INF\Channel\Channel as the method command() does only exist in the following implementations of said interface: DrinkAggregator, DrinkRouter, OrderSplitter, PEIP\ABS\Pipe\EventPipe, PEIP\ABS\Router\Router, PEIP\ABS\Service\ServiceActivator, PEIP\ABS\Splitter\MessageSplitter, PEIP\ABS\Transformer\ContentTransformer, PEIP\ABS\Transformer\Transformer, PEIP\Listener\Wiretap, PEIP\Pipe\CommandPipe, PEIP\Pipe\EventPipe, PEIP\Pipe\FixedEventPipe, PEIP\Pipe\Pipe, PEIP\Pipe\SimpleEventPipe, PEIP\Service\HeaderServiceActivator, PEIP\Service\ServiceActivator, PEIP\Service\SplittingServiceActivator, PEIP\Service\StringServiceActivator.

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...
36
    }
37
}
38