Skip to content

Commit

Permalink
update for PHP7
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Mar 9, 2019
1 parent e993fc6 commit 765e7e7
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 100 deletions.
1 change: 0 additions & 1 deletion core/boot/class/Patchwork/Bootstrapper/Manager.php
Expand Up @@ -277,7 +277,6 @@ protected function initAutoloader()
{
function_exists('__autoload') && $this->substeps[] = array("spl_autoload_register('__autoload');", __FILE__);

$this->substeps[] = array($this->shim('__autoload', 'spl_autoload_call', array('$class')), __FILE__);
$this->substeps[] = array('function patchwork_include() {return include func_get_arg(0);}', __FILE__);
}

Expand Down
2 changes: 1 addition & 1 deletion core/logger/class/Patchwork/PHP/Dumper.php
Expand Up @@ -246,7 +246,7 @@ protected function walkHash($type, &$a, $len)

foreach ($a as &$k)
{
switch ($this->gettype($k))
switch (gettype($k))
{
case 'object':
if (! $len)
Expand Down
8 changes: 4 additions & 4 deletions core/logger/class/Patchwork/PHP/InDepthErrorHandler.php
Expand Up @@ -173,7 +173,7 @@ static function unstackErrors()
/**
* Static version of ->handleException().
*/
static function handleCaughtException($type, $message, $file, $line, &$context, $log_time = 0)
static function handleCaughtException($type, $message, $file, $line, $context, $log_time = 0)
{
return self::$handler->handleException($type, $message, $file, $line, $context, $log_time);
}
Expand All @@ -198,7 +198,7 @@ function __construct($logger = null, $error_levels = array())
* @param int $trace_offset The number of noisy items to skip from the current trace or -1 to disable any trace logging.
* @param float $log_time The microtime(true) when the event has been triggered.
*/
function handleError($type, $message, $file, $line, &$context, $trace_offset = 0, $log_time = 0)
function handleError($type, $message, $file, $line, $context, $trace_offset = 0, $log_time = 0)
{
if (isset(self::$caughtToStringException))
{
Expand Down Expand Up @@ -274,7 +274,7 @@ function handleError($type, $message, $file, $line, &$context, $trace_offset = 0
* @param \Exception $e The exception to log.
* @param float $log_time The microtime(true) when the event has been triggered.
*/
function handleUncaughtException(\Exception $e, $log_time = 0)
function handleUncaughtException($e, $log_time = 0)
{
$context = compact('e');
$s = $this->scopedErrors;
Expand All @@ -288,7 +288,7 @@ function handleUncaughtException(\Exception $e, $log_time = 0)
*
* @param string $message Message must match the format 'Caught \Exception $e'
*/
function handleException($type, $message, $file, $line, &$context, $log_time = 0)
function handleException($type, $message, $file, $line, $context, $log_time = 0)
{
$log = $this->loggedErrors & $type & error_reporting();

Expand Down
16 changes: 7 additions & 9 deletions core/logger/class/Patchwork/PHP/JsonDumpIterator.php
Expand Up @@ -102,7 +102,7 @@ function next()
}
else
{
user_error('Invalid JsonDump stream', E_USER_WARNING);
user_error('Invalid JsonDump stream: '.$line, E_USER_WARNING);
}
}
}
Expand Down Expand Up @@ -167,16 +167,12 @@ protected function parsePhpLine($line, $next_line, $offset)
{
$line = array(' "trace": {');
}
else
elseif (preg_match("' +(\d+)\. (.+?)\((.*)\) (.*)$'", $line, $m))
{
// TODO: more extensive parsing of dumped arguments using token_get_all() / client-side parsing?
preg_match("' +(\d+)\. (.+?)\((.*)\) (.*)$'", $line, $line);

$line = array(
' "' . $line[1] . '": {',
' "call": ' . $this->jsonStr("{$line[2]}() {$line[4]}") . ('' !== $line[3] ? ',' : ''),
'' !== $line[3] ? ' "args": ' . $this->jsonStr($line[3]) : null,
' "' . $m[1] . '": {',
' "call": ' . $this->jsonStr("{$m[2]}() {$m[4]}") . ('' !== $m[3] ? ',' : ''),
'' !== $m[3] ? ' "args": ' . $this->jsonStr($m[3]) : null,
' }'
);

Expand All @@ -188,6 +184,8 @@ protected function parsePhpLine($line, $next_line, $offset)
{
$line[count($line) - 1] .= ',';
}
} else {
$line = array($line);
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/logger/class/Patchwork/PHP/ThrowingErrorHandler.php
Expand Up @@ -42,7 +42,7 @@ static function register($handler = null)
* triggers an E_RECOVERABLE_ERROR that is caught by ->handleError()
* who rethrows the $e Exception.
*/
static function handleToStringException(\Exception $e)
static function handleToStringException($e)
{
self::$caughtToStringException = $e;
return null;
Expand All @@ -60,7 +60,7 @@ function __construct($thrown = null)
/**
* Turns errors to ErrorExceptions.
*/
function handleError($type, $message, $file, $line, &$context)
function handleError($type, $message, $file, $line, $context)
{
if (isset(self::$caughtToStringException))
{
Expand Down
29 changes: 5 additions & 24 deletions core/logger/class/Patchwork/PHP/Walker.php
Expand Up @@ -79,7 +79,7 @@ protected function walkRef(&$a)
{
++$this->counter;

if ('array' === $t = $this->gettype($a))
if ('array' === $t = gettype($a))
{
return $this->walkArray($a);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ protected function walkArray(&$a)
}

$c = $a['ref_counter'];
$t = $this->gettype($this->valPool[$c]);
$t = gettype($this->valPool[$c]);
$a =& $this->valPool[$c];
}

Expand Down Expand Up @@ -189,7 +189,7 @@ protected function cleanRefPools()

foreach ($this->refPool as $k => &$v)
{
if ('array' === $this->gettype($v) && isset($v[0], $v[self::$token]))
if ('array' === gettype($v) && isset($v[0], $v[self::$token]))
{
unset($v['ref_counter']);
array_splice($v, 0, 1);
Expand All @@ -206,25 +206,6 @@ protected function cleanRefPools()
return $refs;
}

// References friendly variants of native functions

protected function gettype(&$a)
{
if (\PHP_VERSION_ID >= 70000) {
return \gettype($a);
}

$this->lastErrorMessage = true;
$this->expectArray($a);
$msg = $this->lastErrorMessage;
$this->lastErrorMessage = false;

if (true === $msg) return 'array';

$i = strpos($msg, 'array, ') + 7;
return substr($msg, $i, strpos($msg, ' given', $i) - $i);
}

protected function count(&$a)
{
$len = 0;
Expand All @@ -236,12 +217,12 @@ private function expectArray(array &$a)
{
}

function handleError($type, $msg, $file, $line, &$context)
function handleError($type, $msg, $file, $line, $context)
{
if (true !== $this->lastErrorMessage)
{
if (null === $this->prevErrorHandler) return false;
else return call_user_func_array($this->prevErrorHandler, array($type, $msg, $file, $line, &$context));
else return call_user_func_array($this->prevErrorHandler, array($type, $msg, $file, $line, $context));
}

$this->lastErrorMessage = $msg;
Expand Down
2 changes: 1 addition & 1 deletion core/superloader/class/Patchwork/Preprocessor.php
Expand Up @@ -89,7 +89,7 @@ static function execute($source, $destination, $level, $class, $is_top, $lazy)

$tmpPool = array();

while (list($source, list($destination, $level, $class, $is_top)) = each($pool))
foreach ($pool as $source => list($destination, $level, $class, $is_top))
{
$preproc = new self;

Expand Down
4 changes: 2 additions & 2 deletions pieces/agent-controller/class/Patchwork.php
Expand Up @@ -21,7 +21,7 @@ function jsquote($a)
/**/}

if (is_object($a)) $a = $a->__toString();
if ((string) $a === (string) ($a-0)) return $a-0;
if (is_numeric($a) && (string) $a === (string) ($a-0)) return $a-0;

$a = (string) $a;

Expand Down Expand Up @@ -1115,7 +1115,7 @@ static function ob_filterOutput($buffer, $mode)
{
self::$ob_clean && $buffer = self::$ob_clean = '';

$one_chunk = $mode === (PHP_OUTPUT_HANDLER_START | PHP_OUTPUT_HANDLER_END);
$one_chunk = (PHP_OUTPUT_HANDLER_START & $mode) && (PHP_OUTPUT_HANDLER_END & $mode);

static $type = false;
false !== $type || $type = isset(self::$headers['content-type']) ? strtolower(substr(self::$headers['content-type'], 14)) : 'html';
Expand Down
6 changes: 4 additions & 2 deletions pieces/agent-controller/class/Patchwork/Serverside.php
Expand Up @@ -76,8 +76,10 @@ static function loadAgent($agent, $args, $is_exo)

if (is_array($a))
{
reset($a);
while (list($k, $v) = each($a)) $args[$k] = is_string($v) ? htmlspecialchars($v) : $v;
foreach ($a as $k => $v)
{
$args[$k] = is_string($v) ? htmlspecialchars($v) : $v;
}
}

self::$get = (object) $args;
Expand Down
2 changes: 1 addition & 1 deletion pieces/agent-controller/class/SESSION.php
Expand Up @@ -189,7 +189,7 @@ static function __init()
$i = $adapter->read();
$j = max(self::$maxIdleTime, self::$maxLifeTime);

if ($j && $_SERVER['REQUEST_TIME'] - $i > $j)
if ($j && @($_SERVER['REQUEST_TIME'] - $i) > $j)
{
$adapter->write($_SERVER['REQUEST_TIME']);
register_shutdown_function(array(__CLASS__, 'gc'), $j);
Expand Down
25 changes: 20 additions & 5 deletions pieces/agent-controller/class/loop/altLang.php
Expand Up @@ -11,7 +11,7 @@

class loop_altLang extends loop
{
protected $lang, $alt;
protected $lang, $generator, $alt, $count;

protected static $nativeLang = array(
'fr' => 'Français',
Expand Down Expand Up @@ -55,17 +55,32 @@ protected function prepare()
}
else user_error('Something is wrong between Patchwork::__URI__() and PATCHWORK_BASE');

$this->alt =& $a;
$this->count = count($a);

$this->generator = function () use ($a) {
yield;
foreach ($a as $v) {
yield $v;
}
};
$this->alt = ($this->generator)();
}

return count($this->alt);
return $this->count;
}
else return 0;
}

protected function next()
{
if (list(, $a) = each($this->alt)) return $a;
else reset($this->alt);
$this->alt->next();

if (!$this->alt->valid()) {
$this->alt = ($this->generator)();

return;
}

return $this->alt->current();
}
}
34 changes: 26 additions & 8 deletions pieces/agent-controller/class/loop/array.php
Expand Up @@ -13,30 +13,48 @@ class loop_array extends loop
{
protected

$generator,
$array,
$count,
$isAssociative = true;


function __construct($array, $filter = '', $isAssociative = null)
{
$this->count = count($array);
reset($array);
$this->array = $array;
if ($filter) $this->addFilter($filter);
$this->isAssociative = $isAssociative!==null ? $isAssociative : $filter!==false;

$this->generator = function () use ($array) {
yield;

foreach ($array as $k => $v) {
yield $k => $v;
}
};
$this->array = ($this->generator)();
}

protected function prepare() {return count($this->array);}
protected function prepare() {return $this->count;}

protected function next()
{
if (list($key, $value) = each($this->array))
{
$data = array('VALUE' => &$value);
if ($this->isAssociative) $data['KEY'] =& $key;
$this->array->next();

if (!$this->array->valid()) {
$this->array = ($this->generator)();

return (object) $data;
return;
}
else reset($this->array);

$key = $this->array->key();
$value = $this->array->current();

$data = array('VALUE' => &$value);
if ($this->isAssociative) $data['KEY'] =& $key;

return (object) $data;
}
}

Expand Down
10 changes: 8 additions & 2 deletions pieces/agent-controller/class/ptlCompiler.php
Expand Up @@ -488,8 +488,14 @@ protected function compileBlock(&$a)
set_error_handler('var_dump', 0);
$len = error_reporting(81);

if (false === eval("($testCode);") && $i = error_get_last())
user_error("PTL parse error: {$i['message']} on line " . $this->getLine());
try {
if (false === eval("($testCode);") && $i = error_get_last())
user_error("PTL parse error: {$i['message']} on line " . $this->getLine());
} catch (DivisionByZeroError $e) {
// ignore
} catch (Error $e) {
user_error("PTL error: {$e->getMessage()} on line " . $this->getLine());
}

error_reporting($len);
restore_error_handler();
Expand Down
6 changes: 3 additions & 3 deletions pieces/agent-controller/class/ptlCompiler/js.php
Expand Up @@ -268,7 +268,7 @@ protected function addELSE($limit)

protected function getEcho($str)
{
if ("'" === $str[0] || (string) $str === (string) ($str-0))
if ("'" === $str[0] || (string) $str === (string) @($str-0))
{
if ("''" !== $str) array_push($this->jscode, self::OP_ECHO, $str);
}
Expand Down Expand Up @@ -300,7 +300,7 @@ protected function getRawString($str)

protected function getVar($name, $type, $prefix, $forceType)
{
if ((string) $name === (string) ($name-0)) return $name;
if ((string) $name === (string) @($name-0)) return $name;

switch ($type)
{
Expand Down Expand Up @@ -329,7 +329,7 @@ protected function getVar($name, $type, $prefix, $forceType)

switch ($forceType)
{
case 'number' : $result = "'" === $result[0] ? ($result-0) : "num($result)" ; break;
case 'number' : $result = "'" === $result[0] ? @($result-0) : "num($result)" ; break;
case 'unified': $result = "'" === $result[0] ? $result : "num($result,1)"; break;
default: if ('concat' === $this->mode && "'" !== $result[0]) $result = "str($result)";
}
Expand Down
2 changes: 1 addition & 1 deletion pieces/agent-controller/class/ptlCompiler/php.php
Expand Up @@ -189,7 +189,7 @@ protected function getRawString($str)

protected function getVar($name, $type, $prefix, $forceType)
{
if ((string) $name === (string) ($name-0)) return $name . '"\'"\'o';
if ((string) $name === (string) @($name-0)) return $name . '"\'"\'o';

switch ($type)
{
Expand Down
2 changes: 1 addition & 1 deletion pieces/jQuery
Submodule jQuery updated 1 files
+2 −2 class/jquery.php

0 comments on commit 765e7e7

Please sign in to comment.