Skip to content

Commit

Permalink
Prefix certain global functions so PHP can optimize them (see #1103).
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Sep 28, 2017
1 parent 3dadd44 commit bdd7bb0
Show file tree
Hide file tree
Showing 24 changed files with 57 additions and 58 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"contao/manager-plugin": "^2.0",
"doctrine/doctrine-migrations-bundle": "^1.1",
"doctrine/orm": "^2.5",
"friendsofphp/php-cs-fixer": "^2.0",
"friendsofphp/php-cs-fixer": "^2.6",
"lexik/maintenance-bundle": "^2.0",
"monolog/monolog": "^1.22",
"phpunit/phpunit": "^6.1",
Expand Down
4 changes: 2 additions & 2 deletions src/Command/SymlinksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private function filterNestedPaths(Finder $finder, string $prepend): array
$path = rtrim(strtr($prepend.'/'.$file->getRelativePath(), '\\', '/'), '/');

if (!empty($parents)) {
$parent = dirname($path);
$parent = \dirname($path);

while (false !== strpos($parent, '/')) {
if (\in_array($parent, $parents, true)) {
Expand All @@ -271,7 +271,7 @@ private function filterNestedPaths(Finder $finder, string $prepend): array
break;
}

$parent = dirname($parent);
$parent = \dirname($parent);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function generateRandomSecret(Event $event): void
return;
}

if (!function_exists('random_bytes')) {
if (!\function_exists('random_bytes')) {
self::loadRandomCompat($event);
}

Expand Down
2 changes: 1 addition & 1 deletion src/DataContainer/PaletteManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ private function applyFieldToField(array &$config, array $action, bool $skipLege
*/
private function applyFallback(array &$config, array $action, bool $skipLegends = false): void
{
if (is_callable($action['fallback'])) {
if (\is_callable($action['fallback'])) {
$action['fallback']($config, $action, $skipLegends);
} else {
$this->applyFallbackPalette($config, $action);
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/AddResourcesPathsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private function getResourcesPathFromClassName($class): ?string
{
$reflection = new \ReflectionClass($class);

if (is_dir($dir = dirname($reflection->getFileName()).'/Resources/contao')) {
if (is_dir($dir = \dirname($reflection->getFileName()).'/Resources/contao')) {
return $dir;
}

Expand Down
20 changes: 10 additions & 10 deletions src/Framework/ContaoFramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,25 +185,25 @@ public function getAdapter($class): Adapter
private function setConstants(): void
{
if (!\defined('TL_MODE')) {
define('TL_MODE', $this->getMode());
\define('TL_MODE', $this->getMode());
}

define('TL_START', microtime(true));
define('TL_ROOT', $this->rootDir);
define('TL_REFERER_ID', $this->getRefererId());
\define('TL_START', microtime(true));
\define('TL_ROOT', $this->rootDir);
\define('TL_REFERER_ID', $this->getRefererId());

if (!\defined('TL_SCRIPT')) {
define('TL_SCRIPT', $this->getRoute());
\define('TL_SCRIPT', $this->getRoute());
}

// Define the login status constants in the back end (see #4099, #5279)
if (null === $this->request || !$this->scopeMatcher->isFrontendRequest($this->request)) {
define('BE_USER_LOGGED_IN', false);
define('FE_USER_LOGGED_IN', false);
\define('BE_USER_LOGGED_IN', false);
\define('FE_USER_LOGGED_IN', false);
}

// Define the relative path to the installation (see #5339)
define('TL_PATH', $this->getPath());
\define('TL_PATH', $this->getPath());
}

/**
Expand Down Expand Up @@ -435,7 +435,7 @@ private function handleRequestToken(): void

// Deprecated since Contao 4.0, to be removed in Contao 5.0
if (!\defined('REQUEST_TOKEN')) {
define('REQUEST_TOKEN', 'cli' === PHP_SAPI ? null : $requestToken->get());
\define('REQUEST_TOKEN', 'cli' === PHP_SAPI ? null : $requestToken->get());
}

if ($this->canSkipTokenCheck() || $requestToken->validate($this->request->request->get('REQUEST_TOKEN'))) {
Expand All @@ -453,7 +453,7 @@ private function handleRequestToken(): void
*/
private function iniSet(string $key, string $value): void
{
if (function_exists('ini_set')) {
if (\function_exists('ini_set')) {
ini_set($key, $value);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/HttpKernel/Bundle/ContaoModuleBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class ContaoModuleBundle extends Bundle
public function __construct(string $name, string $rootDir)
{
$this->name = $name;
$this->path = dirname($rootDir).'/system/modules/'.$this->name;
$this->path = \dirname($rootDir).'/system/modules/'.$this->name;

if (!is_dir($this->path)) {
throw new \LogicException(sprintf('The module folder "system/modules/%s" does not exist.', $this->name));
Expand Down
4 changes: 2 additions & 2 deletions src/Picker/PickerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function urlEncode(): string
{
$data = json_encode($this);

if (function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
if (\function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
$data = $encoded;
}

Expand All @@ -165,7 +165,7 @@ public static function urlDecode(string $data): PickerConfig
{
$data = base64_decode(strtr($data, '-_,', '+/='), true);

if (function_exists('gzdecode') && false !== ($uncompressed = @gzdecode($data))) {
if (\function_exists('gzdecode') && false !== ($uncompressed = @gzdecode($data))) {
$data = $uncompressed;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Util/SymlinkUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function symlink(string $target, string $link, string $rootDir): v
if ('\\' === DIRECTORY_SEPARATOR) {
$fs->symlink($target, $link);
} else {
$fs->symlink(rtrim($fs->makePathRelative($target, dirname($link)), '/'), $link);
$fs->symlink(rtrim($fs->makePathRelative($target, \dirname($link)), '/'), $link);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Command/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function setUp(): void
$tcpdfPath = $this->getRootDir().'/vendor/contao/core-bundle/src/Resources/contao/config/tcpdf.php';

if (!file_exists($tcpdfPath)) {
if (!file_exists(dirname($tcpdfPath))) {
mkdir(dirname($tcpdfPath), 0777, true);
if (!file_exists(\dirname($tcpdfPath))) {
mkdir(\dirname($tcpdfPath), 0777, true);
}

file_put_contents($tcpdfPath, '');
Expand Down
9 changes: 4 additions & 5 deletions tests/Contao/CombinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ protected function setUp(): void
{
parent::setUp();

define('TL_ERROR', 'ERROR');
define('TL_ROOT', self::$rootDir);
define('TL_ASSETS_URL', '');
\define('TL_ERROR', 'ERROR');
\define('TL_ROOT', self::$rootDir);
\define('TL_ASSETS_URL', '');

$this->container = $this->mockContainerWithContaoScopes();
$this->container->setParameter('contao.web_dir', self::$rootDir.'/web');
Expand Down Expand Up @@ -191,7 +191,7 @@ public function testHandlesSpecialCharactersWhileFixingTheFilePaths(): void

$this->assertSame(
$expected,
$method->invokeArgs($class->newInstance(), [$css, ['name' => "web/(test)/file.css"]])
$method->invokeArgs($class->newInstance(), [$css, ['name' => 'web/(test)/file.css']])
);
}

Expand Down Expand Up @@ -232,7 +232,6 @@ public function testCombinesScssFiles(): void

$combinedFile = $combiner->getCombinedFile();


$this->assertSame(
"body{color:red}\nbody{color:green}\n",
file_get_contents(static::$rootDir.'/'.$combinedFile)
Expand Down
2 changes: 1 addition & 1 deletion tests/Contao/GdImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function setUp(): void
{
parent::setUp();

define('TL_ROOT', self::$rootDir);
\define('TL_ROOT', self::$rootDir);
System::setContainer($this->mockContainerWithContaoScopes());
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Contao/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

self::$rootDir = dirname(dirname(__DIR__)).'/tmp';
self::$rootDir = \dirname(\dirname(__DIR__)).'/tmp';

$fs = new Filesystem();
$fs->mkdir(self::$rootDir);
Expand Down Expand Up @@ -78,8 +78,8 @@ protected function setUp(): void
$GLOBALS['TL_CONFIG']['gdMaxImgHeight'] = 3000;
$GLOBALS['TL_CONFIG']['validImageTypes'] = 'jpeg,jpg,svg,svgz';

define('TL_ERROR', 'ERROR');
define('TL_ROOT', self::$rootDir);
\define('TL_ERROR', 'ERROR');
\define('TL_ROOT', self::$rootDir);

$container = $this->mockContainerWithContaoScopes();
$this->addImageServicesToContainer($container, self::$rootDir);
Expand Down
6 changes: 3 additions & 3 deletions tests/Contao/PictureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ protected function setUp(): void
$GLOBALS['TL_CONFIG']['gdMaxImgHeight'] = 3000;
$GLOBALS['TL_CONFIG']['validImageTypes'] = 'jpeg,jpg,svg,svgz';

define('TL_ERROR', 'ERROR');
define('TL_FILES_URL', 'http://example.com/');
define('TL_ROOT', self::$rootDir);
\define('TL_ERROR', 'ERROR');
\define('TL_FILES_URL', 'http://example.com/');
\define('TL_ROOT', self::$rootDir);

$container = $this->mockContainerWithContaoScopes();
$this->addImageServicesToContainer($container, self::$rootDir);
Expand Down
6 changes: 3 additions & 3 deletions tests/Contao/StringUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function setUpBeforeClass(): void
parent::setUpBeforeClass();

if (!\defined('TL_ERROR')) {
define('TL_ERROR', 'ERROR');
\define('TL_ERROR', 'ERROR');
}
}

Expand All @@ -41,7 +41,7 @@ protected function setUp(): void
parent::setUp();

if (!\defined('TL_ROOT')) {
define('TL_ROOT', $this->getRootDir());
\define('TL_ROOT', $this->getRootDir());
}

System::setContainer($this->mockContainerWithContaoScopes());
Expand Down Expand Up @@ -485,7 +485,7 @@ public function testFailsIfThePathIsTheParentFolder(): void
{
$this->expectException('InvalidArgumentException');

StringUtil::stripRootDir(dirname($this->getRootDir()).'/');
StringUtil::stripRootDir(\dirname($this->getRootDir()).'/');
}

public function testFailsIfThePathDoesNotMatch(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Contao/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ protected function setUp(): void
$fs = new Filesystem();
$fs->mkdir($this->getRootDir().'/templates');

define('TL_ROOT', $this->getRootDir());
define('TL_MODE', 'BE');
\define('TL_ROOT', $this->getRootDir());
\define('TL_MODE', 'BE');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Contao/WidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

if (!function_exists('utf8_decode_entities')) {
if (!\function_exists('utf8_decode_entities')) {
include_once __DIR__.'/../../src/Resources/contao/helper/functions.php';
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Controller/InitializeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public function testCanBeInstantiated(): void
public function testReturnsAResponseInTheIndexActionMethod(): void
{
if (!\defined('TL_MODE')) {
define('TL_MODE', 'BE');
\define('TL_MODE', 'BE');
}

if (!\defined('TL_SCRIPT')) {
define('TL_SCRIPT', 'index.php');
\define('TL_SCRIPT', 'index.php');
}

$requestStack = new RequestStack();
Expand Down
18 changes: 9 additions & 9 deletions tests/Image/ImageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ function (ResizeConfiguration $config): bool {

$path = $this->getRootDir().'/assets/images/dummy.svg';

if (!file_exists(dirname($path))) {
mkdir(dirname($path), 0777, true);
if (!file_exists(\dirname($path))) {
mkdir(\dirname($path), 0777, true);
}

file_put_contents($path, '');
Expand Down Expand Up @@ -587,7 +587,7 @@ public function testIgnoresTheImportantPartIfItIsOutOfBounds(): void
*/
public function testExecutesTheExecuteResizeHook(): void
{
define('TL_ROOT', $this->getRootDir());
\define('TL_ROOT', $this->getRootDir());
$GLOBALS['TL_CONFIG']['validImageTypes'] = 'jpg';

$path = $this->getRootDir().'/images/dummy.jpg';
Expand Down Expand Up @@ -665,8 +665,8 @@ public static function executeResizeHookCallback(ContaoImage $imageObj): string
.'.jpg'
;

if (!file_exists(dirname(TL_ROOT.'/'.$path))) {
mkdir(dirname(TL_ROOT.'/'.$path), 0777, true);
if (!file_exists(\dirname(TL_ROOT.'/'.$path))) {
mkdir(\dirname(TL_ROOT.'/'.$path), 0777, true);
}

file_put_contents(TL_ROOT.'/'.$path, '');
Expand All @@ -680,7 +680,7 @@ public static function executeResizeHookCallback(ContaoImage $imageObj): string
*/
public function testExecutesTheGetImageHook(): void
{
define('TL_ROOT', $this->getRootDir());
\define('TL_ROOT', $this->getRootDir());
$GLOBALS['TL_CONFIG']['validImageTypes'] = 'jpg';

System::setContainer($this->mockContainerWithContaoScopes());
Expand Down Expand Up @@ -773,8 +773,8 @@ public static function getImageHookCallback(string $originalPath, int $targetWid
.'.jpg'
;

if (!file_exists(dirname(TL_ROOT.'/'.$path))) {
mkdir(dirname(TL_ROOT.'/'.$path), 0777, true);
if (!file_exists(\dirname(TL_ROOT.'/'.$path))) {
mkdir(\dirname(TL_ROOT.'/'.$path), 0777, true);
}

file_put_contents(TL_ROOT.'/'.$path, '');
Expand All @@ -788,7 +788,7 @@ public static function getImageHookCallback(string $originalPath, int $targetWid
*/
public function testIgnoresAnEmptyHookReturnValue(): void
{
define('TL_ROOT', $this->getRootDir());
\define('TL_ROOT', $this->getRootDir());
$GLOBALS['TL_CONFIG']['validImageTypes'] = 'jpg';

System::setContainer($this->mockContainerWithContaoScopes());
Expand Down
2 changes: 1 addition & 1 deletion tests/Picker/ArticlePickerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testCreatesTheMenuItem(): void
'value' => '',
]);

if (function_exists('gzencode') && false !== ($encoded = @gzencode($picker))) {
if (\function_exists('gzencode') && false !== ($encoded = @gzencode($picker))) {
$picker = $encoded;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Picker/FilePickerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function testCreatesTheMenuItem(): void
'value' => '',
]);

if (function_exists('gzencode') && false !== ($encoded = @gzencode($picker))) {
if (\function_exists('gzencode') && false !== ($encoded = @gzencode($picker))) {
$picker = $encoded;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Picker/PagePickerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testCreatesTheMenuItem(): void
'value' => '',
]);

if (function_exists('gzencode') && false !== ($encoded = @gzencode($picker))) {
if (\function_exists('gzencode') && false !== ($encoded = @gzencode($picker))) {
$picker = $encoded;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Picker/PickerConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testCreatesAnEncodedJsonString(): void
'value' => 'foo',
]);

if (function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
if (\function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
$data = $encoded;
}

Expand All @@ -104,7 +104,7 @@ public function testDecodesAnEncodedJsonString(): void
'value' => 'foo',
]);

if (function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
if (\function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
$data = $encoded;
}

Expand All @@ -121,7 +121,7 @@ public function testFailsToDecodeAnEncodedJsonStringIfTheJsonIsInvalid(): void
{
$data = '{"invalid';

if (function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
if (\function_exists('gzencode') && false !== ($encoded = @gzencode($data))) {
$data = $encoded;
}

Expand Down
Loading

0 comments on commit bdd7bb0

Please sign in to comment.