Updates and fixes from phan/phpstan runs

This commit is contained in:
Clemens Schwaighofer
2023-06-28 15:29:29 +09:00
parent 1754ecf2ee
commit 1e9a2230fe
7 changed files with 61 additions and 56 deletions

View File

@@ -483,7 +483,8 @@ final class CoreLibsTemplateHtmlBuilderElementTest extends TestCase
// if we set invalid tag
$el = new Element('div');
$this->expectException(HtmlBuilderExcpetion::class);
$this->expectExceptionMessage("Invalid or empty tag");
$this->expectExceptionMessageMatches("/^Invalid or empty tag: /");
$this->expectExceptionMessage("Invalid or empty tag: 123123");
$el->setTag('123123');
$this->assertTrue(
Error::hasError(),

View File

@@ -117,7 +117,9 @@ try {
$el_er = new Element('');
} catch (HtmlBuilderExcpetion $e) {
print "E: " . $e->getMessage() . " | " . $e->getTraceAsString() . "<br>";
print "E: " . $e->getPrevious()->getMessage() . " | " . $e->getPrevious()->getTraceAsString() . "<br>";
if ($e->getPrevious() !== null) {
print "E: " . $e->getPrevious()->getMessage() . " | " . $e->getPrevious()->getTraceAsString() . "<br>";
}
}
print "Errors: <pre>" . print_r(Error::getMessages(), true) . "</pre>";
print "Warning: " . Support::printToString(Error::hasWarning()) . "<br>";
@@ -127,7 +129,9 @@ try {
$el_er = new Element('123123');
} catch (HtmlBuilderExcpetion $e) {
print "E: " . $e->getMessage() . " | " . $e->getTraceAsString() . "<br>";
print "E: " . $e->getPrevious()->getMessage() . " | " . $e->getPrevious()->getTraceAsString() . "<br>";
if ($e->getPrevious() !== null) {
print "E: " . $e->getPrevious()->getMessage() . " | " . $e->getPrevious()->getTraceAsString() . "<br>";
}
}
print "Errors: <pre>" . print_r(Error::getMessages(), true) . "</pre>";
print "Warning: " . Support::printToString(Error::hasWarning()) . "<br>";

View File

@@ -1,4 +1,4 @@
<?php
<?php // phpcs:disable Generic.Files.LineLength
/**
* AUTOR: Clemens Schwaighofer
@@ -26,6 +26,7 @@ class Block
* @param string $content
* @param array<string> $css,
* @param array<string,string> $options
* @return array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}
* @throws HtmlBuilderExcpetion
*/
public static function cel(
@@ -58,10 +59,10 @@ class Block
* Search element tree for id and add
* if id is empty add at current
*
* @param array<mixed> $base
* @param array<mixed> $attach
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $base
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $attach
* @param string $id
* @return array<mixed>
* @return array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}
*/
public static function ael(
array $base,
@@ -87,9 +88,9 @@ class Block
/**
* Undocumented function
*
* @param array<mixed> $base
* @param array<mixed> ...$attach
* @return array<mixed>
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $base
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} ...$attach
* @return array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}
*/
public static function aelx(
array $base,
@@ -102,9 +103,9 @@ class Block
/**
* Undocumented function
*
* @param array<mixed> $element
* @param array<mixed> $sub
* @return array<mixed>
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $element
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $sub
* @return array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}
*/
public static function addSub(array $element, array ...$sub): array
{
@@ -118,10 +119,10 @@ class Block
/**
* Undocumented function
*
* @param array<mixed> $element
* @return array<mixed>
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $element
* @return array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}
*/
public static function resetSub(array $elment): array
public static function resetSub(array $element): array
{
$element['sub'] = [];
return $element;
@@ -132,9 +133,9 @@ class Block
/**
* Undocumented function
*
* @param array<mixed> $element
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $element
* @param string ...$css
* @return array<mixed>
* @return array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}
*/
public static function acssel(array $element, string ...$css): array
{
@@ -145,8 +146,9 @@ class Block
/**
* Undocumented function
*
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $element
* @param string ...$css
* @return array<mixed>
* @return array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}
*/
public static function rcssel(array $element, string ...$css): array
{
@@ -159,10 +161,10 @@ class Block
* scssel (switch) is not supported
* use rcssel -> acssel
*
* @param array $element
* @param array $rcss
* @param array $acss
* @return array
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $element
* @param array<string> $rcss
* @param array<string> $acss
* @return array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}
*/
public static function scssel(array $element, array $rcss, array $acss): array
{
@@ -176,7 +178,7 @@ class Block
* Undocumented function
* alias phfo
*
* @param array<mixed> $tree
* @param array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>} $tree
* @param bool $add_nl [default=false]
* @return string
*/
@@ -232,14 +234,15 @@ class Block
* Undocumented function
* alias phfa
*
* @param array<mixed> $list
* @param array<array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}> $list
* @param bool $add_nl [default=false]
* @return string
*/
public static function buildHtmlFromList(array $list): string
public static function buildHtmlFromList(array $list, bool $add_nl = false): string
{
$output = '';
foreach ($list as $el) {
$output .= self::buildHtml($el);
$output .= self::buildHtml($el, $add_nl);
}
return $output;
}
@@ -248,7 +251,7 @@ class Block
* Undocumented function
* wrapper for buildHtmlFromList
*
* @param array<Element> $list array of Elements to build string from
* @param array<array{tag:string,id:string,name:string,content:string,css:array<string>,options:array<string,string>,sub:array<mixed>}> $list array of Elements to build string from
* @param bool $add_nl [default=false] Optional output string line break
* @return string build html as string
*/

View File

@@ -85,7 +85,7 @@ class Element
'invalid or empty tag',
['tag' => $tag]
);
throw new HtmlBuilderExcpetion('Invalid or empty tag');
throw new HtmlBuilderExcpetion('Invalid or empty tag: ' . $tag);
}
$this->tag = $tag;
}
@@ -297,7 +297,9 @@ class Element
}
// only on first level
foreach ($this->sub as $pos => $el) {
if ($el['id'] === $id) {
if (
$el->getId() === $id
) {
unset($this->sub[$pos]);
return;
}
@@ -525,10 +527,10 @@ class Element
* not recommended to be used, rather use "Element->buildHtml()"
* wrapper for buildHtml
*
* @param Element $element Element tree to build
* if not set returns empty string
* @param bool $add_nl [default=false] Optional output string line break
* @return string build html as string
* @param ?Element $tree Element tree to build
* if not set returns empty string
* @param bool $add_nl [default=false] Optional output string line break
* @return string build html as string
* @deprecated Do not use, use Element->buildHtml() instead
*/
public static function printHtmlFromObject(Element $tree = null, bool $add_nl = false): string

View File

@@ -13,7 +13,7 @@ namespace CoreLibs\Template\HtmlBuilder\General;
class Error
{
/** @var array{level:string,id:string,message:string,context:array<mixed>} */
/** @var array<array{level:string,id:string,message:string,context:array<mixed>}> */
private static array $messages = [];
/**
@@ -22,7 +22,7 @@ class Error
* @param string $level
* @param string $id
* @param string $message
* @param array $context
* @param array<mixed> $context
* @return void
*/
private static function writeContent(
@@ -98,7 +98,7 @@ class Error
return array_filter(
self::$messages,
function ($var) use ($level) {
return ($var['level'] ?? '') == $level ? true : false;
return $var['level'] == $level ? true : false;
}
) === [] ? false : true;
}

View File

@@ -13,7 +13,7 @@ namespace CoreLibs\Template\HtmlBuilder\General;
class Settings
{
/** @var array>string> list of html elements that can have the name tag */
/** @var array<string> list of html elements that can have the name tag */
public const NAME_ELEMENTS = [
'button',
'fieldset',

View File

@@ -28,7 +28,7 @@ class StringReplace
* index must be non empty (but has no fixed format)
* if same index is tried twice it will set an error and skip
*
* @param array<string,string> ...$element Elements to load
* @param array{0:string,1:string} ...$element Elements to load
* @return void
* @throws HtmlBuilderExcpetion
*/
@@ -138,32 +138,27 @@ class StringReplace
*
* @param string $index index of set element
* @param array<string,string> $replace array of text to search (key) and replace (value) for
* @return string|false
* @return string
* @throws HtmlBuilderExcpetion
*/
public static function buildElement(
string $index,
array $replace,
string $replace_index = ''
): string|bool {
if (self::getElement($index) === false) {
return false;
): string {
try {
self::getElement($index);
} catch (HtmlBuilderExcpetion $e) {
throw new HtmlBuilderExcpetion('Cannot fetch element with index: ' . $index, 0, $e);
}
if ($replace_index) {
self::setReplaceBlock(
$replace_index,
self::replaceData(
self::$elements[$index],
array_keys($replace),
array_values($replace)
)
self::replaceData(self::$elements[$index], $replace)
);
return self::getReplaceBlock($replace_index);
} else {
return self::replaceData(
self::$elements[$index],
array_keys($replace),
array_values($replace)
);
return self::replaceData(self::$elements[$index], $replace);
}
}
@@ -180,9 +175,9 @@ class StringReplace
*/
public static function replaceData(string $data, array $replace): string
{
$replace = array_keys($replace);
$to_replace = array_keys($replace);
// all replace data must have {} around
array_walk($replace, function (&$entry) {
array_walk($to_replace, function (&$entry) {
if (!str_starts_with($entry, '{')) {
$entry = '{' . $entry;
}
@@ -192,7 +187,7 @@ class StringReplace
// do some validation?
});
// replace content
return str_replace($replace, array_values($replace), $data);
return str_replace($to_replace, array_values($replace), $data);
}
}