Passed
Push — master ( 239fe0...528f7d )
by Kacper
05:10
created

XmppStream::handleFeatures()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 3
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 14
rs 9.2
c 6
b 0
f 3
1
<?php
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp;
17
18
use Kadet\Xmpp\Exception\Protocol\TlsException;
19
use Kadet\Xmpp\Network\SecureStream;
20
use Kadet\Xmpp\Stream\Error;
21
use Kadet\Xmpp\Stream\Features;
22
use Kadet\Xmpp\Xml\XmlElement;
23
use Kadet\Xmpp\Xml\XmlParser;
24
use Kadet\Xmpp\Xml\XmlStream;
25
use React\Stream\DuplexStreamInterface;
26
use Kadet\Xmpp\Utils\filter as with;
27
28
class XmppStream extends XmlStream
29
{
30
    const TLS_NAMESPACE = 'urn:ietf:params:xml:ns:xmpp-tls';
31
32
    private $_attributes = [];
33
    private $_lang;
34
35
    public function __construct(XmlParser $parser, DuplexStreamInterface $transport, string $lang = 'en')
36
    {
37
        parent::__construct($parser, $transport);
38
39
        $this->parser->factory->register(Features::class, self::NAMESPACE_URI, 'features');
40
        $this->parser->factory->register(Error::class,    self::NAMESPACE_URI, 'error');
41
42
        $this->_lang = $lang;
43
44
        $this->on('element', function (Features $element) {
45
            $this->handleFeatures($element);
46
        }, Features::class);
47
48
        $this->on('element', function (XmlElement $element) {
49
            $this->handleTls($element);
50
        }, with\xmlns(self::TLS_NAMESPACE));
51
    }
52
53
    public function start(array $attributes = [])
54
    {
55
        $this->_attributes = $attributes;
56
57
        parent::start(array_merge([
58
            'xmlns'    => 'jabber:client',
59
            'version'  => '1.0',
60
            'xml:lang' => $this->_lang
61
        ], $attributes));
62
    }
63
64
    public function restart()
65
    {
66
        $this->start($this->_attributes);
67
    }
68
69
    private function handleFeatures(Features $element)
70
    {
71
        if ($element->startTls >= Features::TLS_AVAILABLE) {
72
            if ($this->decorated instanceof SecureStream) {
73
                $this->write(XmlElement::create('starttls', null, self::TLS_NAMESPACE));
74
75
                return; // Stop processing
76
            } elseif ($element->startTls === Features::TLS_REQUIRED) {
77
                throw new TlsException('Encryption is not available, but server requires it.');
78
            } else {
79
                $this->getLogger()->warning('Server offers TLS encryption, but stream is not capable of it.');
80
            }
81
        }
82
    }
83
84
    private function handleTls(XmlElement $response)
85
    {
86
        if ($response->localName === 'proceed') {
87
            // this function is called only by event, which can be only fired after instanceof check
88
            /** @noinspection PhpUndefinedMethodInspection */
89
            $this->decorated->encrypt(STREAM_CRYPTO_METHOD_TLS_CLIENT);
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface React\Stream\DuplexStreamInterface as the method encrypt() does only exist in the following implementations of said interface: Kadet\Xmpp\Network\TcpStream.

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...
90
            $this->restart();
91
        } else {
92
            throw new TlsException('TLS negotiation failed.'); // XMPP does not provide any useful information why it happened
93
        }
94
    }
95
}
96