Skip to content

Commit f2e6bc2

Browse files
authored
fix: Add an additional function_exists statement for excluded function checks (#979)
Closes #963
1 parent 82d6630 commit f2e6bc2

File tree

7 files changed

+543
-4
lines changed

7 files changed

+543
-4
lines changed

fixtures/set040-polyfills/bootstrap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@
1313
if (!function_exists('new_php20_function')) {
1414
function new_php20_function(bool $echo = false): void { Php20::new_php20_function($echo); }
1515
}
16+
17+
// 2nd declaration: in practice this can easily happen for a function polyfilled in
18+
// multiple packages.
19+
if (!function_exists('new_php20_function')) {
20+
function new_php20_function(bool $echo = false): void { Php20::new_php20_function($echo); }
21+
}
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
return [
16+
'meta' => [
17+
'title' => 'Function declarations in the global scope',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
21+
'expose-global-constants' => false,
22+
'expose-global-classes' => false,
23+
'expose-global-functions' => false,
24+
'expose-namespaces' => [],
25+
'expose-constants' => [],
26+
'expose-classes' => [],
27+
'expose-functions' => [],
28+
29+
'exclude-namespaces' => [],
30+
'exclude-constants' => [],
31+
'exclude-classes' => [],
32+
'exclude-functions' => ['new_php20_function'],
33+
34+
'expected-recorded-classes' => [],
35+
'expected-recorded-functions' => [
36+
['new_php20_function', 'Humbug\new_php20_function'],
37+
],
38+
],
39+
40+
'simple declaration' => <<<'PHP'
41+
<?php
42+
43+
if (!function_exists('new_php20_function')) {
44+
function new_php20_function() {}
45+
}
46+
----
47+
<?php
48+
49+
namespace Humbug;
50+
51+
if (!\function_exists('new_php20_function') && !\function_exists('Humbug\\new_php20_function')) {
52+
function new_php20_function()
53+
{
54+
}
55+
}
56+
57+
PHP,
58+
59+
'simple declaration with explicit equal comparison check' => <<<'PHP'
60+
<?php
61+
62+
if (false == function_exists('new_php20_function')) {
63+
function new_php20_function() {}
64+
}
65+
----
66+
<?php
67+
68+
namespace Humbug;
69+
70+
if (\false == \function_exists('new_php20_function') && !\function_exists('Humbug\\new_php20_function')) {
71+
function new_php20_function()
72+
{
73+
}
74+
}
75+
76+
PHP,
77+
78+
'simple inversed declaration with explicit equal comparison check' => <<<'PHP'
79+
<?php
80+
81+
if (function_exists('new_php20_function') == false) {
82+
function new_php20_function() {}
83+
}
84+
----
85+
<?php
86+
87+
namespace Humbug;
88+
89+
if (\function_exists('new_php20_function') == \false && !\function_exists('Humbug\\new_php20_function')) {
90+
function new_php20_function()
91+
{
92+
}
93+
}
94+
95+
PHP,
96+
97+
'simple declaration with explicit identical comparison check' => <<<'PHP'
98+
<?php
99+
100+
if (false === function_exists('new_php20_function')) {
101+
function new_php20_function() {}
102+
}
103+
----
104+
<?php
105+
106+
namespace Humbug;
107+
108+
if (\false === \function_exists('new_php20_function') && !\function_exists('Humbug\\new_php20_function')) {
109+
function new_php20_function()
110+
{
111+
}
112+
}
113+
114+
PHP,
115+
116+
'simple inversed declaration with explicit identical comparison check' => <<<'PHP'
117+
<?php
118+
119+
if (function_exists('new_php20_function') === false) {
120+
function new_php20_function() {}
121+
}
122+
----
123+
<?php
124+
125+
namespace Humbug;
126+
127+
if (\function_exists('new_php20_function') === \false && !\function_exists('Humbug\\new_php20_function')) {
128+
function new_php20_function()
129+
{
130+
}
131+
}
132+
133+
PHP,
134+
135+
'Already handled declaration' => <<<'PHP'
136+
<?php
137+
138+
if (!function_exists('new_php20_function') && !function_exists('Humbug\new_php20_function')) {
139+
function new_php20_function() {}
140+
}
141+
----
142+
<?php
143+
144+
namespace Humbug;
145+
146+
if (!\function_exists('new_php20_function') && !\function_exists('Humbug\\new_php20_function') && !\function_exists('Humbug\\new_php20_function')) {
147+
function new_php20_function()
148+
{
149+
}
150+
}
151+
152+
PHP,
153+
154+
'Non boolean not condition' => <<<'PHP'
155+
<?php
156+
157+
if (function_exists('new_php20_function')) {
158+
function new_php20_function() {}
159+
}
160+
----
161+
<?php
162+
163+
namespace Humbug;
164+
165+
if (\function_exists('new_php20_function')) {
166+
function new_php20_function()
167+
{
168+
}
169+
}
170+
171+
PHP,
172+
173+
'If condition is a BinaryOp_BooleanAnd; function exists is the left operand' => <<<'PHP'
174+
<?php
175+
176+
if (!function_exists('new_php20_function') && PHP_VERSION_ID <= 80000) {
177+
function new_php20_function() {}
178+
}
179+
----
180+
<?php
181+
182+
namespace Humbug;
183+
184+
if (!\function_exists('new_php20_function') && !\function_exists('Humbug\\new_php20_function') && \PHP_VERSION_ID <= 80000) {
185+
function new_php20_function()
186+
{
187+
}
188+
}
189+
190+
PHP,
191+
192+
'If condition is a BinaryOp_BooleanAnd; function exists is the right operand' => <<<'PHP'
193+
<?php
194+
195+
if (PHP_VERSION_ID <= 80000 && !function_exists('new_php20_function')) {
196+
function new_php20_function() {}
197+
}
198+
----
199+
<?php
200+
201+
namespace Humbug;
202+
203+
if (\PHP_VERSION_ID <= 80000 && (!\function_exists('new_php20_function') && !\function_exists('Humbug\\new_php20_function'))) {
204+
function new_php20_function()
205+
{
206+
}
207+
}
208+
209+
PHP,
210+
211+
'If condition is a BinaryOp_BooleanOr' => <<<'PHP'
212+
<?php
213+
214+
if (!function_exists('new_php20_function') || PHP_VERSION_ID <= 80000) {
215+
function new_php20_function() {}
216+
}
217+
----
218+
<?php
219+
220+
namespace Humbug;
221+
222+
if (!\function_exists('new_php20_function') && !\function_exists('Humbug\\new_php20_function') || \PHP_VERSION_ID <= 80000) {
223+
function new_php20_function()
224+
{
225+
}
226+
}
227+
228+
PHP,
229+
];

specs/func-declaration/global.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ function trigger_deprecation() {}
614614
615615
namespace Humbug;
616616
617-
if (!\function_exists('trigger_deprecation')) {
617+
if (!\function_exists('trigger_deprecation') && !\function_exists('Humbug\\trigger_deprecation')) {
618618
function trigger_deprecation()
619619
{
620620
}

0 commit comments

Comments
 (0)