/
ProxiedMethodReturnExpression.php
69 lines (64 loc) · 2.48 KB
/
ProxiedMethodReturnExpression.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ProxyManager\Generator\Util;
use Composer\Autoload\ClassLoader;
use ReflectionMethod;
use Symfony\Component\Debug\DebugClassLoader;
if (class_exists(\ProxyManager\Version::class) && \version_compare(\ProxyManager\Version::getVersion(), '2.5', '<')) {
/**
* Utility class to generate return expressions in method, given a method signature.
*
* This is required since return expressions may be forbidden by the method signature (void).
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
final class ProxiedMethodReturnExpression
{
public static function generate(string $returnedValueExpression, ?ReflectionMethod $originalMethod) : string
{
$originalReturnType = $originalMethod === null
? null
: $originalMethod->getReturnType();
$originalReturnTypeName = $originalReturnType === null
? null
: $originalReturnType->getName();
if ($originalReturnTypeName === 'void') {
return $returnedValueExpression . ";\nreturn;";
}
return 'return ' . $returnedValueExpression . ';';
}
}
} else {
// Fallback to the original class by unregistering this file from composer class loader
$getComposerClassLoader = static function($functionLoader) use (&$getComposerClassLoader) {
if (\is_array($functionLoader)) {
$functionLoader = $functionLoader[0];
}
if (!\is_object($functionLoader)) {
return;
}
if ($functionLoader instanceof ClassLoader) {
return $functionLoader;
}
if ($functionLoader instanceof DebugClassLoader) {
return $getComposerClassLoader($functionLoader->getClassLoader());
}
};
$classLoader = null;
while($classLoader === null && count($functions = spl_autoload_functions()) > 0) {
$classLoader = $getComposerClassLoader(\array_shift($functions));
}
if (null === $classLoader) {
throw new \RuntimeException('Unable to find a class loader for "%s"');
}
$classLoader->addClassMap([ProxiedMethodReturnExpression::class => null]);
$classLoader->loadClass(ProxiedMethodReturnExpression::class);
}