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

HasGenericContextTrait::getGenericType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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\Model\Generic\GenericContext;
18
use Spaark\CompositeUtils\Model\Reflection\Type\AbstractType;
19
use Spaark\CompositeUtils\Model\Reflection\Type\GenericType;
20
use Spaark\CompositeUtils\Exception\MissingContextException;
21
22
/**
23
 * Do something
24
 */
25
trait HasGenericContextTrait
26
{
27
    /**
28
     * @var GenericContext
29
     */
30
    protected $context;
31
32 46
    public function __construct(GenericContext $context = null)
33
    {
34 46
        $this->context = $context;
35 46
    }
36
37 8
    protected function getGenericType(GenericType $generic)
38
        : AbstractType
39
    {
40 8
        if (!$this->context)
41
        {
42 1
            throw new MissingContextException();
43
        }
44
45 7
        return $this->context->getGenericType($generic->name);
0 ignored issues
show
Documentation introduced by
The property $name is declared protected in Spaark\CompositeUtils\Mo...ection\Type\GenericType. 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...
46
    }
47
}
48