Completed
Push — 1.x ( c183a4...05b51a )
by Alexander
8s
created

AbstractProxy::getParameterCode()   F

Complexity

Conditions 12
Paths 512

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 12.7571

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 32
ccs 19
cts 23
cp 0.8261
rs 3.0554
cc 12
eloc 25
nc 512
nop 1
crap 12.7571

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Proxy;
12
13
use ReflectionParameter as Parameter;
14
use TokenReflection\ReflectionMethod as ParsedMethod;
15
use TokenReflection\ReflectionParameter as ParsedParameter;
16
17
/**
18
 * Abstract class for building different proxies
19
 */
20
abstract class AbstractProxy
21
{
22
23
    /**
24
     * Indent for source code
25
     *
26
     * @var int
27
     */
28
    protected $indent = 4;
29
30
    /**
31
     * List of advices that are used for generation of child
32
     *
33
     * @var array
34
     */
35
    protected $advices = [];
36
37
    /**
38
     * PHP expression string for accessing LSB information
39
     *
40
     * @var string
41
     */
42
    protected static $staticLsbExpression = 'static::class';
43
44
    /**
45
     * Should proxy use variadics support or not
46
     *
47
     * @var bool
48
     */
49
    protected $useVariadics = false;
50
51
    /**
52
     * Constructs an abstract proxy class
53
     *
54
     * @param array $advices List of advices
55
     * @param bool $useVariadics Should proxy use variadics syntax or not
56
     */
57 5
    public function __construct(array $advices = [], $useVariadics = false)
58
    {
59 5
        $this->advices      = $this->flattenAdvices($advices);
60 5
        $this->useVariadics = $useVariadics;
61 5
    }
62
63
    /**
64
     * Returns text representation of class
65
     *
66
     * @return string
67
     */
68
    abstract public function __toString();
69
70
    /**
71
     * Indent block of code
72
     *
73
     * @param string $text Non-indented text
74
     *
75
     * @return string Indented text
76
     */
77 5
    protected function indent($text)
78
    {
79 5
        $pad   = str_pad('', $this->indent, ' ');
80
        $lines = array_map(function($line) use ($pad) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
81 5
            return $pad . $line;
82 5
        }, explode("\n", $text));
83
84 5
        return join("\n", $lines);
85
    }
86
87
    /**
88
     * Returns list of string representation of parameters
89
     *
90
     * @param array|Parameter[]|ParsedParameter[] $parameters List of parameters
91
     *
92
     * @return array
93
     */
94 5
    protected function getParameters(array $parameters)
95
    {
96 5
        $parameterDefinitions = [];
97 5
        foreach ($parameters as $parameter) {
98
            // Deprecated since PHP5.6 in the favor of variadics, needed for BC only
99 2
            if ($parameter->name == '...') {
100
                continue;
101
            }
102 2
            $parameterDefinitions[] = $this->getParameterCode($parameter);
103
        }
104
105 5
        return $parameterDefinitions;
106
    }
107
108
    /**
109
     * Return string representation of parameter
110
     *
111
     * @param Parameter|ParsedParameter $parameter Reflection parameter
112
     *
113
     * @return string
114
     */
115 2
    protected function getParameterCode($parameter)
116
    {
117 2
        $type = '';
118 2
        if ($parameter->isArray()) {
119
            $type = 'array';
120 2
        } elseif ($parameter->isCallable()) {
121
            $type = 'callable';
122 2
        } elseif ($parameter->getClass()) {
123
            $type = '\\' . $parameter->getClass()->name;
124
        }
125 2
        $defaultValue = null;
126 2
        $isDefaultValueAvailable = $parameter->isDefaultValueAvailable();
127 2
        if ($isDefaultValueAvailable) {
128 2
            if ($parameter instanceof ParsedParameter) {
0 ignored issues
show
Bug introduced by
The class TokenReflection\ReflectionParameter does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
129 2
                $defaultValue = $parameter->getDefaultValueDefinition();
130
            } else {
131 2
                $defaultValue = var_export($parameter->getDefaultValue(), true);
132
            }
133 2
        } elseif ($parameter->isOptional()) {
134
            $defaultValue = 'null';
135
        }
136
        $code = (
137 2
            ($type ? "$type " : '') . // Typehint
138 2
            ($parameter->isPassedByReference() ? '&' : '') . // By reference sign
139 2
            ($this->useVariadics && $parameter->isVariadic() ? '...' : '') . // Variadic symbol
140 2
            '$' . // Variable symbol
141 2
            ($parameter->name) . // Name of the argument
142 2
            ($defaultValue !== null ? (" = " . $defaultValue) : '') // Default value if present
143
        );
144
145 2
        return $code;
146
    }
147
148
    /**
149
     * Replace concrete advices with list of ids
150
     *
151
     * @param $advices
152
     *
153
     * @return array flatten list of advices
154
     */
155 5
    private function flattenAdvices($advices)
156
    {
157 5
        $flattenAdvices = [];
158 5
        foreach ($advices as $type => $typedAdvices) {
159 5
            foreach ($typedAdvices as $name => $concreteAdvices) {
160 5
                if (is_array($concreteAdvices)) {
161 5
                    $flattenAdvices[$type][$name] = array_keys($concreteAdvices);
162
                }
163
            }
164
        }
165
166 5
        return $flattenAdvices;
167
    }
168
169
    /**
170
     * Prepares a line with args from the method definition
171
     *
172
     * @param ParsedMethod $method
173
     *
174
     * @return string
175
     */
176
    protected function prepareArgsLine(ParsedMethod $method)
177
    {
178 5
        $args = join(', ', array_map(function(ParsedParameter $param) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
179 2
            $byReference = $param->isPassedByReference() ? '&' : '';
180
181 2
            return $byReference . '$' . $param->name;
182 5
        }, $method->getParameters()));
183
184 5
        return $args;
185
    }
186
}
187