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 * Remove method functionality from the double which will clash with php keywords.
18 *
19 * @author Milan Magudia <milan@magudia.com>
20 */
21class KeywordPatch implements ClassPatchInterface
22{
23    /**
24     * Support any class
25     *
26     * @param ClassNode $node
27     *
28     * @return boolean
29     */
30    public function supports(ClassNode $node)
31    {
32        return true;
33    }
34
35    /**
36     * Remove methods that clash with php keywords
37     *
38     * @param ClassNode $node
39     */
40    public function apply(ClassNode $node)
41    {
42        $methodNames = array_keys($node->getMethods());
43        $methodsToRemove = array_intersect($methodNames, $this->getKeywords());
44        foreach ($methodsToRemove as $methodName) {
45            $node->removeMethod($methodName);
46        }
47    }
48
49    /**
50     * Returns patch priority, which determines when patch will be applied.
51     *
52     * @return int Priority number (higher - earlier)
53     */
54    public function getPriority()
55    {
56        return 49;
57    }
58
59    /**
60     * Returns array of php keywords.
61     *
62     * @return array
63     */
64    private function getKeywords()
65    {
66        if (\PHP_VERSION_ID >= 70000) {
67            return array('__halt_compiler');
68        }
69
70        return array(
71            '__halt_compiler',
72            'abstract',
73            'and',
74            'array',
75            'as',
76            'break',
77            'callable',
78            'case',
79            'catch',
80            'class',
81            'clone',
82            'const',
83            'continue',
84            'declare',
85            'default',
86            'die',
87            'do',
88            'echo',
89            'else',
90            'elseif',
91            'empty',
92            'enddeclare',
93            'endfor',
94            'endforeach',
95            'endif',
96            'endswitch',
97            'endwhile',
98            'eval',
99            'exit',
100            'extends',
101            'final',
102            'finally',
103            'for',
104            'foreach',
105            'function',
106            'global',
107            'goto',
108            'if',
109            'implements',
110            'include',
111            'include_once',
112            'instanceof',
113            'insteadof',
114            'interface',
115            'isset',
116            'list',
117            'namespace',
118            'new',
119            'or',
120            'print',
121            'private',
122            'protected',
123            'public',
124            'require',
125            'require_once',
126            'return',
127            'static',
128            'switch',
129            'throw',
130            'trait',
131            'try',
132            'unset',
133            'use',
134            'var',
135            'while',
136            'xor',
137            'yield',
138        );
139    }
140}
141