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

@ -5,43 +5,43 @@ use ctiso\Collection;
abstract class AbstractRule
{
public $field;
public string $field;
protected $errorMsg;
protected $ctx;
public function __construct($field, $errorMsg = null)
public function __construct(string $field, $errorMsg = null)
{
$this->field = $field;
$this->errorMsg = $errorMsg;
}
public function setName($field)
public function setName(string $field): self
{
$this->field = $field;
return $this;
}
public function setErrorMsg($errorMsg)
public function setErrorMsg($errorMsg): self
{
$this->errorMsg = $errorMsg;
return $this;
}
public function getErrorMsg()
{
public function getErrorMsg(): string
{
return $this->errorMsg;
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
return true;
}
function skipEmpty() {
function skipEmpty(): bool {
return true;
}
public function setContext($ctx)
public function setContext($ctx): void
{
$this->ctx = $ctx;
}

View file

@ -9,12 +9,12 @@ use ctiso\Validator\Rule\AbstractRule,
class Alpha extends AbstractRule
{
public function getErrorMsg()
{
public function getErrorMsg(): string
{
return "Поле должно содержать только буквы";
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
return ctype_alpha($container->get($this->field));
}

View file

@ -13,7 +13,7 @@ class Code extends AbstractRule
return "Неправильно указан персональный код";
}
function checkCode($code): bool {
function checkCode(array $code): bool {
foreach($code as $c) {
if (empty($c)) {
return false;

View file

@ -12,22 +12,22 @@ class Count extends AbstractRule
public $size = 1;
public $max = null;
public function getErrorMsg()
{
public function getErrorMsg(): string
{
return "Количество записей должно быть не менне {$this->size} и не более {$this->max}";
}
function not_empty($s) {
return $s != "";
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
if (!$this->max) {
$this->max = $this->size;
}
$count = count(array_filter(array_map('trim',
}
$count = count(array_filter(array_map('trim',
explode(";", $container->get($this->field))), [$this, 'not_empty']));
return $count >= $this->size && $count <= ((int)$this->max);

View file

@ -9,16 +9,16 @@ use ctiso\Validator\Rule\AbstractRule,
class Date extends AbstractRule
{
public function getErrorMsg()
{
public function getErrorMsg(): string
{
return "Неверный формат даты";
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
$pattern = "/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$/";
$matches = [];
return (preg_match($pattern, $container->get($this->field), $matches)
return (preg_match($pattern, $container->get($this->field), $matches)
&& checkdate((int)$matches[2], (int)$matches[1], (int)$matches[3]));
}
}

View file

@ -9,12 +9,12 @@ use ctiso\Validator\Rule\AbstractRule,
class Email extends AbstractRule
{
public function getErrorMsg()
public function getErrorMsg(): string
{
return "Неверный формат электронной почты";
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
$emails = explode(",", $container->get($this->field));
foreach ($emails as $email) {

View file

@ -9,20 +9,14 @@ use ctiso\Validator\Rule\AbstractRule,
class EmailList extends AbstractRule
{
public function getErrorMsg()
{
public function getErrorMsg(): string
{
return "Неверный формат электронной почты";
}
public function isValid(Collection $container, $status = null) {
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\|\{\}~\']+';
$doIsValid = '(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\.?)+';
$ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
$ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
public function isValid(Collection $container, $status = null): bool {
$emails = $container->get($this->field);
foreach ($emails as $email) {
// if (! preg_match("/^$user@($doIsValid|(\[($ipv4|$ipv6)\]))$/", $email)) return false;
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
}
return true;

View file

@ -7,12 +7,12 @@ use ctiso\Validator\Rule\AbstractRule,
class FileName extends AbstractRule {
public function getErrorMsg()
public function getErrorMsg(): string
{
return 'Название файла может содержать только символы латиницы в нижнем регистре и цифры';
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
return Path::isName($container->get($this->field));
}

View file

@ -9,42 +9,42 @@ use ctiso\Validator\Rule\AbstractRule,
class IsFile extends AbstractRule
{
private $type = array();
private $maxsize = 1024;
private array $type = [];
private int $maxsize = 1024;
function skipEmpty() {
function skipEmpty(): bool {
return false;
}
function setSize($size) {
function setSize(int $size): void {
$this->maxsize = $size;
}
function setType(array $type) {
function setType(array $type): void {
$this->type = $type;
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
if (!isset($_FILES[$this->field]) || $_FILES[$this->field]['error'] == UPLOAD_ERR_NO_FILE) {
$this->setErrorMsg('Файл не загружен');
return false;
return false;
}
if ($_FILES[$this->field]['error'] == UPLOAD_ERR_INI_SIZE) {
$this->setErrorMsg('Превышен размер файла');
return false;
return false;
}
$tmp = $_FILES[$this->field];
if (!in_array($tmp['type'], $this->type)) {
$this->setErrorMsg('Неверный формат файла');
return false;
return false;
}
if (((int)$tmp['size']) > $this->maxsize*1024) {
$this->setErrorMsg('Неверный размер файла');
return false;
return false;
}
return true;

View file

@ -11,12 +11,13 @@ class MatchRule extends AbstractRule
{
public $same;
public function getErrorMsg()
{
public function getErrorMsg(): string
{
return "Поля не совпадают";
}
public function isValid(Collection $container, $status = null) {
public function isValid(Collection $container, $status = null): bool
{
return (strcmp($container->get($this->field), $container->get($this->same)) == 0);
}
}

View file

@ -6,16 +6,16 @@ use ctiso\Validator\Rule\AbstractRule,
class Notnull extends AbstractRule
{
function skipEmpty() {
function skipEmpty(): bool {
return false;
}
public function getErrorMsg()
{
public function getErrorMsg(): string
{
return "Поле не должно быть пустым";
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
$data = $container->get($this->field);
if (is_array($data)) {

View file

@ -9,12 +9,12 @@ use ctiso\Validator\Rule\AbstractRule,
class Numeric extends AbstractRule
{
public function getErrorMsg()
{
public function getErrorMsg(): string
{
return "Значение поля должно быть числом";
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
return (is_numeric($container->get($this->field)));
}

View file

@ -1,21 +1,20 @@
<?php
/**
*/
namespace ctiso\Validator\Rule;
use ctiso\Validator\Rule\AbstractRule,
ctiso\Collection;
class PregMatch extends AbstractRule
{
public $pattern;
public function getErrorMsg()
{
return "Поле в неправильном формате";
}
public string $pattern;
public function getErrorMsg(): string
{
return "Поле в неправильном формате";
}
public function isValid(Collection $container, $status = null)
{
return preg_match($this->pattern,$container->get($this->field));
}
public function isValid(Collection $container, $status = null): bool
{
return preg_match($this->pattern, $container->get($this->field));
}
}

View file

@ -11,19 +11,20 @@ class Time extends AbstractRule
{
private $split = ":";
public function getErrorMsg()
public function getErrorMsg(): string
{
return "Неверный формат времени";
}
static function checktime($hour, $minute)
static function checktime(int $hour, int $minute): bool
{
if ($hour > -1 && $hour < 24 && $minute > -1 && $minute < 60) {
return true;
}
return false;
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
/** @var list<string> */
$tmp = explode($this->split, $container->get($this->field), 2);

View file

@ -8,12 +8,12 @@ use ctiso\Validator\Rule\AbstractRule,
class Unique extends AbstractRule
{
public function getErrorMsg()
public function getErrorMsg(): string
{
return $this->ctx->getMessage();
}
public function isValid(Collection $container, $status = null)
public function isValid(Collection $container, $status = null): bool
{
return $this->ctx->isUnique($container->get($this->field), $status, $container);
}