Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize native function and constant calls #169

Merged
merged 1 commit into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
- [Limitations](#limitations)
- [PSR-0 support](#psr-0-support)
- [String values](#string-values)
- [Native functions and constants shadowing](#native-functions-shadowing)
- [Contributing](#contributing)
- [Credits](#credits)

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


### Native functions and constants shadowing

In the following example:

```php
<?php

namespace Foo;

is_array([]);

```

No use statement is used for the function `is_array`. This means that PHP will try to load the function `\Foo\is_array`
and if fails to do so will fallback on `\is_array` (note that PHP does so only for functions and constants: not classes).

In order to bring some performance optimisation, the call will nonetheless be prefixed in `\is_array`. This *will* break
your code if you were relying on `\Foo\is_array` instead. This however should be _extremely_ rare, so if that happens
you have two solutions: use a [patcher](#patchers) or simpler remove any ambiguity by making use of a use statement
(which is unneeded outside of the context of prefixing your code):

```php
<?php

namespace Foo;

use function Foo\is_array;

is_array([]);

```

The situation is exactly the same for constants.


## Contributing

[Contribution Guide](CONTRIBUTING.md)
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,12 @@ function (string $filePath, string $prefix, string $contents): string {
//
// PHP-Parser patch
//

if ($filePath === __DIR__.'/vendor/nikic/php-parser/lib/PhpParser/Lexer.php') {
return preg_replace(
'%if \(defined\(\$name = \'PhpParser\\\\\\\\Parser\\\\\\\\Tokens%',
'if (defined($name = \''.$prefix.'\\\\\\\\PhpParser\\\\\\\\Parser\\\\\\\\Tokens',
$contents
);
}

if ($filePath === realpath(__DIR__.'/vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php')) {
$length = 15 + strlen($prefix) + 1;

return preg_replace(
'%rtrim\(get_class\(\$this\), \'_\'\), 15\)%',
sprintf('rtrim(get_class($this), \'_\'), %d)', $length),
'%strpos\((.+?)\) \+ 15%',
sprintf('strpos($1) + %d', $length),
$contents
);
}
Expand Down
20 changes: 10 additions & 10 deletions specs/class-FQ.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ class Foo
class Bar
{
}
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
namespace Humbug\Foo\Bar;

class Poz
{
}
class_alias('Humbug\\Foo\\Bar\\Poz', 'Foo\\Bar\\Poz', \false);
\class_alias('Humbug\\Foo\\Bar\\Poz', 'Foo\\Bar\\Poz', \false);
namespace Humbug;

use Humbug\Foo as X;
Expand Down Expand Up @@ -279,47 +279,47 @@ class Foo
class Bar
{
}
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
namespace Humbug\Foo\Bar;

class Poz
{
}
class_alias('Humbug\\Foo\\Bar\\Poz', 'Foo\\Bar\\Poz', \false);
\class_alias('Humbug\\Foo\\Bar\\Poz', 'Foo\\Bar\\Poz', \false);
namespace Humbug\A;

class Aoo
{
}
class_alias('Humbug\\A\\Aoo', 'A\\Aoo', \false);
\class_alias('Humbug\\A\\Aoo', 'A\\Aoo', \false);
class Foo
{
}
class_alias('Humbug\\A\\Foo', 'A\\Foo', \false);
\class_alias('Humbug\\A\\Foo', 'A\\Foo', \false);
namespace Humbug\A\Foo;

class Bar
{
}
class_alias('Humbug\\A\\Foo\\Bar', 'A\\Foo\\Bar', \false);
\class_alias('Humbug\\A\\Foo\\Bar', 'A\\Foo\\Bar', \false);
namespace Humbug\A\Foo\Bar;

class Poz
{
}
class_alias('Humbug\\A\\Foo\\Bar\\Poz', 'A\\Foo\\Bar\\Poz', \false);
\class_alias('Humbug\\A\\Foo\\Bar\\Poz', 'A\\Foo\\Bar\\Poz', \false);
namespace Humbug\A\Aoo;

class Aoz
{
}
class_alias('Humbug\\A\\Aoo\\Aoz', 'A\\Aoo\\Aoz', \false);
\class_alias('Humbug\\A\\Aoo\\Aoz', 'A\\Aoo\\Aoz', \false);
namespace Humbug\A\Aoo\Aoz;

class Poz
{
}
class_alias('Humbug\\A\\Aoo\\Aoz\\Poz', 'A\\Aoo\\Aoz\\Poz', \false);
\class_alias('Humbug\\A\\Aoo\\Aoz\\Poz', 'A\\Aoo\\Aoz\\Poz', \false);
namespace Humbug\A;

use Humbug\Foo as X;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class Foo
class Bar
{
}
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
namespace Humbug;

use Humbug\Foo as X;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class Foo
class Bar
{
}
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
namespace Humbug;

use Humbug\Foo;
Expand Down Expand Up @@ -235,7 +235,7 @@ class Foo
class Bar
{
}
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
namespace Humbug;

use Humbug\Foo;
Expand Down
4 changes: 2 additions & 2 deletions specs/class-const/global-scope-two-level.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Command {}
class Command
{
}
class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
\class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
namespace Humbug;

\Humbug\PHPUnit\Command::MAIN_CONST;
Expand Down Expand Up @@ -144,7 +144,7 @@ class Command {}
class Command
{
}
class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
\class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
namespace Humbug;

\Humbug\PHPUnit\Command::MAIN_CONST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class Foo
class Bar
{
}
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
namespace Humbug\A;

use Humbug\Foo as X;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class Foo
class Bar
{
}
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
namespace Humbug\X;

use Humbug\Foo;
Expand Down Expand Up @@ -240,7 +240,7 @@ class Foo
class Bar
{
}
class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
\class_alias('Humbug\\Foo\\Bar', 'Foo\\Bar', \false);
namespace Humbug\X;

use Humbug\Foo;
Expand Down
4 changes: 2 additions & 2 deletions specs/class-const/namespace-scope-two-level.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Command {}
class Command
{
}
class_alias('Humbug\\X\\PHPUnit\\Command', 'X\\PHPUnit\\Command', \false);
\class_alias('Humbug\\X\\PHPUnit\\Command', 'X\\PHPUnit\\Command', \false);
namespace Humbug\X;

\Humbug\X\PHPUnit\Command::MAIN_CONST;
Expand Down Expand Up @@ -148,7 +148,7 @@ class Command {}
class Command
{
}
class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
\class_alias('Humbug\\PHPUnit\\Command', 'PHPUnit\\Command', \false);
namespace Humbug\X;

\Humbug\PHPUnit\Command::MAIN_CONST;
Expand Down
6 changes: 3 additions & 3 deletions specs/class/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function a()
}
public abstract function b();
}
class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);
\class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);

PHP
],
Expand Down Expand Up @@ -154,7 +154,7 @@ public function a()
{
}
}
class_alias('Humbug\\Foo\\WA', 'Foo\\WA', \false);
\class_alias('Humbug\\Foo\\WA', 'Foo\\WA', \false);
namespace Humbug\Bar;

abstract class B
Expand All @@ -169,7 +169,7 @@ public function b()
{
}
}
class_alias('Humbug\\Bar\\WB', 'Bar\\WB', \false);
\class_alias('Humbug\\Bar\\WB', 'Bar\\WB', \false);
namespace Humbug;

abstract class C
Expand Down
10 changes: 5 additions & 5 deletions specs/class/conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class A {}

namespace Humbug\Foo;

if (true) {
if (\true) {
class A
{
}
Expand All @@ -107,7 +107,7 @@ class A {}

namespace Humbug\Foo;

if (true) {
if (\true) {
class A
{
}
Expand Down Expand Up @@ -141,21 +141,21 @@ class C {}

namespace Humbug\X;

if (true) {
if (\true) {
class A
{
}
}
namespace Humbug\Y;

if (true) {
if (\true) {
class B
{
}
}
namespace Humbug\Z;

if (true) {
if (\true) {
class C
{
}
Expand Down
2 changes: 1 addition & 1 deletion specs/class/final.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class A {}
final class A
{
}
class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);
\class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);

PHP
],
Expand Down
2 changes: 1 addition & 1 deletion specs/class/interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ interface A extends \Humbug\Foo\C, \Humbug\Foo\D, \Iterator
{
public function a();
}
class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);
\class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);

PHP
],
Expand Down
2 changes: 1 addition & 1 deletion specs/class/regular-extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function a()
class B extends \Humbug\Foo\A
{
}
class_alias('Humbug\\Foo\\B', 'Foo\\B', \false);
\class_alias('Humbug\\Foo\\B', 'Foo\\B', \false);

PHP
],
Expand Down
6 changes: 3 additions & 3 deletions specs/class/regular.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function a()
{
}
}
class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);
\class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);

PHP
],
Expand Down Expand Up @@ -207,7 +207,7 @@ public function a()
{
}
}
class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);
\class_alias('Humbug\\Foo\\A', 'Foo\\A', \false);
class B
{
public function b()
Expand All @@ -234,7 +234,7 @@ public function b()
{
}
}
class_alias('Humbug\\Bar\\B', 'Bar\\B', \false);
\class_alias('Humbug\\Bar\\B', 'Bar\\B', \false);
class C
{
public function c()
Expand Down
Loading