Completed
Push — master ( 5eff09...92586b )
by Kacper
03:19
created

TlsEnabler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 44
ccs 21
cts 21
cp 1
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setClient() 0 12 1
A startTls() 0 16 4
A handleTls() 0 11 2
1
<?php
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2016, Some rights 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\Module;
17
18
use Kadet\Xmpp\Exception\Protocol\TlsException;
19
use Kadet\Xmpp\Network\SecureStream;
20
use Kadet\Xmpp\Stream\Features;
21
use Kadet\Xmpp\Utils\filter as with;
22
use Kadet\Xmpp\Xml\XmlElement;
23
use Kadet\Xmpp\XmppClient;
24
25
class TlsEnabler extends ClientModule
26
{
27 4
    public function setClient(XmppClient $client)
28
    {
29 4
        parent::setClient($client);
30
31
        $client->on('features', function (Features $features) {
32
            return !$this->startTls($features);
33 4
        }, null, 10);
34
35 4
        $client->on('element', function (XmlElement $element) {
36 2
            $this->handleTls($element);
37 4
        }, with\xmlns(Features\StartTls::XMLNS));
38 4
    }
39
40 4
    public function startTls(Features $features)
41
    {
42 4
        if ($features->startTls) {
43 4
            if ($this->_client->getDecorated() instanceof SecureStream) {
44 2
                $this->_client->write(new Features\StartTls());
45
46 2
                return true; // Stop processing
47 2
            } elseif ($features->startTls->required) {
48 1
                throw new TlsException('Encryption is not available, but server requires it.');
49
            } else {
50 1
                $this->_client->getLogger()->warning('Server offers TLS encryption, but stream is not capable of it.');
51
            }
52
        }
53
54 1
        return false;
55
    }
56
57 2
    private function handleTls(XmlElement $response)
58
    {
59 2
        if ($response->localName === 'proceed') {
60
            // this function is called only by event, which can be only fired after instanceof check
61
            /** @noinspection PhpUndefinedMethodInspection */
62 2
            $this->_client->getDecorated()->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...
63 2
            $this->_client->restart();
64
        } else {
65
            throw new TlsException('TLS negotiation failed.'); // XMPP does not provide any useful information why it happened
66
        }
67 2
    }
68
}
69