Skip to content

Commit 4203433

Browse files
authored
Optimize native function and constant calls (#169)
1 parent c443d4d commit 4203433

35 files changed

+248
-102
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
4545
- [Limitations](#limitations)
4646
- [PSR-0 support](#psr-0-support)
4747
- [String values](#string-values)
48+
- [Native functions and constants shadowing](#native-functions-shadowing)
4849
- [Contributing](#contributing)
4950
- [Credits](#credits)
5051

@@ -437,6 +438,41 @@ bound to have confusing cases. For example:
437438
name or a random string.
438439

439440

441+
### Native functions and constants shadowing
442+
443+
In the following example:
444+
445+
```php
446+
<?php
447+
448+
namespace Foo;
449+
450+
is_array([]);
451+
452+
```
453+
454+
No use statement is used for the function `is_array`. This means that PHP will try to load the function `\Foo\is_array`
455+
and if fails to do so will fallback on `\is_array` (note that PHP does so only for functions and constants: not classes).
456+
457+
In order to bring some performance optimisation, the call will nonetheless be prefixed in `\is_array`. This *will* break
458+
your code if you were relying on `\Foo\is_array` instead. This however should be _extremely_ rare, so if that happens
459+
you have two solutions: use a [patcher](#patchers) or simpler remove any ambiguity by making use of a use statement
460+
(which is unneeded outside of the context of prefixing your code):
461+
462+
```php
463+
<?php
464+
465+
namespace Foo;
466+
467+
use function Foo\is_array;
468+
469+
is_array([]);
470+
471+
```
472+
473+
The situation is exactly the same for constants.
474+
475+
440476
## Contributing
441477

442478
[Contribution Guide](CONTRIBUTING.md)

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scoper.inc.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,12 @@ function (string $filePath, string $prefix, string $contents): string {
4444
//
4545
// PHP-Parser patch
4646
//
47-
48-
if ($filePath === __DIR__.'/vendor/nikic/php-parser/lib/PhpParser/Lexer.php') {
49-
return preg_replace(
50-
'%if \(defined\(\$name = \'PhpParser\\\\\\\\Parser\\\\\\\\Tokens%',
51-
'if (defined($name = \''.$prefix.'\\\\\\\\PhpParser\\\\\\\\Parser\\\\\\\\Tokens',
52-
$contents
53-
);
54-
}
55-
5647
if ($filePath === realpath(__DIR__.'/vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php')) {
5748
$length = 15 + strlen($prefix) + 1;
5849

5950
return preg_replace(
60-
'%rtrim\(get_class\(\$this\), \'_\'\), 15\)%',
61-
sprintf('rtrim(get_class($this), \'_\'), %d)', $length),
51+
'%strpos\((.+?)\) \+ 15%',
52+
sprintf('strpos($1) + %d', $length),
6253
$contents
6354
);
6455
}

specs/class-FQ.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ class Foo
7878
class Bar
7979
{
8080
}
81-
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
81+
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
8282
namespace Humbug\Foo\Bar;
8383
8484
class Poz
8585
{
8686
}
87-
class_alias('Humbug\\Foo\\Bar\\Poz', 'Foo\\Bar\\Poz', \false);
87+
\class_alias('Humbug\\Foo\\Bar\\Poz', 'Foo\\Bar\\Poz', \false);
8888
namespace Humbug;
8989
9090
use Humbug\Foo as X;
@@ -279,47 +279,47 @@ class Foo
279279
class Bar
280280
{
281281
}
282-
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
282+
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
283283
namespace Humbug\Foo\Bar;
284284
285285
class Poz
286286
{
287287
}
288-
class_alias('Humbug\\Foo\\Bar\\Poz', 'Foo\\Bar\\Poz', \false);
288+
\class_alias('Humbug\\Foo\\Bar\\Poz', 'Foo\\Bar\\Poz', \false);
289289
namespace Humbug\A;
290290
291291
class Aoo
292292
{
293293
}
294-
class_alias('Humbug\\A\\Aoo', 'A\\Aoo', \false);
294+
\class_alias('Humbug\\A\\Aoo', 'A\\Aoo', \false);
295295
class Foo
296296
{
297297
}
298-
class_alias('Humbug\\A\\Foo', 'A\\Foo', \false);
298+
\class_alias('Humbug\\A\\Foo', 'A\\Foo', \false);
299299
namespace Humbug\A\Foo;
300300
301301
class Bar
302302
{
303303
}
304-
class_alias('Humbug\\A\\Foo\\Bar', 'A\\Foo\\Bar', \false);
304+
\class_alias('Humbug\\A\\Foo\\Bar', 'A\\Foo\\Bar', \false);
305305
namespace Humbug\A\Foo\Bar;
306306
307307
class Poz
308308
{
309309
}
310-
class_alias('Humbug\\A\\Foo\\Bar\\Poz', 'A\\Foo\\Bar\\Poz', \false);
310+
\class_alias('Humbug\\A\\Foo\\Bar\\Poz', 'A\\Foo\\Bar\\Poz', \false);
311311
namespace Humbug\A\Aoo;
312312
313313
class Aoz
314314
{
315315
}
316-
class_alias('Humbug\\A\\Aoo\\Aoz', 'A\\Aoo\\Aoz', \false);
316+
\class_alias('Humbug\\A\\Aoo\\Aoz', 'A\\Aoo\\Aoz', \false);
317317
namespace Humbug\A\Aoo\Aoz;
318318
319319
class Poz
320320
{
321321
}
322-
class_alias('Humbug\\A\\Aoo\\Aoz\\Poz', 'A\\Aoo\\Aoz\\Poz', \false);
322+
\class_alias('Humbug\\A\\Aoo\\Aoz\\Poz', 'A\\Aoo\\Aoz\\Poz', \false);
323323
namespace Humbug\A;
324324
325325
use Humbug\Foo as X;

specs/class-const/global-scope-two-level-with-single-level-use-and-alias.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class Foo
179179
class Bar
180180
{
181181
}
182-
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
182+
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
183183
namespace Humbug;
184184
185185
use Humbug\Foo as X;

specs/class-const/global-scope-two-level-with-single-level-use.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class Foo
189189
class Bar
190190
{
191191
}
192-
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
192+
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
193193
namespace Humbug;
194194
195195
use Humbug\Foo;
@@ -235,7 +235,7 @@ class Foo
235235
class Bar
236236
{
237237
}
238-
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
238+
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
239239
namespace Humbug;
240240
241241
use Humbug\Foo;

specs/class-const/global-scope-two-level.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Command {}
110110
class Command
111111
{
112112
}
113-
class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
113+
\class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
114114
namespace Humbug;
115115
116116
\Humbug\PHPUnit\Command::MAIN_CONST;
@@ -144,7 +144,7 @@ class Command {}
144144
class Command
145145
{
146146
}
147-
class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
147+
\class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
148148
namespace Humbug;
149149
150150
\Humbug\PHPUnit\Command::MAIN_CONST;

specs/class-const/namespace-scope-two-level-with-single-level-use-and-alias.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class Foo
183183
class Bar
184184
{
185185
}
186-
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
186+
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
187187
namespace Humbug\A;
188188
189189
use Humbug\Foo as X;

specs/class-const/namespace-scope-two-level-with-single-level-use.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class Foo
193193
class Bar
194194
{
195195
}
196-
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
196+
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
197197
namespace Humbug\X;
198198
199199
use Humbug\Foo;
@@ -240,7 +240,7 @@ class Foo
240240
class Bar
241241
{
242242
}
243-
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
243+
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
244244
namespace Humbug\X;
245245
246246
use Humbug\Foo;

specs/class-const/namespace-scope-two-level.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Command {}
113113
class Command
114114
{
115115
}
116-
class_alias('Humbug\\X\\PHPUnit\\Command', 'X\\PHPUnit\\Command', \false);
116+
\class_alias('Humbug\\X\\PHPUnit\\Command', 'X\\PHPUnit\\Command', \false);
117117
namespace Humbug\X;
118118
119119
\Humbug\X\PHPUnit\Command::MAIN_CONST;
@@ -148,7 +148,7 @@ class Command {}
148148
class Command
149149
{
150150
}
151-
class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
151+
\class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
152152
namespace Humbug\X;
153153
154154
\Humbug\PHPUnit\Command::MAIN_CONST;

0 commit comments

Comments
 (0)