Skip to content

Commit

Permalink
Merge pull request #90 from Slamdunk/php_82
Browse files Browse the repository at this point in the history
Add PHP 8.2 support, drop PHP 7.4
  • Loading branch information
Ocramius authored Sep 16, 2022
2 parents efddaab + 2622cad commit 1836dce
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 520 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
- "highest"
- "locked"
php-version:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
operating-system:
- "ubuntu-latest"

Expand Down
1 change: 1 addition & 0 deletions classes/PHPTAL/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Holds template variables and carries state/scope across macro executions.
*
*/
#[AllowDynamicProperties]
class PHPTAL_Context
{
public $repeat;
Expand Down
11 changes: 8 additions & 3 deletions classes/PHPTAL/Dom/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ class PHPTAL_Dom_Element extends PHPTAL_Dom_Node
public $childNodes = array();
public $parentNode;

private array $talHandlers;

/**
* @param string $qname qualified name of the element, e.g. "tal:block"
* @param string $namespace_uri namespace of this element
* @param array $attribute_nodes array of PHPTAL_Dom_Attr elements
* @param object $xmlns object that represents namespaces/prefixes known in element's context
* @param PHPTAL_Dom_XmlnsState $xmlns object that represents namespaces/prefixes known in element's context
*/
public function __construct($qname, $namespace_uri, array $attribute_nodes, PHPTAL_Dom_XmlnsState $xmlns)
public function __construct(
$qname,
$namespace_uri,
array $attribute_nodes,
private PHPTAL_Dom_XmlnsState $xmlns)
{
$this->qualifiedName = $qname;
$this->attribute_nodes = $attribute_nodes;
$this->namespace_uri = $namespace_uri;
$this->xmlns = $xmlns;

// implements inheritance of element's namespace to tal attributes (<metal: use-macro>)
foreach ($attribute_nodes as $index => $attr) {
Expand Down
1 change: 1 addition & 0 deletions classes/PHPTAL/Dom/PHPTALDocumentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PHPTAL_Dom_PHPTALDocumentBuilder extends PHPTAL_Dom_DocumentBuilder
{
private $_xmlns; /* PHPTAL_Dom_XmlnsState */
private $encoding;
private ?PHPTAL_Dom_Element $documentElement = null;

public function __construct()
{
Expand Down
21 changes: 9 additions & 12 deletions classes/PHPTAL/Dom/SaxXmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,9 @@ private function checkEncoding($str)
$forbid = '/((?>[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F]+))/s';

if (preg_match($forbid, $str)) {
$str = preg_replace_callback($forbid, array('self', 'convertBytesToEntities'), $str);
$str = preg_replace_callback($forbid, static function (array $m): string {
return self::convertBytesToEntities($m);
}, $str);
$this->raiseError("Invalid ISO-8859-1 characters: ".$str);
}
}
Expand All @@ -419,10 +421,8 @@ private function checkEncoding($str)
* Changes all bytes to hexadecimal XML entities
*
* @param array $m first array element is used for input
*
* @return string
*/
private static function convertBytesToEntities(array $m)
private static function convertBytesToEntities(array $m): string
{
$m = $m[1];
$out = "";
Expand All @@ -444,21 +444,18 @@ private function sanitizeEscapedText($str)
so they have to be converted into special TALES expression
*/
$types = version_compare(PHP_VERSION, '5.4.0') < 0 ? (ini_get('short_open_tag') ? 'php|=|' : 'php') : 'php|=';
$str = preg_replace_callback("/<\?($types)(.*?)\?>/", array('self', 'convertPHPBlockToTALES'), $str);
$str = preg_replace_callback("/<\?($types)(.*?)\?>/", static function ($m) {
list(, $type, $code) = $m;
if ($type === '=') $code = 'echo '.$code;
return '${structure phptal-internal-php-block:'.rawurlencode($code).'}';
}, $str);

// corrects all non-entities and neutralizes potentially problematic CDATA end marker
$str = strtr(preg_replace('/&(?!(?:#x?[a-f0-9]+|[a-z][a-z0-9]*);)/i', '&amp;', $str), array('<'=>'&lt;', ']]>'=>']]&gt;'));

return $str;
}

private static function convertPHPBlockToTALES($m)
{
list(, $type, $code) = $m;
if ($type === '=') $code = 'echo '.$code;
return '${structure phptal-internal-php-block:'.rawurlencode($code).'}';
}

public function getSourceFile()
{
return $this->_file;
Expand Down
5 changes: 4 additions & 1 deletion classes/PHPTAL/Php/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ protected function doEchoAttribute(PHPTAL_Php_CodeWriter $codewriter, $code)
$codewriter->doEchoRaw($code);
}

protected function parseSetExpression($exp)
/**
* @return non-empty-array<array-key, null|string>
*/
protected function parseSetExpression(string $exp)
{
$exp = trim($exp);
// (dest) (value)
Expand Down
10 changes: 6 additions & 4 deletions classes/PHPTAL/Php/Attribute/TAL/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class PHPTAL_Php_Attribute_TAL_Attributes
* value for default keyword
*/
private $_default_escaped;
private string $_attribute = '';
private string $_attkey = '';

public function before(PHPTAL_Php_CodeWriter $codewriter)
{
Expand All @@ -59,7 +61,7 @@ public function before(PHPTAL_Php_CodeWriter $codewriter)
}
}

private function prepareAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $expression)
private function prepareAttribute(PHPTAL_Php_CodeWriter $codewriter, string $qname, $expression)
{
$tales_code = $this->extractEchoType($expression);
$code = $codewriter->evaluateExpression($tales_code);
Expand Down Expand Up @@ -127,7 +129,7 @@ private function prepareAttributeConditional(PHPTAL_Php_CodeWriter $codewriter,
$this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($attkey);
}

private function prepareChainedAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $chain)
private function prepareChainedAttribute(PHPTAL_Php_CodeWriter $codewriter, string $qname, $chain)
{
$this->_default_escaped = false;
$this->_attribute = $qname;
Expand All @@ -139,7 +141,7 @@ private function prepareChainedAttribute(PHPTAL_Php_CodeWriter $codewriter, $qna
$this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($this->_attkey);
}

private function prepareBooleanAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $code)
private function prepareBooleanAttribute(PHPTAL_Php_CodeWriter $codewriter, string $qname, $code)
{
$attkey = $this->getVarName($qname, $codewriter);

Expand All @@ -156,7 +158,7 @@ private function prepareBooleanAttribute(PHPTAL_Php_CodeWriter $codewriter, $qna
$this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($attkey);
}

private function getVarName($qname, PHPTAL_Php_CodeWriter $codewriter)
private function getVarName($qname, PHPTAL_Php_CodeWriter $codewriter): string
{
$var = $codewriter->createTempVariable();
$this->vars_to_recycle[] = $var;
Expand Down
44 changes: 21 additions & 23 deletions classes/PHPTAL/Php/TalesChainExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ class PHPTAL_Php_TalesChainExecutor
const CHAIN_BREAK = 1;
const CHAIN_CONT = 2;

public function __construct(PHPTAL_Php_CodeWriter $codewriter, array $chain, PHPTAL_Php_TalesChainReader $reader)
private int $state = 0;
private bool $chainStarted = false;

public function __construct(
private PHPTAL_Php_CodeWriter $codewriter,
private array $chain,
private PHPTAL_Php_TalesChainReader $reader
)
{
$this->_chain = $chain;
$this->_chainStarted = false;
$this->codewriter = $codewriter;
$this->_reader = $reader;
$this->_executeChain();
}

Expand All @@ -37,10 +40,10 @@ public function getCodeWriter()
return $this->codewriter;
}

public function doIf($condition)
public function doIf(string $condition)
{
if ($this->_chainStarted == false) {
$this->_chainStarted = true;
if ($this->chainStarted == false) {
$this->chainStarted = true;
$this->codewriter->doIf($condition);
} else {
$this->codewriter->doElseIf($condition);
Expand All @@ -54,43 +57,38 @@ public function doElse()

public function breakChain()
{
$this->_state = self::CHAIN_BREAK;
$this->state = self::CHAIN_BREAK;
}

public function continueChain()
{
$this->_state = self::CHAIN_CONT;
$this->state = self::CHAIN_CONT;
}

private function _executeChain()
{
$this->codewriter->noThrow(true);

end($this->_chain); $lastkey = key($this->_chain);
end($this->chain); $lastkey = key($this->chain);

foreach ($this->_chain as $key => $exp) {
$this->_state = 0;
foreach ($this->chain as $key => $exp) {
$this->state = 0;

if ($exp == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) {
$this->_reader->talesChainNothingKeyword($this);
$this->reader->talesChainNothingKeyword($this);
} elseif ($exp == PHPTAL_Php_TalesInternal::DEFAULT_KEYWORD) {
$this->_reader->talesChainDefaultKeyword($this);
$this->reader->talesChainDefaultKeyword($this);
} else {
$this->_reader->talesChainPart($this, $exp, $lastkey === $key);
$this->reader->talesChainPart($this, $exp, $lastkey === $key);
}

if ($this->_state == self::CHAIN_BREAK)
if ($this->state == self::CHAIN_BREAK)
break;
if ($this->_state == self::CHAIN_CONT)
if ($this->state == self::CHAIN_CONT)
continue;
}

$this->codewriter->doEnd('if');
$this->codewriter->noThrow(false);
}

private $_state = 0;
private $_chain;
private $_chainStarted = false;
private $codewriter = null;
}
1 change: 1 addition & 0 deletions classes/PHPTAL/RepeatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class PHPTAL_RepeatController implements Iterator
* computed lazily
*/
private $length = null;
private PHPTAL_RepeatControllerGroups $groups;

/**
* Construct a new RepeatController.
Expand Down
4 changes: 3 additions & 1 deletion classes/PHPTAL/StringSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
class PHPTAL_StringSource implements PHPTAL_Source
{
const NO_PATH_PREFIX = '<string ';
private string $_data;
private string $_realpath;

public function __construct($data, $realpath = null)
public function __construct(string $data, ?string $realpath = null)
{
$this->_data = $data;
$this->_realpath = $realpath ? $realpath : self::NO_PATH_PREFIX.md5($data).'>';
Expand Down
12 changes: 9 additions & 3 deletions classes/PHPTAL/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@

class PHPTAL_Tokenizer
{
private $regex, $names, $offset, $str;
private string $regex;
private array $names;
private int $offset;
private string $str;

private $current_token, $current_value;
private ?string $current_token = null;
private ?string $current_value = null;

function __construct($str, array $tokens)
private int $end;

function __construct(string $str, array $tokens)
{
$this->offset = 0;
$this->str = $str;
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
}
],
"require": {
"php": "~7.4.0 || ~8.0.0 || ~8.1.0"
"php": "~8.0.0 || ~8.1.0 || ~8.2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5.10"
"phpunit/phpunit": "^9.5.24"
},
"autoload": {
"psr-0": {
Expand Down
Loading

0 comments on commit 1836dce

Please sign in to comment.