Completed
Push — master ( b8da3f...f66325 )
by Emily
10s
created

GenericNameProvider::inferObjectName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 9
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\Exception\PropertyNotWritableException;
26
use Spaark\CompositeUtils\Exception\PropertyNotReadableException;
27
28
/**
29
 * Used to retrieve the classname for an AbstractType
30
 */
31
class GenericNameProvider
32
{
33
    const BASE = 'Spaark\CompositeUtils\Generic\\';
34
35
    /**
36
     * Infers the serialized name of the given AbstractType
37
     *
38
     * @param AbstractType $reflect
39
     * @return string
40
     */
41 2
    public function inferName(AbstractType $reflect)
42
    {
43 2
        switch (get_class($reflect))
44
        {
45 2
            case ObjectType::class:
46 2
                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...
47 2
            case BooleanType::class:
48
                return 'boolean';
49 2
            case IntegerType::class:
50 1
                return 'int';
51 2
            case FloatType::class:
52
                return 'float';
53 2
            case MixedType::class:
54
                return '';
55 2
            case StringType::class:
56 2
                return 'string';
57
        }
58
    }
59
60
    /**
61
     * Infers the serialized name of the given ObjectType
62
     *
63
     * @param ObjectType $reflect
64
     * @return string
65
     */
66 2
    protected function inferObjectName(ObjectType $reflect)
67
    {
68 2
        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...
69
        {
70 2
            return $reflect->classname;
71
        }
72
        else
73
        {
74 2
            $items = [];
75 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...
76
            {
77 2
                $items[] = $this->inferName($generic);
78
            }
79
80 2
            return self::BASE . $reflect->classname
81 2
                . '_g' . implode('_c', $items) . '_e';
82
        }
83
    }
84
}
85