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

GenericTrait::setGenericContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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