From a2b77aad54e742c0df77cef4cf157728373ae897 Mon Sep 17 00:00:00 2001 From: Gemorroj Date: Thu, 6 Jan 2022 23:05:26 +0300 Subject: [PATCH] fix: add type hints #4 --- src/Attributes.php | 42 ++------ src/Tag/A.php | 4 +- src/Tag/Abbr.php | 4 +- src/Tag/Acronym.php | 4 +- src/Tag/Address.php | 4 +- src/Tag/Align.php | 4 +- src/Tag/Altfont.php | 4 +- src/Tag/Bbcode.php | 4 +- src/Tag/Bdo.php | 4 +- src/Tag/Br.php | 4 +- src/Tag/Caption.php | 4 +- src/Tag/Code.php | 4 +- src/Tag/Color.php | 4 +- src/Tag/Font.php | 4 +- src/Tag/Hr.php | 4 +- src/Tag/Img.php | 4 +- src/Tag/Li.php | 4 +- src/Tag/Nobb.php | 4 +- src/Tag/Ol.php | 4 +- src/Tag/P.php | 4 +- src/Tag/Quote.php | 4 +- src/Tag/Simple.php | 4 +- src/Tag/Size.php | 4 +- src/Tag/Spoiler.php | 4 +- src/Tag/Table.php | 4 +- src/Tag/Tag.php | 93 ++++------------ src/Tag/Td.php | 4 +- src/Tag/Th.php | 4 +- src/Tag/Tr.php | 4 +- src/Tag/Ul.php | 4 +- src/Tag/Youtube.php | 4 +- src/Xbbcode.php | 252 ++++++++++---------------------------------- 32 files changed, 110 insertions(+), 393 deletions(-) diff --git a/src/Attributes.php b/src/Attributes.php index 9935a7f..5f241a8 100644 --- a/src/Attributes.php +++ b/src/Attributes.php @@ -28,40 +28,23 @@ class Attributes extends \ArrayObject { /** - * @var array + * @var array */ protected $attributes = ['class' => 'bb']; - /** - * @param string $name - * - * @return string - */ - public function getAttributeValue($name) + public function getAttributeValue(string $name): string { return $this->attributes[$name]; } - /** - * @param string $name - * @param string $value - * - * @return Attributes - */ - public function set($name, $value) + public function set(string $name, string $value): self { $this->attributes[$name] = $value; return $this; } - /** - * @param string $name - * @param string $value - * - * @return Attributes - */ - public function add($name, $value) + public function add(string $name, string $value): self { if (isset($this->attributes[$name])) { $this->attributes[$name] .= ' '.$value; @@ -72,30 +55,19 @@ public function add($name, $value) return $this; } - /** - * @param string $name - * - * @return Attributes - */ - public function remove($name) + public function remove(string $name): self { unset($this->attributes[$name]); return $this; } - /** - * @return \ArrayIterator - */ - public function getIterator() + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->attributes); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { $str = ''; foreach ($this->getIterator() as $name => $value) { diff --git a/src/Tag/A.php b/src/Tag/A.php index 57dae49..dc66a30 100644 --- a/src/Tag/A.php +++ b/src/Tag/A.php @@ -84,10 +84,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { $this->setAutoLinks(false); diff --git a/src/Tag/Abbr.php b/src/Tag/Abbr.php index 7c11e48..bb556c2 100644 --- a/src/Tag/Abbr.php +++ b/src/Tag/Abbr.php @@ -48,10 +48,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Acronym.php b/src/Tag/Acronym.php index e0d1121..17b0099 100644 --- a/src/Tag/Acronym.php +++ b/src/Tag/Acronym.php @@ -48,10 +48,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Address.php b/src/Tag/Address.php index 724b4f3..6994aa5 100644 --- a/src/Tag/Address.php +++ b/src/Tag/Address.php @@ -30,10 +30,8 @@ class Address extends Tag { /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return '
getAttributes().'>'.$this->getBody().'
'; } diff --git a/src/Tag/Align.php b/src/Tag/Align.php index 20c2ff0..87b0e0e 100644 --- a/src/Tag/Align.php +++ b/src/Tag/Align.php @@ -65,10 +65,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return '
getAttributes().'>'.$this->getBody().'
'; } diff --git a/src/Tag/Altfont.php b/src/Tag/Altfont.php index b57658f..7216ed9 100644 --- a/src/Tag/Altfont.php +++ b/src/Tag/Altfont.php @@ -56,10 +56,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Bbcode.php b/src/Tag/Bbcode.php index c607bad..f000716 100644 --- a/src/Tag/Bbcode.php +++ b/src/Tag/Bbcode.php @@ -54,10 +54,8 @@ protected function build() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { $this->build(); diff --git a/src/Tag/Bdo.php b/src/Tag/Bdo.php index d8028cd..4e20e8c 100644 --- a/src/Tag/Bdo.php +++ b/src/Tag/Bdo.php @@ -65,10 +65,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Br.php b/src/Tag/Br.php index b81f320..114b6a0 100644 --- a/src/Tag/Br.php +++ b/src/Tag/Br.php @@ -33,10 +33,8 @@ class Br extends Tag /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return '
getAttributes().' />'; } diff --git a/src/Tag/Caption.php b/src/Tag/Caption.php index d55e215..115b9a6 100644 --- a/src/Tag/Caption.php +++ b/src/Tag/Caption.php @@ -32,10 +32,8 @@ class Caption extends Tag /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Code.php b/src/Tag/Code.php index 4f1187e..ad4e12a 100644 --- a/src/Tag/Code.php +++ b/src/Tag/Code.php @@ -203,10 +203,8 @@ protected function getFooter() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { $this->setLanguage(); $this->setSource(); diff --git a/src/Tag/Color.php b/src/Tag/Color.php index da6c2c6..0fe42c1 100644 --- a/src/Tag/Color.php +++ b/src/Tag/Color.php @@ -48,10 +48,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Font.php b/src/Tag/Font.php index 93fcb22..10129e8 100644 --- a/src/Tag/Font.php +++ b/src/Tag/Font.php @@ -63,10 +63,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Hr.php b/src/Tag/Hr.php index cb79268..24a4a81 100644 --- a/src/Tag/Hr.php +++ b/src/Tag/Hr.php @@ -33,10 +33,8 @@ class Hr extends Tag /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return '
getAttributes().' />'; } diff --git a/src/Tag/Img.php b/src/Tag/Img.php index f6b7b52..4c9eb47 100644 --- a/src/Tag/Img.php +++ b/src/Tag/Img.php @@ -100,10 +100,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().' />'; } diff --git a/src/Tag/Li.php b/src/Tag/Li.php index b222a0a..f9a8bb9 100644 --- a/src/Tag/Li.php +++ b/src/Tag/Li.php @@ -50,10 +50,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return '
  • getAttributes().'>'.$this->getBody().'
  • '; } diff --git a/src/Tag/Nobb.php b/src/Tag/Nobb.php index 73001df..4d73841 100644 --- a/src/Tag/Nobb.php +++ b/src/Tag/Nobb.php @@ -32,10 +32,8 @@ class Nobb extends Tag /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return \htmlspecialchars($this->getTreeText(), \ENT_NOQUOTES); } diff --git a/src/Tag/Ol.php b/src/Tag/Ol.php index c515f91..1c0c2d5 100644 --- a/src/Tag/Ol.php +++ b/src/Tag/Ol.php @@ -56,10 +56,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return '
      getAttributes().'>'.$this->getBody().'
    '; } diff --git a/src/Tag/P.php b/src/Tag/P.php index c1f54d4..6c97045 100644 --- a/src/Tag/P.php +++ b/src/Tag/P.php @@ -32,10 +32,8 @@ class P extends Align /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return '<'.$this->getTagName().' '.$this->getAttributes().'>'.$this->getBody().'getTagName().'>'; } diff --git a/src/Tag/Quote.php b/src/Tag/Quote.php index ed0ff5c..c5fa662 100644 --- a/src/Tag/Quote.php +++ b/src/Tag/Quote.php @@ -68,10 +68,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return '
    getAttributes().'>'.$this->getAuthor().$this->getBody().'
    '; } diff --git a/src/Tag/Simple.php b/src/Tag/Simple.php index d7e1e84..a1e312c 100644 --- a/src/Tag/Simple.php +++ b/src/Tag/Simple.php @@ -32,10 +32,8 @@ class Simple extends Tag /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { switch ($this->getTagName()) { case 'b': diff --git a/src/Tag/Size.php b/src/Tag/Size.php index 24c63aa..965047a 100644 --- a/src/Tag/Size.php +++ b/src/Tag/Size.php @@ -50,10 +50,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Spoiler.php b/src/Tag/Spoiler.php index b8c05f5..261f96c 100644 --- a/src/Tag/Spoiler.php +++ b/src/Tag/Spoiler.php @@ -69,10 +69,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { $attr = $this->getAttributes(); $id = $attr->getAttributeValue('id'); diff --git a/src/Tag/Table.php b/src/Tag/Table.php index 032c3fc..16691d5 100644 --- a/src/Tag/Table.php +++ b/src/Tag/Table.php @@ -80,10 +80,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().'
    '; } diff --git a/src/Tag/Tag.php b/src/Tag/Tag.php index e1cfd6c..942e7e3 100644 --- a/src/Tag/Tag.php +++ b/src/Tag/Tag.php @@ -87,7 +87,7 @@ public function __construct() /** * @return string HTML code */ - abstract public function __toString(); + abstract public function __toString(): string; /** * @return Attributes Tag attributes @@ -97,12 +97,12 @@ protected function getAttributes() return new Attributes(); } - public function setAttributes(array $attributes) + public function setAttributes(array $attributes): void { $this->attributes = $attributes; } - protected function cleanTreeText() + protected function cleanTreeText(): void { foreach ($this->getTree() as $key => $item) { if ('text' === $item['type']) { @@ -111,10 +111,7 @@ protected function cleanTreeText() } } - /** - * @return string - */ - protected function getTreeText() + protected function getTreeText(): string { $text = ''; foreach ($this->getTree() as $val) { @@ -126,10 +123,7 @@ protected function getTreeText() return $text; } - /** - * @return string - */ - protected function getBody() + protected function getBody(): string { if (\in_array(static::BEHAVIOUR, ['table', 'tr', 'ul'])) { $this->cleanTreeText(); @@ -140,12 +134,8 @@ protected function getBody() /** * Функция преобразует строку URL в соответствии с RFC 3986. - * - * @param string $url - * - * @return string */ - protected function parseUrl($url) + protected function parseUrl(string $url): string { $parse = \parse_url($url); @@ -183,16 +173,13 @@ protected function parseUrl($url) /** * Аналог parse_str но без преобразования точек и пробелов в подчеркивания. - * - * @param string $str - * - * @return array */ - private function parseStr($str) + private function parseStr(string $str): array { $original = ['.', ' ']; $replace = ["xbbdot\txbbdot", "xbbspace\txbbspace"]; + $query = []; \parse_str(\str_replace($original, $replace, $str), $query); foreach ($query as $k => $v) { @@ -203,83 +190,43 @@ private function parseStr($str) return $query; } - /** - * @param string $size - * - * @return bool - */ - protected function isValidSize($size) + protected function isValidSize(string $size): bool { return (bool) \preg_match('/^[0-9]+(?:px|%)?$/i', $size); } - /** - * @param string $number - * - * @return bool - */ - protected function isValidNumber($number) + protected function isValidNumber(string $number): bool { return (bool) \preg_match('/^[0-9]+$/', $number); } - /** - * @param string $size - * - * @return bool - */ - protected function isValidFontSize($size) + protected function isValidFontSize(string $size): bool { return (bool) \preg_match('/^(?:\+|-)?(?:1|2|3|4|5|6|7){1}$/', $size); } - /** - * @param string $align - * - * @return bool - */ - protected function isValidAlign($align) + protected function isValidAlign(string $align): bool { - return \in_array(\strtolower($align), ['left', 'right', 'center', 'justify']); + return \in_array(\strtolower($align), ['left', 'right', 'center', 'justify'], true); } - /** - * @param string $valign - * - * @return bool - */ - protected function isValidValign($valign) + protected function isValidValign(string $valign): bool { - return \in_array(\strtolower($valign), ['top', 'middle', 'bottom', 'baseline']); + return \in_array(\strtolower($valign), ['top', 'middle', 'bottom', 'baseline'], true); } - /** - * @param string $ulType - * - * @return bool - */ - protected function isValidUlType($ulType) + protected function isValidUlType(string $ulType): bool { - return \in_array(\strtolower($ulType), ['disc', 'circle', 'square']); + return \in_array(\strtolower($ulType), ['disc', 'circle', 'square'], true); } - /** - * @param string $olType - * - * @return bool - */ - protected function isValidOlType($olType) + protected function isValidOlType(string $olType): bool { return (bool) \preg_match('/^[a-z0-9]+$/i', $olType); } - /** - * @param string $target - * - * @return bool - */ - protected function isValidTarget($target) + protected function isValidTarget(string $target): bool { - return \in_array(\strtolower($target), ['_blank', '_self', '_parent', '_top']); + return \in_array(\strtolower($target), ['_blank', '_self', '_parent', '_top'], true); } } diff --git a/src/Tag/Td.php b/src/Tag/Td.php index 466127f..1f0f04e 100644 --- a/src/Tag/Td.php +++ b/src/Tag/Td.php @@ -80,10 +80,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Th.php b/src/Tag/Th.php index f781487..740d64a 100644 --- a/src/Tag/Th.php +++ b/src/Tag/Th.php @@ -32,10 +32,8 @@ class Th extends Td /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Tr.php b/src/Tag/Tr.php index aef4ed4..299deb5 100644 --- a/src/Tag/Tr.php +++ b/src/Tag/Tr.php @@ -32,10 +32,8 @@ class Tr extends Tag /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return 'getAttributes().'>'.$this->getBody().''; } diff --git a/src/Tag/Ul.php b/src/Tag/Ul.php index 35b74b3..d322248 100644 --- a/src/Tag/Ul.php +++ b/src/Tag/Ul.php @@ -56,10 +56,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return ''; } diff --git a/src/Tag/Youtube.php b/src/Tag/Youtube.php index 365fd9b..8cb8d7a 100644 --- a/src/Tag/Youtube.php +++ b/src/Tag/Youtube.php @@ -88,10 +88,8 @@ protected function getAttributes() /** * Return html code. - * - * @return string */ - public function __toString() + public function __toString(): string { return ''; } diff --git a/src/Xbbcode.php b/src/Xbbcode.php index a88e7cd..7fb8268 100644 --- a/src/Xbbcode.php +++ b/src/Xbbcode.php @@ -57,7 +57,7 @@ class Xbbcode /** * Список поддерживаемых тегов с указанием специализированных классов. * - * @var string[] + * @var array */ protected $tags = [ // Основные теги @@ -398,7 +398,7 @@ class Xbbcode /** * Статистические сведения по обработке BBCode. * - * @var array + * @var array */ protected $statistics = [ 'time_parse' => 0, // Время парсинга @@ -536,7 +536,7 @@ class Xbbcode /** * Смайлы. * - * @var array + * @var array> */ protected $smiles = [ ':D' => [ @@ -940,10 +940,10 @@ class Xbbcode /** * Конструктор класса. * - * @param string $webPath Web path - * @param array $allowed Allowed tags + * @param string $webPath Web path + * @param array|null $allowed Allowed tags */ - public function __construct($webPath = '', array $allowed = null) + public function __construct(string $webPath = '', ?array $allowed = null) { $this->webPath = $webPath; $this->reloadSmiles(); @@ -1059,7 +1059,7 @@ protected function getToken() * * @param array|string $code */ - public function parse($code) + public function parse($code): void { $time_start = \microtime(true); @@ -1106,10 +1106,7 @@ public function parse($code) $this->statistics['time_parse'] = \microtime(true) - $time_start; } - /** - * @return array - */ - protected function finiteAutomaton() + protected function finiteAutomaton(): array { $finite_automaton = $this->finiteAutomaton; $mode = 0; @@ -1299,14 +1296,7 @@ protected function finiteAutomaton() ]; } - /** - * specialchars. - * - * @param string $str - * - * @return string - */ - protected function specialchars($str) + protected function specialchars(string $str): string { $chars = [ '[' => '@l;', @@ -1319,14 +1309,7 @@ protected function specialchars($str) return \strtr($str, $chars); } - /** - * unspecialchars. - * - * @param string $str - * - * @return string - */ - protected function unspecialchars($str) + protected function unspecialchars(string $str): string { $chars = [ '@l;' => '[', @@ -1339,121 +1322,73 @@ protected function unspecialchars($str) return \strtr($str, $chars); } - /** - * @param bool $autoLinks - * - * @return Xbbcode - */ - public function setAutoLinks($autoLinks = false) + public function setAutoLinks(bool $autoLinks = false): self { - $this->autoLinks = (bool) $autoLinks; + $this->autoLinks = $autoLinks; return $this; } - /** - * @return bool - */ - public function getAutoLinks() + public function getAutoLinks(): bool { return $this->autoLinks; } - /** - * @param bool $keywordLinks - * - * @return Xbbcode - */ - public function setKeywordLinks($keywordLinks = false) + public function setKeywordLinks(bool $keywordLinks = false): self { - $this->keywordLinks = (bool) $keywordLinks; + $this->keywordLinks = $keywordLinks; return $this; } - /** - * @return bool - */ - public function getKeywordLinks() + public function getKeywordLinks(): bool { return $this->keywordLinks; } - /** - * @param string $key - * @param string $name - * @param string $title - * - * @return Xbbcode - */ - public function setSmile($key, $name, $title = '') + public function setSmile(string $key, string $name, string $title = ''): self { $this->setMnemonic($key, ''.\htmlspecialchars($title).''); return $this; } - /** - * @param string $key - * - * @return Xbbcode - */ - public function removeMnemonic($key) + public function removeMnemonic(string $key): self { unset($this->mnemonics[$key]); return $this; } - /** - * @return array - */ - public function getMnemonics() + public function getMnemonics(): array { return $this->mnemonics; } - /** - * @return Xbbcode - */ - public function setMnemonics(array $mnemonics) + public function setMnemonics(array $mnemonics): self { $this->mnemonics = $mnemonics; return $this; } - /** - * @param string $key - * @param string $value - * - * @return Xbbcode - */ - public function setMnemonic($key, $value) + public function setMnemonic(string $key, string $value): self { $this->mnemonics[$key] = $value; return $this; } - /** - * @param bool $enableSmiles - * - * @return Xbbcode - */ - public function setEnableSmiles($enableSmiles = true) + public function setEnableSmiles(bool $enableSmiles = true): self { - $this->enableSmiles = (bool) $enableSmiles; + $this->enableSmiles = $enableSmiles; $this->reloadSmiles(); return $this; } - /** - * @return Xbbcode - */ - protected function reloadSmiles() + protected function reloadSmiles(): self { if ($this->getEnableSmiles()) { foreach ($this->getSmiles() as $key => $val) { @@ -1468,18 +1403,15 @@ protected function reloadSmiles() return $this; } - /** - * @return bool - */ - public function getEnableSmiles() + public function getEnableSmiles(): bool { return $this->enableSmiles; } /** - * @return Xbbcode + * @param array> $smiles */ - public function setSmiles(array $smiles) + public function setSmiles(array $smiles): self { $this->smiles = $smiles; @@ -1489,17 +1421,17 @@ public function setSmiles(array $smiles) } /** - * @return array + * @return array> */ - public function getSmiles() + public function getSmiles(): array { return $this->smiles; } /** - * @return Xbbcode + * @param array $tags */ - public function setTags(array $tags) + public function setTags(array $tags): self { $this->tags = $tags; @@ -1507,80 +1439,53 @@ public function setTags(array $tags) } /** - * @return array + * @return array */ - public function getTags() + public function getTags(): array { return $this->tags; } - /** - * @param string $tagName - * @param string $handler Имя класса отнаследованного от абстрактного Tag - * - * @return Xbbcode - */ - public function setTagHandler($tagName, $handler) + public function setTagHandler(string $tagName, string $handler): self { + if (!\is_subclass_of($handler, Tag::class, true)) { + throw new \RuntimeException(\sprintf('%s should implements the %s class', $handler, Tag::class)); + } $this->tags[$tagName] = $handler; return $this; } - /** - * @param string $tagName - * - * @return string - */ - public function getTagHandler($tagName) + public function getTagHandler(string $tagName): string { return $this->tags[$tagName]; } - /** - * @param string $tagName - * - * @return Xbbcode - */ - public function setTagName($tagName) + public function setTagName(string $tagName): self { $this->tagName = $tagName; return $this; } - /** - * @return string - */ - public function getTagName() + public function getTagName(): string { return $this->tagName; } - /** - * @return Xbbcode - */ - public function setTree(array $tree) + public function setTree(array $tree): self { $this->tree = $tree; return $this; } - /** - * @return array - */ - public function getTree() + public function getTree(): array { return $this->tree; } - /** - * @param string $tagName - * - * @return Tag - */ - protected function getTagObject($tagName) + protected function getTagObject(string $tagName): Tag { $this->includeTag($tagName); @@ -1590,13 +1495,8 @@ protected function getTagObject($tagName) /** * Функция проверяет, должен ли тег с именем $current закрыться, * если начинается тег с именем $next. - * - * @param string $current - * @param string $next - * - * @return bool */ - protected function mustCloseTag($current, $next) + protected function mustCloseTag(string $current, string $next): bool { if (isset($this->tags[$current])) { $tag = $this->getTagObject($current); @@ -1624,16 +1524,9 @@ protected function mustCloseTag($current, $next) * потомком тег с именем $child. В противном случае - false. * Если $parent - пустая строка, то проверяется, разрешено ли $child входить в * корень дерева BBCode. - * - * @param string $parent - * @param string $child - * - * @return bool */ - protected function isPermissiblyChild($parent, $child) + protected function isPermissiblyChild(string $parent, string $child): bool { - $parent = (string) $parent; - $child = (string) $child; if (isset($this->tags[$parent])) { $tag = $this->getTagObject($parent); $parent_behaviour = $tag::BEHAVIOUR; @@ -1654,12 +1547,7 @@ protected function isPermissiblyChild($parent, $child) return $permissibly; } - /** - * normalizeBracket. - * - * @return array - */ - protected function normalizeBracket(array $syntax) + protected function normalizeBracket(array $syntax): array { $structure = []; $structure_key = -1; @@ -1793,10 +1681,7 @@ protected function normalizeBracket(array $syntax) return $structure; } - /** - * parseTree. - */ - protected function parseTree() + protected function parseTree(): void { /* Превращаем $this->syntax в правильную скобочную структуру */ $structure = $this->normalizeBracket($this->syntax); @@ -1958,13 +1843,9 @@ protected function parseTree() } /** - * getSyntax. - * * @param bool|array $tree - * - * @return array */ - protected function getSyntax($tree = false) + protected function getSyntax($tree = false): array { if (!\is_array($tree)) { $tree = $this->getTree(); @@ -2035,14 +1916,7 @@ protected function getSyntax($tree = false) return $syntax; } - /** - * insertSmiles. - * - * @param string $text - * - * @return string - */ - protected function insertMnemonics($text) + protected function insertMnemonics(string $text): string { $text = \htmlspecialchars($text, \ENT_NOQUOTES); if ($this->getAutoLinks()) { @@ -2056,12 +1930,7 @@ protected function insertMnemonics($text) return $text; } - /** - * highlight. - * - * @return string - */ - public function highlight() + public function highlight(): string { $time_start = \microtime(true); $chars = [ @@ -2138,14 +2007,7 @@ public function highlight() return $str; } - /** - * Возвращает HTML код. - * - * @param array $elems - * - * @return string - */ - public function getHtml(array $elems = null) + public function getHtml(array $elems = null): string { $time_start = \microtime(true); if (null === $elems) { @@ -2205,12 +2067,8 @@ public function getHtml(array $elems = null) * если нет, пытается подключить файл с соответствующим классом. Если это не * возможно, переназначает тегу обработчик, - сопоставляет ему класс bbcode. * Затем инициализирует объект обработчика (если он еще не инициализирован). - * - * @param string $tagName - * - * @return bool */ - protected function includeTag($tagName) + protected function includeTag(string $tagName): void { if (!isset($this->tags[$tagName])) { $this->tags[$tagName] = 'bbcode'; @@ -2220,8 +2078,6 @@ protected function includeTag($tagName) if (!isset($this->tagObjects[$handler])) { $this->tagObjects[$handler] = new $handler(); } - - return true; } /** @@ -2233,9 +2089,9 @@ protected function includeTag($tagName) * 'count_level' => 0 // Число уровней вложенности тегов BBCode * 'memory_peak_usage' => 0, // Максимально выделенный объем памяти * - * @return array + * @return array */ - public function getStatistics() + public function getStatistics(): array { $this->statistics['memory_peak_usage'] = \memory_get_peak_usage(true);