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

View File

@ -17,71 +17,72 @@ use Fenom;
*/ */
class Render extends \ArrayObject class Render extends \ArrayObject
{ {
private static $_props = array( private static array $_props = [
"name" => "runtime", "name" => "runtime",
"base_name" => "", "base_name" => "",
"scm" => false, "scm" => false,
"time" => 0, "time" => 0,
"depends" => array(), "depends" => [],
"macros" => array() "macros" => []
); ];
/** /**
* @var \Closure * @var \Closure
*/ */
protected $_code; protected \Closure $_code;
/** /**
* Template name * Template name
* @var string * @var string
*/ */
protected $_name = 'runtime'; protected mixed $_name = 'runtime';
/** /**
* Provider's schema * Provider's schema
* @var bool * @var string|null
*/ */
protected $_scm = false; protected ?string $_scm;
/** /**
* Basic template name * Basic template name
* @var string * @var string
*/ */
protected $_base_name = 'runtime'; protected string $_base_name = 'runtime';
/** /**
* @var Fenom * @var Fenom
*/ */
protected $_fenom; protected Fenom $_fenom;
/** /**
* Timestamp of compilation * Timestamp of compilation
* @var float * @var float
*/ */
protected $_time = 0.0; protected float $_time = 0.0;
/** /**
* @var array depends list * @var array depends list
*/ */
protected $_depends = array(); protected array $_depends = [];
/** /**
* @var int template options (see Fenom options) * @var int template options (see Fenom options)
*/ */
protected $_options = 0; protected int $_options = 0;
/** /**
* Template provider * Template provider
* @var ProviderInterface * @var ProviderInterface
*/ */
protected $_provider; protected ProviderInterface $_provider;
/** /**
* @var \Closure[] * @var \Closure[]
*/ */
protected $_macros; protected array $_macros;
/** /**
* @param Fenom $fenom * @param Fenom $fenom
* @param callable $code template body * @param \Closure $code template body
* @param array $props * @param array $props
*/ */
public function __construct(Fenom $fenom, \Closure $code, array $props = array()) public function __construct(Fenom $fenom, \Closure $code, array $props = array())
{ {
parent::__construct();
$this->_fenom = $fenom; $this->_fenom = $fenom;
$props += self::$_props; $props += self::$_props;
$this->_name = $props["name"]; $this->_name = $props["name"];
@ -97,25 +98,25 @@ class Render extends \ArrayObject
* Get template storage * Get template storage
* @return \Fenom * @return \Fenom
*/ */
public function getStorage() public function getStorage(): Fenom
{ {
return $this->_fenom; return $this->_fenom;
} }
/** /**
* Get depends list * Get list of dependencies.
* @return array * @return array
*/ */
public function getDepends() public function getDepends(): array
{ {
return $this->_depends; return $this->_depends;
} }
/** /**
* Get schema name * Get schema name
* @return string * @return string|null
*/ */
public function getScm() public function getScm(): ?string
{ {
return $this->_scm; return $this->_scm;
} }
@ -124,7 +125,7 @@ class Render extends \ArrayObject
* Get provider of template source * Get provider of template source
* @return ProviderInterface * @return ProviderInterface
*/ */
public function getProvider() public function getProvider(): ProviderInterface
{ {
return $this->_fenom->getProvider($this->_scm); return $this->_fenom->getProvider($this->_scm);
} }
@ -133,7 +134,7 @@ class Render extends \ArrayObject
* Get name without schema * Get name without schema
* @return string * @return string
*/ */
public function getBaseName() public function getBaseName(): string
{ {
return $this->_base_name; return $this->_base_name;
} }
@ -142,7 +143,7 @@ class Render extends \ArrayObject
* Get parse options * Get parse options
* @return int * @return int
*/ */
public function getOptions() public function getOptions(): int
{ {
return $this->_options; return $this->_options;
} }
@ -159,7 +160,7 @@ class Render extends \ArrayObject
* Get template name * Get template name
* @return string * @return string
*/ */
public function getName() public function getName(): string
{ {
return $this->_name; return $this->_name;
} }
@ -174,7 +175,7 @@ class Render extends \ArrayObject
* Validate template * Validate template
* @return bool * @return bool
*/ */
public function isValid() public function isValid(): bool
{ {
foreach ($this->_depends as $scm => $templates) { foreach ($this->_depends as $scm => $templates) {
$provider = $this->_fenom->getProvider($scm); $provider = $this->_fenom->getProvider($scm);
@ -197,7 +198,7 @@ class Render extends \ArrayObject
* @throws \RuntimeException * @throws \RuntimeException
* @return mixed * @return mixed
*/ */
public function getMacro($name) public function getMacro($name): mixed
{ {
if (empty($this->_macros[$name])) { if (empty($this->_macros[$name])) {
throw new \RuntimeException('macro ' . $name . ' not found'); throw new \RuntimeException('macro ' . $name . ' not found');
@ -208,9 +209,9 @@ class Render extends \ArrayObject
/** /**
* Execute template and write into output * Execute template and write into output
* @param array $values for template * @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); $this->_code->__invoke($values, $this);
return $values; return $values;
@ -222,7 +223,7 @@ class Render extends \ArrayObject
* @return string * @return string
* @throws \Exception * @throws \Exception
*/ */
public function fetch(array $values) public function fetch(array $values): string
{ {
ob_start(); ob_start();
try { try {
@ -236,17 +237,12 @@ class Render extends \ArrayObject
/** /**
* Stub * Stub
* @param $method * @param string $method
* @param $args * @param mixed $args
* @throws \BadMethodCallException * @throws \BadMethodCallException
*/ */
public function __call($method, $args) public function __call(string $method, mixed $args)
{ {
throw new \BadMethodCallException("Unknown method " . $method); 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 * @var Template
*/ */
public $tpl; public Template $tpl;
public $name; public string $name;
public $options = array(); public array $options = [];
public $line = 0; public int $line = 0;
public $level = 0; public int $level = 0;
public $callback; public mixed $callback;
public $escape; public bool $escape;
private $_offset = 0; private int $_offset = 0;
private $_closed = true; private bool $_closed = true;
private $_body; private string $_body;
private $_type = 0; private int $_type = 0;
private $_open; private string $_open;
private $_close; private string $_close;
private $_tags = array(); private array $_tags = [];
private $_floats = array(); private array $_floats = [];
private $_changed = array(); private array $_changed = [];
/** /**
* Create tag entity * Create tag entity
* @param string $name the tag name * @param string $name the tag name
* @param Template $tpl current template * @param Template $tpl current template
* @param string $info tag's information * @param array $info tag's information
* @param string $body template's code * @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->tpl = $tpl;
$this->name = $name; $this->name = $name;
$this->line = $tpl->getLine(); $this->line = $tpl->getLine();
@ -62,8 +63,8 @@ class Tag extends \ArrayObject
if ($this->_type & self::BLOCK) { if ($this->_type & self::BLOCK) {
$this->_open = $info["open"]; $this->_open = $info["open"];
$this->_close = $info["close"]; $this->_close = $info["close"];
$this->_tags = isset($info["tags"]) ? $info["tags"] : array(); $this->_tags = $info["tags"] ?? [];
$this->_floats = isset($info["float_tags"]) ? $info["float_tags"] : array(); $this->_floats = $info["float_tags"] ?? [];
$this->_closed = false; $this->_closed = false;
} else { } else {
$this->_open = $info["parser"]; $this->_open = $info["parser"];
@ -79,7 +80,7 @@ class Tag extends \ArrayObject
* @param string $option * @param string $option
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function tagOption($option) public function tagOption(string $option)
{ {
if (method_exists($this, 'opt' . $option)) { if (method_exists($this, 'opt' . $option)) {
$this->options[] = $option; $this->options[] = $option;
@ -93,7 +94,7 @@ class Tag extends \ArrayObject
* @param int $option option constant * @param int $option option constant
* @param bool $value true add option, false remove option * @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); $actual = (bool)($this->tpl->getOptions() & $option);
if ($actual != $value) { if ($actual != $value) {
@ -106,7 +107,7 @@ class Tag extends \ArrayObject
* Restore the option * Restore the option
* @param int $option * @param int $option
*/ */
public function restore($option) public function restore(int $option)
{ {
if (isset($this->_changed[$option])) { if (isset($this->_changed[$option])) {
$this->tpl->setOption($option, $this->_changed[$option]); $this->tpl->setOption($option, $this->_changed[$option]);
@ -126,7 +127,7 @@ class Tag extends \ArrayObject
* Check, if the tag closed * Check, if the tag closed
* @return bool * @return bool
*/ */
public function isClosed() public function isClosed(): bool
{ {
return $this->_closed; return $this->_closed;
} }
@ -137,7 +138,7 @@ class Tag extends \ArrayObject
* @param Tokenizer $tokenizer * @param Tokenizer $tokenizer
* @return mixed * @return mixed
*/ */
public function start($tokenizer) public function start(Tokenizer $tokenizer): mixed
{ {
foreach ($this->options as $option) { foreach ($this->options as $option) {
$option = 'opt' . $option; $option = 'opt' . $option;
@ -153,7 +154,7 @@ class Tag extends \ArrayObject
* @param int $level * @param int $level
* @return bool * @return bool
*/ */
public function hasTag($tag, $level) public function hasTag(string $tag, int $level): bool
{ {
if (isset($this->_tags[$tag])) { if (isset($this->_tags[$tag])) {
if ($level) { if ($level) {
@ -171,10 +172,10 @@ class Tag extends \ArrayObject
* *
* @param string $tag * @param string $tag
* @param Tokenizer $tokenizer * @param Tokenizer $tokenizer
* @throws \LogicException
* @return string * @return string
* @throws \LogicException
*/ */
public function tag($tag, $tokenizer) public function tag(string $tag, Tokenizer $tokenizer): string
{ {
if (isset($this->_tags[$tag])) { if (isset($this->_tags[$tag])) {
return call_user_func($this->_tags[$tag], $tokenizer, $this); return call_user_func($this->_tags[$tag], $tokenizer, $this);
@ -187,10 +188,10 @@ class Tag extends \ArrayObject
* Close callback * Close callback
* *
* @param Tokenizer $tokenizer * @param Tokenizer $tokenizer
* @throws \LogicException
* @return string * @return string
* @throws \LogicException
*/ */
public function end($tokenizer) public function end(Tokenizer $tokenizer): string
{ {
if ($this->_closed) { if ($this->_closed) {
throw new \LogicException("Tag {$this->name} already closed"); throw new \LogicException("Tag {$this->name} already closed");
@ -224,7 +225,7 @@ class Tag extends \ArrayObject
* @throws \LogicException * @throws \LogicException
* @return string * @return string
*/ */
public function getContent() public function getContent(): string
{ {
return substr($this->_body, $this->_offset); return substr($this->_body, $this->_offset);
} }
@ -235,7 +236,7 @@ class Tag extends \ArrayObject
* @return string * @return string
* @throws \LogicException * @throws \LogicException
*/ */
public function cutContent() public function cutContent(): string
{ {
$content = substr($this->_body, $this->_offset); $content = substr($this->_body, $this->_offset);
$this->_body = substr($this->_body, 0, $this->_offset); $this->_body = substr($this->_body, 0, $this->_offset);
@ -258,7 +259,7 @@ class Tag extends \ArrayObject
* @param string $code * @param string $code
* @return string * @return string
*/ */
public function out($code) public function out(string $code): string
{ {
return $this->tpl->out($code, $this->escape); return $this->tpl->out($code, $this->escape);
} }