migrate to php8

This commit is contained in:
ivan shalganov 2023-02-05 22:18:18 +01:00
parent 84dac62a85
commit 4cdfd306d9
3 changed files with 81 additions and 82 deletions

View File

@ -2,15 +2,17 @@
namespace Fenom;
use Countable;
use Iterator;
class RangeIterator implements \Iterator, \Countable
class RangeIterator implements Iterator, Countable
{
public $current;
public $index = 0;
public $min;
public $max;
public $step;
public int $current;
public int $index = 0;
public int $min;
public int $max;
public int $step;
public function __construct($min, $max, $step = 1)
{
@ -23,7 +25,7 @@ class RangeIterator implements \Iterator, \Countable
* @param int $step
* @return $this
*/
public function setStep($step)
public function setStep(int $step): static
{
if($step > 0) {
$this->current = min($this->min, $this->max);
@ -40,7 +42,7 @@ class RangeIterator implements \Iterator, \Countable
/**
* Return the current element
*/
public function current()
public function current(): int
{
return $this->current;
}
@ -58,7 +60,7 @@ class RangeIterator implements \Iterator, \Countable
* Return the key of the current element
* @return int
*/
public function key()
public function key(): int
{
return $this->index;
}
@ -67,7 +69,7 @@ class RangeIterator implements \Iterator, \Countable
* Checks if current position is valid
* @return bool
*/
public function valid()
public function valid(): bool
{
return $this->current >= $this->min && $this->current <= $this->max;
}
@ -88,7 +90,7 @@ class RangeIterator implements \Iterator, \Countable
/**
* Count elements of an object
*/
public function count()
public function count(): int
{
return intval(($this->max - $this->min + 1) / $this->step);
}

View File

@ -17,71 +17,72 @@ use Fenom;
*/
class Render extends \ArrayObject
{
private static $_props = array(
private static array $_props = [
"name" => "runtime",
"base_name" => "",
"scm" => false,
"time" => 0,
"depends" => array(),
"macros" => array()
);
"depends" => [],
"macros" => []
];
/**
* @var \Closure
*/
protected $_code;
protected \Closure $_code;
/**
* Template name
* @var string
*/
protected $_name = 'runtime';
protected mixed $_name = 'runtime';
/**
* Provider's schema
* @var bool
* @var string|null
*/
protected $_scm = false;
protected ?string $_scm;
/**
* Basic template name
* @var string
*/
protected $_base_name = 'runtime';
protected string $_base_name = 'runtime';
/**
* @var Fenom
*/
protected $_fenom;
protected Fenom $_fenom;
/**
* Timestamp of compilation
* @var float
*/
protected $_time = 0.0;
protected float $_time = 0.0;
/**
* @var array depends list
*/
protected $_depends = array();
protected array $_depends = [];
/**
* @var int template options (see Fenom options)
*/
protected $_options = 0;
protected int $_options = 0;
/**
* Template provider
* @var ProviderInterface
*/
protected $_provider;
protected ProviderInterface $_provider;
/**
* @var \Closure[]
*/
protected $_macros;
protected array $_macros;
/**
* @param Fenom $fenom
* @param callable $code template body
* @param \Closure $code template body
* @param array $props
*/
public function __construct(Fenom $fenom, \Closure $code, array $props = array())
{
parent::__construct();
$this->_fenom = $fenom;
$props += self::$_props;
$this->_name = $props["name"];
@ -97,25 +98,25 @@ class Render extends \ArrayObject
* Get template storage
* @return \Fenom
*/
public function getStorage()
public function getStorage(): Fenom
{
return $this->_fenom;
}
/**
* Get depends list
* Get list of dependencies.
* @return array
*/
public function getDepends()
public function getDepends(): array
{
return $this->_depends;
}
/**
* Get schema name
* @return string
* @return string|null
*/
public function getScm()
public function getScm(): ?string
{
return $this->_scm;
}
@ -124,7 +125,7 @@ class Render extends \ArrayObject
* Get provider of template source
* @return ProviderInterface
*/
public function getProvider()
public function getProvider(): ProviderInterface
{
return $this->_fenom->getProvider($this->_scm);
}
@ -133,7 +134,7 @@ class Render extends \ArrayObject
* Get name without schema
* @return string
*/
public function getBaseName()
public function getBaseName(): string
{
return $this->_base_name;
}
@ -142,7 +143,7 @@ class Render extends \ArrayObject
* Get parse options
* @return int
*/
public function getOptions()
public function getOptions(): int
{
return $this->_options;
}
@ -159,7 +160,7 @@ class Render extends \ArrayObject
* Get template name
* @return string
*/
public function getName()
public function getName(): string
{
return $this->_name;
}
@ -174,7 +175,7 @@ class Render extends \ArrayObject
* Validate template
* @return bool
*/
public function isValid()
public function isValid(): bool
{
foreach ($this->_depends as $scm => $templates) {
$provider = $this->_fenom->getProvider($scm);
@ -197,7 +198,7 @@ class Render extends \ArrayObject
* @throws \RuntimeException
* @return mixed
*/
public function getMacro($name)
public function getMacro($name): mixed
{
if (empty($this->_macros[$name])) {
throw new \RuntimeException('macro ' . $name . ' not found');
@ -208,9 +209,9 @@ class Render extends \ArrayObject
/**
* Execute template and write into output
* @param array $values for template
* @return Render
* @return array
*/
public function display(array $values)
public function display(array $values): array
{
$this->_code->__invoke($values, $this);
return $values;
@ -222,7 +223,7 @@ class Render extends \ArrayObject
* @return string
* @throws \Exception
*/
public function fetch(array $values)
public function fetch(array $values): string
{
ob_start();
try {
@ -236,17 +237,12 @@ class Render extends \ArrayObject
/**
* Stub
* @param $method
* @param $args
* @param string $method
* @param mixed $args
* @throws \BadMethodCallException
*/
public function __call($method, $args)
public function __call(string $method, mixed $args)
{
throw new \BadMethodCallException("Unknown method " . $method);
}
public function __get($name)
{
return $this->$name = null;
}
}

View File

@ -23,33 +23,34 @@ class Tag extends \ArrayObject
/**
* @var Template
*/
public $tpl;
public $name;
public $options = array();
public $line = 0;
public $level = 0;
public $callback;
public $escape;
public Template $tpl;
public string $name;
public array $options = [];
public int $line = 0;
public int $level = 0;
public mixed $callback;
public bool $escape;
private $_offset = 0;
private $_closed = true;
private $_body;
private $_type = 0;
private $_open;
private $_close;
private $_tags = array();
private $_floats = array();
private $_changed = array();
private int $_offset = 0;
private bool $_closed = true;
private string $_body;
private int $_type = 0;
private string $_open;
private string $_close;
private array $_tags = [];
private array $_floats = [];
private array $_changed = [];
/**
* Create tag entity
* @param string $name the tag name
* @param Template $tpl current template
* @param string $info tag's information
* @param array $info tag's information
* @param string $body template's code
*/
public function __construct($name, Template $tpl, $info, &$body)
public function __construct(string $name, Template $tpl, array $info, string &$body)
{
parent::__construct();
$this->tpl = $tpl;
$this->name = $name;
$this->line = $tpl->getLine();
@ -62,8 +63,8 @@ class Tag extends \ArrayObject
if ($this->_type & self::BLOCK) {
$this->_open = $info["open"];
$this->_close = $info["close"];
$this->_tags = isset($info["tags"]) ? $info["tags"] : array();
$this->_floats = isset($info["float_tags"]) ? $info["float_tags"] : array();
$this->_tags = $info["tags"] ?? [];
$this->_floats = $info["float_tags"] ?? [];
$this->_closed = false;
} else {
$this->_open = $info["parser"];
@ -79,7 +80,7 @@ class Tag extends \ArrayObject
* @param string $option
* @throws \RuntimeException
*/
public function tagOption($option)
public function tagOption(string $option)
{
if (method_exists($this, 'opt' . $option)) {
$this->options[] = $option;
@ -93,7 +94,7 @@ class Tag extends \ArrayObject
* @param int $option option constant
* @param bool $value true add option, false remove option
*/
public function setOption($option, $value)
public function setOption(int $option, bool $value)
{
$actual = (bool)($this->tpl->getOptions() & $option);
if ($actual != $value) {
@ -106,7 +107,7 @@ class Tag extends \ArrayObject
* Restore the option
* @param int $option
*/
public function restore($option)
public function restore(int $option)
{
if (isset($this->_changed[$option])) {
$this->tpl->setOption($option, $this->_changed[$option]);
@ -126,7 +127,7 @@ class Tag extends \ArrayObject
* Check, if the tag closed
* @return bool
*/
public function isClosed()
public function isClosed(): bool
{
return $this->_closed;
}
@ -137,7 +138,7 @@ class Tag extends \ArrayObject
* @param Tokenizer $tokenizer
* @return mixed
*/
public function start($tokenizer)
public function start(Tokenizer $tokenizer): mixed
{
foreach ($this->options as $option) {
$option = 'opt' . $option;
@ -153,7 +154,7 @@ class Tag extends \ArrayObject
* @param int $level
* @return bool
*/
public function hasTag($tag, $level)
public function hasTag(string $tag, int $level): bool
{
if (isset($this->_tags[$tag])) {
if ($level) {
@ -171,10 +172,10 @@ class Tag extends \ArrayObject
*
* @param string $tag
* @param Tokenizer $tokenizer
* @throws \LogicException
* @return string
* @throws \LogicException
*/
public function tag($tag, $tokenizer)
public function tag(string $tag, Tokenizer $tokenizer): string
{
if (isset($this->_tags[$tag])) {
return call_user_func($this->_tags[$tag], $tokenizer, $this);
@ -187,10 +188,10 @@ class Tag extends \ArrayObject
* Close callback
*
* @param Tokenizer $tokenizer
* @throws \LogicException
* @return string
* @throws \LogicException
*/
public function end($tokenizer)
public function end(Tokenizer $tokenizer): string
{
if ($this->_closed) {
throw new \LogicException("Tag {$this->name} already closed");
@ -224,7 +225,7 @@ class Tag extends \ArrayObject
* @throws \LogicException
* @return string
*/
public function getContent()
public function getContent(): string
{
return substr($this->_body, $this->_offset);
}
@ -235,7 +236,7 @@ class Tag extends \ArrayObject
* @return string
* @throws \LogicException
*/
public function cutContent()
public function cutContent(): string
{
$content = substr($this->_body, $this->_offset);
$this->_body = substr($this->_body, 0, $this->_offset);
@ -258,7 +259,7 @@ class Tag extends \ArrayObject
* @param string $code
* @return string
*/
public function out($code)
public function out(string $code): string
{
return $this->tpl->out($code, $this->escape);
}