fix: phpstan level=6

This commit is contained in:
origami11@yandex.ru 2025-10-06 12:49:36 +03:00
parent acbf2c847d
commit 48269bd424
41 changed files with 324 additions and 347 deletions

View file

@ -2,6 +2,8 @@
namespace ctiso\Tools;
use GdImage;
class Drawing
{
const ALIGN_LEFT = "left";
@ -10,13 +12,13 @@ class Drawing
const ALIGN_CENTER = "center";
const ALIGN_RIGHT = "right";
static function drawrectnagle(&$image, $left, $top, $width, $height, $rgb)
static function drawrectnagle(GdImage &$image, int $left, int $top, int $width, int $height, array $rgb): void
{
$color = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]);
$right = $left + $width;
$bottom = $top + $height;
imageline($image, $left, $top, $right, $top, $color);
imageline($image, $right,$top, $right, $bottom, $color);
imageline($image, $right, $top, $right, $bottom, $color);
imageline($image, $left, $bottom, $right, $bottom, $color);
imageline($image, $left, $top, $left, $bottom, $color);
}
@ -24,12 +26,23 @@ class Drawing
/**
* http://ru2.php.net/imagettftext
*/
static function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text,
$max_width, $max_height, $align, $valign)
{
// echo $left,"\n", $top, "\n";
// echo $max_width,"\n", $max_height, "\n";
// self::drawrectnagle($image, $left, $top, $max_width, $max_height, array(0xFF,0,0));
static function imagettftextbox(
GdImage &$image,
int $size,
float $angle,
$left,
$top,
$color,
$font,
$text,
$max_width,
$max_height,
$align,
$valign
) {
// echo $left,"\n", $top, "\n";
// echo $max_width,"\n", $max_height, "\n";
// self::drawrectnagle($image, $left, $top, $max_width, $max_height, array(0xFF,0,0));
$text_lines = explode("\n", $text); // Supports manual line breaks!
$lines = [];
@ -103,17 +116,13 @@ class Drawing
}
function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0)
function imagettftextSp(GdImage $image, float $size, float $angle, int $x, int $y, int $color, string $font, string $text, int $spacing = 0)
{
if ($spacing == 0)
{
if ($spacing == 0) {
imagettftext($image, $size, $angle, $x, $y, $color, $font, $text);
}
else
{
} else {
$temp_x = $x;
for ($i = 0; $i < mb_strlen($text); $i++)
{
for ($i = 0; $i < mb_strlen($text); $i++) {
$bbox = imagettftext($image, $size, $angle, $temp_x, $y, $color, $font, $text[$i]);
$temp_x += $spacing + ($bbox[2] - $bbox[0]);
}

View file

@ -2,9 +2,11 @@
namespace ctiso\Tools;
use GdImage;
class Image
{
static function load($uri)
{
static function load($uri): GdImage|false
{
$e = strtolower(pathinfo($uri, PATHINFO_EXTENSION));
switch ($e) {
@ -12,9 +14,10 @@ class Image
case 'jpeg': case 'jpg': return imagecreatefromjpeg($uri);
case 'gif': return imagecreatefromgif($uri);
}
return false;
}
static function fit($image, $prewidth, $preheight, $force = true)
static function fit(GdImage $image, int $prewidth, int $preheight, bool $force = true): GdImage|false
{
$width = imagesx($image);
$height = imagesy($image);
@ -29,7 +32,7 @@ class Image
return $image_p;
}
static function save($image, $uri)
static function save($image, $uri): bool
{
$e = strtolower(pathinfo($uri, PATHINFO_EXTENSION));
switch ($e) {
@ -37,5 +40,6 @@ class Image
case 'png': imagepng($image, $uri); break;
case 'gif': imagegif($image, $uri); break;
}
return false;
}
}

View file

@ -124,7 +124,7 @@ class SQLStatementExtractor {
* @param string $string The string to check in (haystack).
* @return boolean True if $string ends with $check, or they are equal, or $check is empty.
*/
protected static function endsWith(string $check, $string) {
protected static function endsWith(string $check, string $string) {
if ($check === "" || $check === $string) {
return true;
} else {
@ -136,7 +136,7 @@ class SQLStatementExtractor {
* a natural way of getting a subtring, php's circular string buffer and strange
* return values suck if you want to program strict as of C or friends
*/
protected static function substring($string, $startpos, $endpos = -1) {
protected static function substring(string $string, int $startpos, int $endpos = -1) {
$len = strlen($string);
$endpos = (int) (($endpos === -1) ? $len-1 : $endpos);
if ($startpos > $len-1 || $startpos < 0) {
@ -157,9 +157,9 @@ class SQLStatementExtractor {
* Convert string buffer into array of lines.
*
* @param string $buffer
* @return array string[] lines of file.
* @return string[] lines of file.
*/
protected static function getLines($buffer) {
protected static function getLines(string $buffer): array {
$lines = preg_split("/\r?\n|\r/", $buffer);
return $lines;
}

View file

@ -5,7 +5,7 @@ namespace ctiso\Tools;
class StringUtil {
// from creole
static function strToArray($str) {
static function strToArray(string $str): array {
$str = substr($str, 1, -1); // remove { }
$res = [];
@ -40,7 +40,7 @@ class StringUtil {
}
//Нормализация строк на русском
static function normalizeRussian($str) {
static function normalizeRussian(string $str): string {
$result = preg_replace('/\s+/',' ', $str);
if (is_string($result)) {
$result = trim($result); //Замена длинных пробелов на одинарные, пробелы по краям
@ -62,8 +62,8 @@ class StringUtil {
* output: true
* input: $str="foo" $variants="foo1|foo2|foo3"
* output: false
*/
static function compare_string_to_variants($str, $variants){
*/
static function compare_string_to_variants($str, string $variants){
$variants_array = explode('|', $variants);
$founded = false;
foreach ($variants_array as $variant) {
@ -72,7 +72,7 @@ class StringUtil {
return $founded;
}
static function mb_str_split($str) {
static function mb_str_split(string $str): array|false {
return preg_split('~~u', $str, -1, PREG_SPLIT_NO_EMPTY);
}
@ -80,32 +80,33 @@ class StringUtil {
return str_replace(self::mb_str_split($from), self::mb_str_split($to), $str);
}
static function encodestring($st) {
static function encodestring(string $st): string
{
$st = self::mb_strtr($st,"абвгдеёзийклмнопрстуфхъыэ !+()", "abvgdeeziyklmnoprstufh_ie_____");
$st = self::mb_strtr($st,"АБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЭ", "ABVGDEEZIYKLMNOPRSTUFH_IE");
$st = strtr($st, [
" " => '_',
"." => '_',
"," => '_',
"?" => '_',
"\"" => '_',
"'" => '_',
"/" => '_',
"\\" => '_',
"%" => '_',
"#" => '_',
"*" => '_',
"ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh",
"щ"=>"shch","ь"=>"", "ю"=>"yu", "я"=>"ya",
"Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH",
"Щ"=>"SHCH","Ь"=>"", "Ю"=>"YU", "Я"=>"YA",
"Й"=>"i", "й"=>"ie", "ё"=>"Ye",
""=>"N"
]);
" " => '_',
"." => '_',
"," => '_',
"?" => '_',
"\"" => '_',
"'" => '_',
"/" => '_',
"\\" => '_',
"%" => '_',
"#" => '_',
"*" => '_',
"ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh",
"щ"=>"shch","ь"=>"", "ю"=>"yu", "я"=>"ya",
"Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH",
"Щ"=>"SHCH","Ь"=>"", "Ю"=>"YU", "Я"=>"YA",
"Й"=>"i", "й"=>"ie", "ё"=>"Ye",
""=>"N"
]);
return strtolower($st);
}
static function validate_encoded_string($st) {
static function validate_encoded_string(string $st): int|false {
$enc_st = self::encodestring($st);
return preg_match('/^[\w_-]+(\.[\w_-]+)?$/', $enc_st);
}

View file

@ -8,8 +8,8 @@ use ctiso\Tools\Drawing;
class TemplateImage
{
static $listfiles = array('jpg' => 'jpeg', 'gif' => 'gif', 'png' => 'png', 'bmp' => 'wbmp');
static $listfonts = array(
static array $listfiles = array('jpg' => 'jpeg', 'gif' => 'gif', 'png' => 'png', 'bmp' => 'wbmp');
static array $listfonts = array(
'georgia' => 'georgia.ttf',
'georgiabd' => 'georgiab.ttf',
'georgiaz' => 'georgiaz.ttf',
@ -32,16 +32,16 @@ class TemplateImage
);
protected $src;
protected $context = array();
protected $data = array();
protected $base = "c:\\windows\\fonts\\";
protected array $context = [];
protected array $data = [];
protected string $base = "c:\\windows\\fonts\\";
protected $image;
protected $_prepare = true;
public $debug = false;
public $resource;
public $filename;
function __construct (?string $template = null)
function __construct (?array $template = null)
{
if ($template) {
$this->data = $template;
@ -51,7 +51,7 @@ class TemplateImage
/**
* Путь к изображению
*/
function resourcePath(string $path)
function resourcePath(string $path): void
{
$this->resource = $path;
}
@ -59,7 +59,7 @@ class TemplateImage
/**
* Путь у шрифтам
*/
function fontPath(string $path)
function fontPath(string $path): void
{
$this->base = $path;
}
@ -69,13 +69,13 @@ class TemplateImage
$this->context['['.$name.']'] = $this->encode($value);
}
function setImage($name)
function setImage($name): void
{
$this->filename = $name;
$this->image = $this->imagefromfile($name);
}
function setEmptyImage($width, $height)
function setEmptyImage($width, $height): void
{
$this->image = imagecreatetruecolor($width, $height);
}
@ -92,7 +92,7 @@ class TemplateImage
return null;
}
function getFontFile(string $name)
function getFontFile(string $name): string
{
if(array_key_exists(strtolower($name), self::$listfonts)) {
return $this->base . self::$listfonts[$name];
@ -100,7 +100,7 @@ class TemplateImage
return $this->base . 'arial.ttf';
}
function fontSuffix(array $style)
function fontSuffix(array $style): string
{
if($style[0] && $style[1]) return "z";
@ -110,14 +110,8 @@ class TemplateImage
return "";
}
/**
* @param string $text
* @param object $value
*/
function imageText($text, $value)
function imageText(string $text, object $value)
{
assert(is_string($text));
$text = strtr($text, $this->context);
$size = $value->fontSize;
$fontfile = $this->getFontFile($value->fontFamily . $this->fontSuffix($value->fontStyle));
@ -148,17 +142,12 @@ class TemplateImage
* Перекодировка текста
* @deprecated
*/
function encode($text)
function encode(string $text): string
{
assert(is_string($text));
return $text; //iconv("WINDOWS-1251", "UTF-8", $text);
}
/**
* @param int $new_width
* @param int $new_height
*/
function setSize($new_width, $new_height)
function setSize(int $new_width, int $new_height): void
{
$width = imagesx($this->image);
$height = imagesy($this->image);
@ -173,7 +162,7 @@ class TemplateImage
$this->image = $image_p;
}
function prepare() {
function prepare(): void {
if($this->_prepare) {
$this->_prepare = false;
foreach ($this->data as $value) {
@ -185,10 +174,8 @@ class TemplateImage
/**
* Генерирует изображение из шаблона
*/
function render($file = null)
function render(?string $file = null): string|bool
{
assert(is_string($file) || is_null($file));
$this->prepare();
if ($file == null) {