Completed
Pull Request — master (#7)
by Emily
02:46
created

GenericCompositeGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 105
ccs 43
cts 43
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createObject() 0 12 2
B generateClassCode() 0 26 2
B generateMethodCode() 0 27 3
A createClass() 0 4 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\Factory\Reflection;
16
17
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite;
18
use Spaark\CompositeUtils\Model\Reflection\ReflectionMethod;
19
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType;
20
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType;
21
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType;
22
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType;
23
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType;
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\Service\RawPropertyAccessor;
28
use Spaark\CompositeUtils\Service\GenericNameProvider;
29
use Spaark\CompositeUtils\Traits\AutoConstructTrait;
30
31
/**
32
 * Generates the code for a generic class
33
 */
34
class GenericCompositeGenerator
35
{
36
    use AutoConstructTrait;
37
38
    /**
39
     * @var ReflectionComposite
40
     * @construct required
41
     */
42
    protected $reflect;
43
44
    /**
45
     * @var GenericNameProvider
46
     */
47
    protected $nameProvider;
48
49
    /**
50
     * Creates an ObjectType from the given list of generics
51
     *
52
     * @param AbstractType[] $generics
53
     */
54 4
    private function createObject(...$generics) : ObjectType
55
    {
56 4
        $object = new ObjectType($this->reflect->classname);
57 4
        $i = 0;
58
59 4
        foreach ($this->reflect->generics as $name => $value)
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...
60
        {
61 4
            $object->generics[] = $generics[$i++] ?? $value;
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...
62
        }
63
64 4
        return $object;
65
    }
66
67
    /**
68
     * Generate class code for the given generics
69
     *
70
     * @param AbstractType[] $generics
71
     * @return string
72
     */
73 4
    public function generateClassCode(...$generics) : string
74
    {
75 4
        $object = $this->createObject(...$generics);
76 4
        $this->nameProvider = new GenericNameProvider
77
        (
78 4
            new GenericContext($object, $this->reflect)
79
        );
80 4
        $class = $this->nameProvider->inferName($object);
81 4
        $originalClass = $this->reflect->classname;
82 4
        $i = 0;
0 ignored issues
show
Unused Code introduced by
$i is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
84
        $code =
85 4
              'namespace ' . $class->namespace . ';'
86 4
            . 'class ' . $class->classname . ' '
87 4
            .     'extends \\' . $originalClass
88 4
            . '{';
89
90 4
        foreach ($this->reflect->methods as $method)
91
        {
92 4
            $code .= $this->generateMethodCode($method);
93
        }
94
95 4
        $code .= '}';
96
97 4
        return $code;
98
    }
99
100
    /**
101
     * Generates the method code for the current class being generated
102
     *
103
     * @param ReflectionMethod $method
104
     * @return string
105
     */
106 4
    public function generateMethodCode(ReflectionMethod $method)
107
        : string
108
    {
109 4
        $params = [];
110 4
        $newParams = [];
111 4
        $paramNames = [];
112 4
        foreach ($method->parameters as $i => $param)
113
        {
114 4
            $paramNames[] = $name = ' $' . $param->name;
115 4
            $params[] = $method->nativeParameters[$i] . $name;
0 ignored issues
show
Documentation introduced by
The property $nativeParameters is declared protected in Spaark\CompositeUtils\Mo...ection\ReflectionMethod. 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...
116 4
            $newParams[] =
117 4
                $this->nameProvider->inferName($param->type) . $name;
118
        }
119
120
        return
121 4
              ($method->scope === 'static' ? 'static ' : '')
122 4
            . 'function ' . $method->name
123 4
            . '(' . implode(',', $params) . '){'
124 4
            . '__generic_' . $method->name
125 4
            . '(' . implode(',', $paramNames) . ');}'
126
            . "\n"
127 4
            . 'function __generic_' . $method->name
128 4
            . '(' . implode(',', $newParams) . '){'
129 4
            . 'parent::' . $method->name
130 4
            . '(' . implode(',', $paramNames) . ');}'
131 4
            . "\n";
132
    }
133
134 2
    public function createClass(...$generics)
135
    {
136 2
        eval($this->generateClassCode(...$generics));
137 2
    }
138
}
139