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

Cleanup/#7095 php 5.3 support end #4

Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 4 additions & 7 deletions library/Zend/Escaper/Escaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ class Escaper

/**
* Holds the value of the special flags passed as second parameter to
* htmlspecialchars(). We modify these for PHP 5.4 to take advantage
* of the new ENT_SUBSTITUTE flag for correctly dealing with invalid
* UTF-8 sequences.
* htmlspecialchars().
*
* @var string
*/
protected $htmlSpecialCharsFlags = ENT_QUOTES;
protected $htmlSpecialCharsFlags;

/**
* Static Matcher which escapes characters for HTML Attribute contexts
Expand Down Expand Up @@ -116,9 +114,8 @@ public function __construct($encoding = null)
$this->encoding = $encoding;
}

if (defined('ENT_SUBSTITUTE')) {
$this->htmlSpecialCharsFlags|= ENT_SUBSTITUTE;
}
// We take advantage of ENT_SUBSTITUTE flag to correctly deal with invalid UTF-8 sequences.
$this->htmlSpecialCharsFlags = ENT_QUOTES | ENT_SUBSTITUTE;

// set matcher callbacks
$this->htmlAttrMatcher = array($this, 'htmlAttrMatcher');
Expand Down
35 changes: 9 additions & 26 deletions library/Zend/Stdlib/CallbackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ class CallbackHandler
/**
* PHP version is greater as 5.4rc1?
* @var bool
*
* @deprecated since 2.4
*/
protected static $isPhp54;
protected static $isPhp54 = true;

/**
* Constructor
Expand Down Expand Up @@ -85,14 +87,9 @@ public function call(array $args = array())
{
$callback = $this->getCallback();

// Minor performance tweak, if the callback gets called more than once
if (!isset(static::$isPhp54)) {
static::$isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>=');
}

$argCount = count($args);

if (static::$isPhp54 && is_string($callback)) {
if (is_string($callback)) {
$result = $this->validateStringCallbackFor54($callback);

if ($result !== true && $argCount <= 3) {
Expand All @@ -103,34 +100,22 @@ public function call(array $args = array())
}
}

// Minor performance tweak; use call_user_func() until > 3 arguments
// Minor performance tweak; use call_user_func_array() with > 3 arguments
// reached
switch ($argCount) {
case 0:
if (static::$isPhp54) {
return $callback();
}
return call_user_func($callback);
return $callback();
case 1:
if (static::$isPhp54) {
return $callback(array_shift($args));
}
return call_user_func($callback, array_shift($args));
return $callback(array_shift($args));
case 2:
$arg1 = array_shift($args);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be $args[0] and $args[1] instead?

$arg2 = array_shift($args);
if (static::$isPhp54) {
return $callback($arg1, $arg2);
}
return call_user_func($callback, $arg1, $arg2);
return $callback($arg1, $arg2);
case 3:
$arg1 = array_shift($args);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$arg2 = array_shift($args);
$arg3 = array_shift($args);
if (static::$isPhp54) {
return $callback($arg1, $arg2, $arg3);
}
return call_user_func($callback, $arg1, $arg2, $arg3);
return $callback($arg1, $arg2, $arg3);
default:
return call_user_func_array($callback, $args);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call_user_func_array() is actually faster than multiple array_shift() and argument counting: we could directly use it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, that would bring the whole method back to its state before 6f18994

i got ridden of the shifts, it should be the right compromise now

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But is there any real benefit of using a switch condition? call_user_func_array() was always avoided because of its slowness, but as of PHP 5.4 it should be much faster, and in PHP 7 it's even reduced to a single opcode

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm let's wait for some benchmarks and then re-evaluate. for the time being, we're just leaving it a little bit better than before :)

}
Expand Down Expand Up @@ -173,8 +158,6 @@ public function getMetadatum($name)
/**
* Validate a static method call
*
* Validates that a static method call in PHP 5.4 will actually work
*
* @param string $callback
* @return true|array
* @throws Exception\InvalidCallbackException if invalid
Expand Down