Completed
Push — master ( 499778...79f24a )
by Emily
9s
created

ObjectType   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 76
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
B equals() 0 22 6
A __toString() 0 12 2
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Reflection\Type;
16
17
use Spaark\CompositeUtils\Model\Collection\ArrayList;
18
use Spaark\CompositeUtils\Model\ClassName;
19
20
/**
21
 * Represents a data type which must be an instance of an object
22
 *
23
 * @property-read string $classname
24
 */
25
class ObjectType extends AbstractType
26
{
27
    /**
28
     * The name of the class this must be an instance of
29
     *
30
     * @readable
31
     * @var ClassName
32
     */
33
    protected $classname;
34
35
    /**
36
     * Generic types for this object
37
     *
38
     * @readable
39
     * @var ArrayList
40
     */
41
    protected $generics;
42
43
    /**
44
     * Creates this ObjectType with the given classname
45
     *
46
     * @param mixed $class The name of the class this must be an
0 ignored issues
show
Bug introduced by
There is no parameter named $class. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
47
     *     instance of
48
     */
49 39
    public function __construct($classname)
50
    {
51 39
        $this->generics = new ArrayList();
52 39
        $this->classname = $classname instanceof ClassName
53
            ? $classname
54 39
            : new ClassName($classname);
55 39
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 2
    public function equals($type) : bool
61
    {
62
        if
63
        (
64 2
            $type instanceof ObjectType &&
65 2
            $type->classname->equals($this->classname) &&
0 ignored issues
show
Documentation introduced by
$this->classname is of type object<Spaark\CompositeUtils\Model\ClassName>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66 2
            $type->generics->size() === $this->generics->size()
67
        )
68
        {
69 2
            foreach ($type->generics as $i => $generic)
70
            {
71 2
                if (!$this->generics[$i]->equals($generic))
72
                {
73
                    return false;
74
                }
75
            }
76
77 2
            return true;
78
        }
79
80 1
        return false;
81
    }
82
83
    /**
84
     * Returns a string representation of the object
85
     *
86
     * @return string
87
     */
88 2
    public function __toString() : string
89
    {
90 2
        $return = (string)$this->classname;
91
92 2
        if ($this->generics->count())
93
        {
94
            $return .=
95 1
                '<' . implode(',', $this->generics->toArray()) . '>';
96
        }
97
98 2
        return $return;
99
    }
100
}
101