Completed
Pull Request — master (#8)
by Emily
02:13
created

GenericTrait::getObjectType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 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\Traits;
16
17
use Spaark\CompositeUtils\Factory\Reflection\TypeParser;
18
use Spaark\CompositeUtils\Model\Generic\GenericContext;
19
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType;
20
21
/**
22
 * Classes using this trait can have generic properties
23
 *
24
 * All generated generics will automatically use this Generic, but you
25
 * may also use this in the root class itself as it provides helpers
26
 * for creating generic instances
27
 */
28
trait GenericTrait
29
{
30
    use HasReflectorTrait;
31
32
    /**
33
     * @var GenericContext
34
     */
35
    protected $genericContext;
36
37
    /**
38
     * @var ObjectType
39
     */
40
    protected $objectType;
41
42
    /**
43
     * Gets the object descriptor for this class
44
     *
45
     * @return ObjectType
46
     */
47 2
    public function getObjectType() : ObjectType
48
    {
49 2
        if (!$this->objectType)
50
        {
51 2
            $this->objectType =
52 2
                (new TypeParser())->parseFromType($this);
53
        }
54
55 2
        return $this->objectType;
56
    }
57
58
    /**
59
     * Gets the generic context for this class or creates one if it does
60
     * not exist
61
     *
62
     * @return GenericContext
63
     */
64
    public function getGenericContext() : GenericContext
65
    {
66
        if (!$this->genericContext)
67
        {
68
            $this->genericContext = new GenericContext
69
            (
70
                $this->getObjectType(),
71
                static::getReflectionComposite()
72
            );
73
        }
74
75
        return $this->genericContext;
76
    }
77
78
    /**
79
     * Sets the generic context for this class if one does not already
80
     * exist
81
     *
82
     * @param GenericContext $genericContext
83
     */
84
    public function setGenericContext(GenericContext $genericContext)
85
        : void
86
    {
87
        if ($this->genericContext)
88
        {
89
            throw new ImmutablePropertyException
90
            (
91
                (string)$this->getObjectType(),
92
                'genericContext'
93
            );
94
        }
95
96
        $this->genericContext = $genericContext;
97
        $this->objectType = $genericContext->object;
0 ignored issues
show
Documentation introduced by
The property $object is declared protected in Spaark\CompositeUtils\Model\Generic\GenericContext. 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...
98
    }
99
100
    /**
101
     * Gets an instance of a generic
102
     *
103
     * @param string $name
104
     * @return mixed
105
     */
106
    protected function getInstanceOfGeneric(string $name)
107
    {
108
        $class =
109
            (string)$this->getGenericContext()->getGenericType($name);
110
111
        return new $class();
112
    }
113
}
114