Completed
Push — master ( 5f3a6c...fcd09b )
by Kacper
05:50
created

BindingException::fromError()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 3
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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\Exception\Protocol;
17
18
19
use Exception;
20
use Kadet\Xmpp\Jid;
21
use Kadet\Xmpp\Stanza\Error;
22
23
class BindingException extends StanzaException
24
{
25
    /**
26
     * Construct the exception. Note: The message is NOT binary safe.
27
     *
28
     * @param Jid       $jid
29
     * @param Error     $error
30
     * @param Exception $previous [optional] The previous exception used for the exception chaining. Since 5.3.0
31
     * @return static
32
     * @since    5.1.0
33
     */
34
    public static function fromError(Jid $jid, Error $error, Exception $previous = null)
35
    {
36
        return new static($error, \Kadet\Xmpp\Utils\helper\format("Cannot bind {resource} for {bare}. {condition}", [
37
            'resource'  => $jid->resource ?: "no resource",
38
            'bare'      => (string)$jid->bare(),
39
            'condition' => static::_conditionDescription($error)
0 ignored issues
show
Bug introduced by
Since _conditionDescription() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of _conditionDescription() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
40
        ]), 0, $previous);
41
    }
42
43
    private static function _conditionDescription(Error $error)
44
    {
45
        if(!empty($error->text)) {
46
            return $error->text;
47
        }
48
49
        return [
50
            'bad-request' => 'Bad Request: "{resource}" is not valid XMPP resource identifier.',
51
            'conflict'    => 'Conflict: {bare}/{resource} is already in use.'
52
        ][$error->condition] ?? 'Unknown reason: '.$error->condition;
53
    }
54
}
55