DisableConstructorPatch   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 1
A apply() 0 24 4
A getPriority() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Prophecy.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *     Marcello Duarte <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Prophecy\Doubler\ClassPatch;
13
14
use Prophecy\Doubler\Generator\Node\ClassNode;
15
use Prophecy\Doubler\Generator\Node\MethodNode;
16
17
/**
18
 * Disable constructor.
19
 * Makes all constructor arguments optional.
20
 *
21
 * @author Konstantin Kudryashov <[email protected]>
22
 */
23
class DisableConstructorPatch implements ClassPatchInterface
24
{
25
    /**
26
     * Checks if class has `__construct` method.
27
     *
28
     * @param ClassNode $node
29
     *
30
     * @return bool
31
     */
32
    public function supports(ClassNode $node)
33
    {
34
        return true;
35
    }
36
37
    /**
38
     * Makes all class constructor arguments optional.
39
     *
40
     * @param ClassNode $node
41
     */
42
    public function apply(ClassNode $node)
43
    {
44
        if (!$node->isExtendable('__construct')) {
45
            return;
46
        }
47
48
        if (!$node->hasMethod('__construct')) {
49
            $node->addMethod(new MethodNode('__construct', ''));
50
51
            return;
52
        }
53
54
        $constructor = $node->getMethod('__construct');
55
        foreach ($constructor->getArguments() as $argument) {
56
            $argument->setDefault(null);
57
        }
58
59
        $constructor->setCode(<<<PHP
60
if (0 < func_num_args()) {
61
    call_user_func_array(array('parent', '__construct'), func_get_args());
62
}
63
PHP
64
        );
65
    }
66
67
    /**
68
     * Returns patch priority, which determines when patch will be applied.
69
     *
70
     * @return int Priority number (higher - earlier)
71
     */
72
    public function getPriority()
73
    {
74
        return 100;
75
    }
76
}
77