Completed
Pull Request — master (#253)
by Alexander
22:35
created

AbstractProxy::flattenAdvices()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4
Metric Value
dl 0
loc 13
ccs 2
cts 2
cp 1
rs 9.2
cc 4
eloc 7
nc 4
nop 1
crap 4
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 ReflectionFunctionAbstract;
14
use ReflectionParameter;
15
16
/**
17
 * Abstract class for building different proxies
18
 */
19
abstract class AbstractProxy
20
{
21
22
    /**
23
     * Indent for source code
24
     *
25
     * @var int
26
     */
27
    protected $indent = 4;
28
29
    /**
30
     * List of advices that are used for generation of child
31
     *
32
     * @var array
33
     */
34
    protected $advices = [];
35
36
    /**
37
     * PHP expression string for accessing LSB information
38
     *
39
     * @var string
40
     */
41
    protected static $staticLsbExpression = 'static::class';
42
43
    /**
44
     * Constructs an abstract proxy class
45
     *
46
     * @param array $advices List of advices
47
     */
48
    public function __construct(array $advices = [])
49
    {
50
        $this->advices = $this->flattenAdvices($advices);
51
    }
52
53
    /**
54
     * Returns text representation of class
55
     *
56 5
     * @return string
57
     */
58 5
    abstract public function __toString();
59 5
60 5
    /**
61
     * Indent block of code
62
     *
63
     * @param string $text Non-indented text
64
     *
65
     * @return string Indented text
66
     */
67
    protected function indent($text)
68
    {
69
        $pad   = str_pad('', $this->indent, ' ');
70
        $lines = array_map(function($line) use ($pad) {
71
            return $pad . $line;
72
        }, explode("\n", $text));
73
74
        return join("\n", $lines);
75
    }
76 5
77
    /**
78 5
     * Returns list of string representation of parameters
79
     *
80 5
     * @param array|ReflectionParameter[] $parameters List of parameters
81 5
     *
82
     * @return array
83 5
     */
84
    protected function getParameters(array $parameters)
85
    {
86
        $parameterDefinitions = [];
87
        foreach ($parameters as $parameter) {
88
            $parameterDefinitions[] = $this->getParameterCode($parameter);
89
        }
90
91
        return $parameterDefinitions;
92
    }
93 5
94
    /**
95 5
     * Return string representation of parameter
96 5
     *
97
     * @param ReflectionParameter $parameter Reflection parameter
98 2
     *
99
     * @return string
100
     */
101 2
    protected function getParameterCode(ReflectionParameter $parameter)
102
    {
103
        $type = '';
104 5
        if ($parameter->isArray()) {
105
            $type = 'array';
106
        } elseif ($parameter->isCallable()) {
107
            $type = 'callable';
108
        } elseif ($parameter->getClass()) {
109
            $type = '\\' . $parameter->getClass()->name;
110
        }
111
        $defaultValue = null;
112
        $isDefaultValueAvailable = $parameter->isDefaultValueAvailable();
113
        if ($isDefaultValueAvailable) {
114 2
            $defaultValue = var_export($parameter->getDefaultValue(), true);
115
        } elseif ($parameter->isOptional()) {
116 2
            $defaultValue = 'null';
117 2
        }
118
        $code = (
119 2
            ($type ? "$type " : '') . // Typehint
120
            ($parameter->isPassedByReference() ? '&' : '') . // By reference sign
121 2
            ($parameter->isVariadic() ? '...' : '') . // Variadic symbol
122
            '$' . // Variable symbol
123
            ($parameter->name) . // Name of the argument
124 2
            ($defaultValue !== null ? (" = " . $defaultValue) : '') // Default value if present
125 2
        );
126 2
127 2
        return $code;
128 2
    }
129
130
    /**
131
     * Replace concrete advices with list of ids
132 2
     *
133 2
     * @param $advices
134 2
     *
135 2
     * @return array flatten list of advices
136 2
     */
137 2
    private function flattenAdvices($advices)
138
    {
139
        $flattenAdvices = [];
140 2
        foreach ($advices as $type => $typedAdvices) {
141
            foreach ($typedAdvices as $name => $concreteAdvices) {
142
                if (is_array($concreteAdvices)) {
143
                    $flattenAdvices[$type][$name] = array_keys($concreteAdvices);
144
                }
145
            }
146
        }
147
148
        return $flattenAdvices;
149
    }
150 5
151
    /**
152 5
     * Prepares a line with args from the method definition
153 5
     *
154 5
     * @param ReflectionFunctionAbstract $functionLike
155 5
     *
156 5
     * @return string
157
     */
158
    protected function prepareArgsLine(ReflectionFunctionAbstract $functionLike)
159
    {
160
        $argumentsPart = [];
161 5
        $arguments     = [];
162
        $hasOptionals  = false;
163
164
        foreach ($functionLike->getParameters() as $parameter) {
165
            $byReference  = ($parameter->isPassedByReference() && !$parameter->isVariadic()) ? '&' : '';
166
            $hasOptionals = $hasOptionals || $parameter->isOptional();
167
168
            $arguments[] = $byReference . '$' . $parameter->name;
169
        }
170
171
        $isVariadic = $functionLike->isVariadic();
172
        if ($isVariadic) {
173 5
            $argumentsPart[] = array_pop($arguments);
174 2
        }
175
        if (!empty($arguments)) {
176 2
            // Unshifting to keep correct order
177 5
            $argumentLine = '[' . join(', ', $arguments) . ']';
178
            if ($hasOptionals) {
179 5
                $argumentLine = "\\array_slice($argumentLine, 0, \\func_num_args())";
180
            }
181
            array_unshift($argumentsPart, $argumentLine);
182
        }
183
184
        return join(', ', $argumentsPart);
185
    }
186
}
187