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

GenericCompositeGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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\Type\BooleanType;
19
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType;
20
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType;
21
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType;
22
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType;
23
use Spaark\CompositeUtils\Model\Reflection\Type\StringType;
24
use Spaark\CompositeUtils\Model\Reflection\Type\GenericType;
25
use Spaark\CompositeUtils\Service\RawPropertyAccessor;
26
use Spaark\CompositeUtils\Service\GenericNameProvider;
27
28
/**
29
 */
30
class GenericCompositeGenerator
31
{
32
    protected $reflect;
33
34
    protected $nameProvider;
35
36 1
    public function __construct(ReflectionComposite $reflect)
37
    {
38 1
        $this->reflect = $reflect;
39 1
    }
40
41 1
    private function createObject(...$generics)
42
    {
43 1
        $object = new ObjectType(get_class($this->reflect), '');
0 ignored issues
show
Unused Code introduced by
The call to ObjectType::__construct() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
44 1
        $i = 0;
45
46 1
        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...
47
        {
48 1
            $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...
49
        }
50
51 1
        return $object;
52
    }
53
54 1
    public function generateClassCode(...$generics)
55
    {
56 1
        $this->nameProvider = new GenericNameProvider();
57 1
        $object = $this->createObject(...$generics);
58 1
        $class = $this->nameProvider->inferName($object);
59
60 1
        $class = explode('\\', $class);
61 1
        $baseClass = $class[count($class) - 1];
62 1
        unset($class[count($class) - 1]);
63 1
        $namespace = implode('\\', $class);
64 1
        $originalClass = get_class($this->reflect);
65 1
        $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...
66
67
        $code =
68 1
              '<?php namespace ' . $namespace . ';'
69 1
            . 'class ' . $baseClass . ' extends ' . $originalClass
70 1
            . '{';
71
72 1
        foreach ($this->reflect->methods as $method)
73
        {
74 1
            $code .= $this->generateMethodCode($method, $object);
75
        }
76
77 1
        $code .= '}';
78
79 1
        return $code;
80
    }
81
82 1
    public function generateMethodCode($method, ObjectType $object)
83
    {
84 1
        $params = [];
85 1
        $newParams = [];
86 1
        $paramNames = [];
87 1
        foreach ($method->parameters as $i => $param)
88
        {
89 1
            if (!$param->type instanceof GenericType)
90
            {
91 1
                $type = $param->type;
92
            }
93
            else
94
            {
95 1
                $index = $this->reflect->generics->indexOfKey
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...
96
                (
97 1
                    $param->type->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...
98
                );
99
100 1
                $type = $object->generics[$index];
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...
101
            }
102
103 1
            $paramNames[] = $name = '$' . $param->name;
104 1
            $params[] = $method->nativeParameters[$i] . ' ' . $name;
105 1
            $newParams[] =
106 1
                $this->nameProvider->inferName($type) . ' ' . $name;
107
        }
108
109
        return
110 1
              ($method->scope === 'static' ? 'static ' : '')
111 1
            . 'function ' . $method->name
112 1
            . '(' . implode(',', $params) . '){'
113 1
            . '__generic_' . $method->name
114 1
            . '(' . implode(',', $paramNames) . ');}'
115
            . "\n"
116 1
            . 'function __generic_' . $method->name
117 1
            . '(' . implode(',', $newParams) . '){'
118 1
            . 'parent::' . $method->name
119 1
            . '(' . implode(',', $paramNames) . ');}'
120 1
            . "\n";
121
    }
122
}
123