Completed
Pull Request — master (#7)
by Emily
02:15
created

ObjectType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 63
ccs 7
cts 8
cp 0.875
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 4 1
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 33
    public function __construct($classname)
50
    {
51 33
        $this->generics = new ArrayList();
52 33
        $this->classname = $classname instanceof ClassName
53
            ? $classname
54 33
            : new ClassName($classname);
55 33
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function equals($type) : bool
61
    {
62
        if
63
        (
64
            $type instanceof ObjectType &&
65
            $type->classname === $this->classname &&
66
            $type->generics->size() === $this->generics->size()
67
        )
68
        {
69
            foreach ($type->generics as $i => $generic)
70
            {
71
                if (!$this->generics[$i]->compatible($generic))
72
                {
73
                    return false;
74
                }
75
            }
76
77
            return true;
78
        }
79
80
        return false;
81
    }
82
83 1
    public function __toString() : string
84
    {
85 1
        return $this->classname->classname;
0 ignored issues
show
Documentation introduced by
The property $classname is declared protected in Spaark\CompositeUtils\Model\ClassName. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
86
    }
87
}
88