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
12namespace Prophecy\Doubler\ClassPatch;
13
14use Prophecy\Doubler\Generator\Node\ClassNode;
15
16/**
17 * ReflectionClass::newInstance patch.
18 * Makes first argument of newInstance optional, since it works but signature is misleading
19 *
20 * @author Florian Klein <florian.klein@free.fr>
21 */
22class ReflectionClassNewInstancePatch implements ClassPatchInterface
23{
24    /**
25     * Supports ReflectionClass
26     *
27     * @param ClassNode $node
28     *
29     * @return bool
30     */
31    public function supports(ClassNode $node)
32    {
33        return 'ReflectionClass' === $node->getParentClass();
34    }
35
36    /**
37     * Updates newInstance's first argument to make it optional
38     *
39     * @param ClassNode $node
40     */
41    public function apply(ClassNode $node)
42    {
43        foreach ($node->getMethod('newInstance')->getArguments() as $argument) {
44            $argument->setDefault(null);
45        }
46    }
47
48    /**
49     * Returns patch priority, which determines when patch will be applied.
50     *
51     * @return int Priority number (higher = earlier)
52     */
53    public function getPriority()
54    {
55        return 50;
56    }
57}
58