FixedEventPipe::setInputChannel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 4 Features 0
Metric Value
c 5
b 4
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 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
 * FixedEventPipe.
15
 *
16
 * @author Timo Michna <timomichna/yahoo.de>
17
 * @extends \PEIP\Pipe\EventPipe
18
 * @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
19
 */
20
class FixedEventPipe extends \PEIP\Pipe\EventPipe
21
{
22
    /**
23
     * @param $inputChannel
24
     *
25
     * @return
26
     */
27
    public function setInputChannel(\PEIP\INF\Channel\Channel $inputChannel)
28
    {
29
        if (isset($this->eventName)) {
30
            $this->connectChannel($inputChannel);
31
        } else {
32
            $this->inputChannel = $inputChannel;
33
        }
34
    }
35
36
    /**
37
     * @param string $eventName
38
     *
39
     * @return
40
     */
41
    public function setEventName($eventName)
42
    {
43
        if (!isset($this->eventName)) {
44
            $this->eventName = $eventName;
45
            if ($this->inputChannel) {
46
                $this->inputChannel->connect($this->eventName, $this);
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 connect() does only exist in the following implementations of said interface: DrinkAggregator, DrinkRouter, OrderSplitter, PEIP\ABS\Channel\Channel, PEIP\ABS\Channel\PollableChannel, PEIP\ABS\Channel\SubscribableChannel, 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\Channel\DirectChannel, PEIP\Channel\PollableChannel, PEIP\Channel\PriorityChannel, PEIP\Channel\PublishSubscribeChannel, PEIP\Channel\QueueChannel, 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...
47
            }
48
        }
49
    }
50
}
51