Completed
Push — master ( b0bc39...c39dfb )
by Emily
10s
created

GenericContext   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 3
dl 0
loc 30
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getGenericType() 0 7 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\Generic;
16
17
use Spaark\CompositeUtils\Model\Reflection\Type\AbstractType;
18
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType;
19
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite;
20
use Spaark\CompositeUtils\Traits\AllReadableTrait;
21
use Spaark\CompositeUtils\Traits\AutoConstructTrait;
22
23
/**
24
 */
25
class GenericContext
26
{
27
    use AutoConstructTrait;
28
29
    /**
30
     * @var ObjectType
31
     * @construct required
32
     */
33
    protected $object;
34
35
    /**
36
     * @var ReflectionComposite
37
     * @construct required
38
     */
39
    protected $reflector;
40
41
    /**
42
     * Get the type for the given generic name
43
     *
44
     * @param string $name
45
     * @return AbstractType
46
     */
47 2
    public function getGenericType(string $name) : AbstractType
48
    {
49 2
        return $this->object->generics
0 ignored issues
show
Documentation introduced by
The property $generics is declared protected in Spaark\CompositeUtils\Mo...lection\Type\ObjectType. 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...
50
        [
51 2
            $this->reflector->generics->indexOfKey($name)
0 ignored issues
show
Documentation introduced by
The property $generics is declared protected in Spaark\CompositeUtils\Mo...ion\ReflectionComposite. 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...
52
        ];
53
    }
54
}
55