Completed
Pull Request — master (#277)
by Marco
04:04 queued 17s
created

StaticProxyConstructor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.3142
cc 1
eloc 15
nc 1
nop 2
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
namespace ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator;
20
21
use ProxyManager\Factory\RemoteObject\AdapterInterface;
22
use ProxyManager\Generator\MethodGenerator;
23
use ProxyManager\ProxyGenerator\Util\Properties;
24
use ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator;
25
use ReflectionClass;
26
use Zend\Code\Generator\ParameterGenerator;
27
use Zend\Code\Generator\PropertyGenerator;
28
29
/**
30
 * The `staticProxyConstructor` implementation for remote object proxies
31
 *
32
 * @author Vincent Blanchon <[email protected]>
33
 * @author Marco Pivetta <[email protected]>
34
 * @license MIT
35
 */
36
class StaticProxyConstructor extends MethodGenerator
37
{
38
    /**
39
     * Constructor
40
     *
41
     * @param ReflectionClass   $originalClass Reflection of the class to proxy
42
     * @param PropertyGenerator $adapter       Adapter property
43
     */
44 1
    public function __construct(ReflectionClass $originalClass, PropertyGenerator $adapter)
45
    {
46 1
        $adapterName = $adapter->getName();
47
48 1
        parent::__construct(
49 1
            'staticProxyConstructor',
50 1
            [new ParameterGenerator($adapterName, AdapterInterface::class)],
51 1
            MethodGenerator::FLAG_PUBLIC | MethodGenerator::FLAG_STATIC,
52 1
            null,
53
            'Constructor for remote object control\n\n'
54 1
            . '@param \\ProxyManager\\Factory\\RemoteObject\\AdapterInterface \$adapter'
55
        );
56
57
        $body = 'static $reflection;' . "\n\n"
58
            . '$reflection = $reflection ?: $reflection = new \ReflectionClass(__CLASS__);' . "\n"
59
            . '$instance = (new \ReflectionClass(get_class()))->newInstanceWithoutConstructor();' . "\n\n"
60 1
            . '$instance->' . $adapterName . ' = $' . $adapterName . ";\n\n"
61 1
            . UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'instance');
62
63 1
        $this->setBody($body . "\n\nreturn \$instance;");
64 1
    }
65
}
66