gwCoreExceptionInfo   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 2 Features 2
Metric Value
wmc 13
c 3
b 2
f 2
lcom 1
cbo 0
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A setException() 0 4 1
A __toString() 0 4 1
A fetch() 0 4 1
A fetchHTML() 0 4 1
B getAsString() 0 20 7
1
<?php
2
3
namespace Gwa\Exception;
4
5
/**
6
 * The core exception info class to be extended by all other exception info classes.
7
 * The basic info is simply a string.
8
 */
9
class gwCoreExceptionInfo implements gwiExceptionInfo
10
{
11
    /**
12
     * @var gwCoreException
13
     */
14
    protected $exception;
15
16
    /**
17
     * @var string
18
     */
19
    protected $info;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param gwCoreException $info
0 ignored issues
show
Documentation introduced by
Should the type for parameter $info not be gwCoreException|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
25
     * @param string          $info
26
     */
27
    public function __construct($info = null)
28
    {
29
        $this->info = $info === null ? '' : $info;
0 ignored issues
show
Documentation Bug introduced by
It seems like $info === null ? '' : $info can also be of type object<Gwa\Exception\gwCoreException>. However, the property $info is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
30
    }
31
32
    /**
33
     * Exception setter.
34
     *
35
     * @param gwCoreException $exception
36
     */
37
    final public function setException(gwCoreException $exception)
38
    {
39
        $this->exception = $exception;
40
    }
41
42
    /**
43
     * Returns a string representation of this object.
44
     *
45
     * @return string
46
     */
47
    public function __toString()
48
    {
49
        return $this->fetch();
50
    }
51
52
    /**
53
     * Returns a plain text representation of the info contained in this object.
54
     *
55
     * @return string
56
     */
57
    public function fetch()
58
    {
59
        return $this->info;
60
    }
61
62
    /**
63
     * Returns a HTML representation of the info contained in this object.
64
     *
65
     * @return string
66
     */
67
    public function fetchHTML()
68
    {
69
        return $this->fetch();
70
    }
71
72
    /**
73
     * Returns a string representation of a value.
74
     * Used by subclasses.
75
     *
76
     * @link http://www.php.net/manual/en/function.get-resource-type.php
77
     *
78
     * @param mixed $value
79
     *
80
     * @return string
81
     */
82
    public function getAsString($value)
83
    {
84
        if (is_string($value)) {
85
            return $value;
86
        }
87
        if (is_bool($value)) {
88
            return $value ? 'TRUE' : 'FALSE';
89
        }
90
        if (is_null($value)) {
91
            return 'NULL';
92
        }
93
        if (is_resource($value)) {
94
            return get_resource_type($value);
95
        }
96
        if (is_object($value)) {
97
            return '{'.get_class($value).'}';
98
        }
99
100
        return (string) $value;
101
    }
102
}
103