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

GenericNameProvider::inferObjectName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 9
cts 9
cp 1
rs 9.3142
cc 3
eloc 10
nc 3
nop 1
crap 3
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\Service;
16
17
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType;
18
use Spaark\CompositeUtils\Model\Reflection\Type\AbstractType;
19
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType;
20
use Spaark\CompositeUtils\Model\Reflection\Type\FloatType;
21
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType;
22
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType;
23
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType;
24
use Spaark\CompositeUtils\Model\Reflection\Type\StringType;
25
use Spaark\CompositeUtils\Model\Reflection\Type\GenericType;
26
use Spaark\CompositeUtils\Model\Generic\GenericContext;
27
use Spaark\CompositeUtils\Exception\MissingContextException;
28
use Spaark\CompositeUtils\Traits\AutoConstructTrait;
29
use Spaark\CompositeUtils\Model\ClassName;
30
31
/**
32
 * Used to retrieve the classname for an AbstractType
33
 */
34
class GenericNameProvider
35
{
36
    use AutoConstructTrait;
37
38
    const BASE = 'Spaark\CompositeUtils\Generic\\';
39
40
    /**
41
     * @var GenericContext
42
     * @construct optional
43
     */
44
    protected $context;
45
46
    /**
47
     * Infers the serialized name of the given AbstractType
48
     *
49
     * @param AbstractType $reflect
50
     * @return string
51
     */
52 10
    public function inferName(AbstractType $reflect)
53
    {
54 10
        switch (get_class($reflect))
55
        {
56 10
            case ObjectType::class:
57 3
                return $this->inferObjectName($reflect);
0 ignored issues
show
Compatibility introduced by
$reflect of type object<Spaark\CompositeU...tion\Type\AbstractType> is not a sub-type of object<Spaark\CompositeU...ection\Type\ObjectType>. It seems like you assume a child class of the class Spaark\CompositeUtils\Mo...ction\Type\AbstractType to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
58 10
            case BooleanType::class:
59 1
                return 'bool';
60 9
            case IntegerType::class:
61 2
                return 'int';
62 8
            case FloatType::class:
63 1
                return 'float';
64 7
            case MixedType::class:
65 1
                return '';
66 6
            case StringType::class:
67 3
                return 'string';
68 4
            case GenericType::class:
69 3
                return $this->inferGenericName($reflect);
0 ignored issues
show
Compatibility introduced by
$reflect of type object<Spaark\CompositeU...tion\Type\AbstractType> is not a sub-type of object<Spaark\CompositeU...ction\Type\GenericType>. It seems like you assume a child class of the class Spaark\CompositeUtils\Mo...ction\Type\AbstractType to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70
        }
71
72 1
        throw new \DomainException
73
        (
74 1
            'Unknown type: ' . get_class($reflect)
75
        );
76
    }
77
78
    /**
79
     * Infers the serialized name of the given GenericType
80
     *
81
     * @param GenericType $reflect
82
     * @return string
83
     * @throws Exception
84
     */
85 3
    protected function inferGenericName(GenericType $reflect)
86
    {
87 3
        if (!$this->context)
88
        {
89 1
            throw new MissingContextException();
90
        }
91
92 2
        return $this->inferName
93
        (
94 2
            $this->context->getGenericType($reflect->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...
95
        );
96
    }
97
98
    /**
99
     * Infers the serialized name of the given ObjectType
100
     *
101
     * @param ObjectType $reflect
102
     * @return string
103
     */
104 3
    protected function inferObjectName(ObjectType $reflect)
105
    {
106 3
        if ($reflect->generics->empty())
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...
107
        {
108 3
            return $reflect->classname;
109
        }
110
        else
111
        {
112 2
            $items = [];
113 2
            foreach ($reflect->generics as $generic)
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...
114
            {
115 2
                $items[] = $this->inferName($generic);
116
            }
117
118 2
            return new ClassName
119
            (
120 2
                  self::BASE . $reflect->classname
121 2
                . '_g' . implode('_c', $items) . '_e'
122
            );
123
        }
124
    }
125
}
126