Version 1
[yaffs-website] / vendor / phpspec / prophecy / src / Prophecy / Doubler / ClassPatch / DisableConstructorPatch.php
1 <?php
2
3 /*
4  * This file is part of the Prophecy.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *     Marcello Duarte <marcello.duarte@gmail.com>
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 <ever.zet@gmail.com>
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->hasMethod('__construct')) {
45             $node->addMethod(new MethodNode('__construct', ''));
46
47             return;
48         }
49
50         $constructor = $node->getMethod('__construct');
51         foreach ($constructor->getArguments() as $argument) {
52             $argument->setDefault(null);
53         }
54
55         $constructor->setCode(<<<PHP
56 if (0 < func_num_args()) {
57     call_user_func_array(array('parent', '__construct'), func_get_args());
58 }
59 PHP
60         );
61     }
62
63     /**
64      * Returns patch priority, which determines when patch will be applied.
65      *
66      * @return int Priority number (higher - earlier)
67      */
68     public function getPriority()
69     {
70         return 100;
71     }
72 }