diff --git a/src/Functions.php b/src/Functions.php index 887d741..66e91f1 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -147,6 +147,7 @@ class Functions { } /** + * @deprecated * @param array $value * @param string $name * @@ -156,11 +157,15 @@ class Functions { return $value[$name]; } + /** + * @deprecated + */ static function identity($value) { return $value; } /** + * @deprecated * @param array $a * @param array $b * @param $key diff --git a/src/Mail.php b/src/Mail.php index 7f3930d..a92e0a4 100644 --- a/src/Mail.php +++ b/src/Mail.php @@ -4,7 +4,9 @@ * Класс для работы с почтой * http://en.wikipedia.org/wiki/MIME */ + namespace ctiso; + use ctiso\Path, Exception; @@ -16,14 +18,15 @@ class Mail public $content; public $copy; - private $encoding; + private $encoding; private $_notify = null; - protected $attachment = array (); + protected $attachment = array(); protected $uniqid; protected $type = "text/plain"; - function __construct() { + function __construct() + { $this->setEncoding("UTF-8"); $this->uniqid = strtoupper(uniqid((string)time())); // Идентефикатор разделителя } @@ -31,32 +34,32 @@ class Mail /** * Установка отправителя */ - function from($name) + function from(string $name) { + // filter_var($name, FILTER_VALIDATE_EMAIL); $this->_from = $name; - } + } /** * Установка получателя */ - function to($name) // recipient + function to(string $name) // recipient { $this->_to = $name; } function replyTo($name) // recipient - { - } + {} /** * Установка получателей копии */ - function copy($name) // recipient cc + function copy(string $name) // recipient cc { $this->copy = $name; } - - function notify($notify) + + function notify(string $notify) { $this->_notify = $notify; } @@ -64,23 +67,23 @@ class Mail /** * Тема письма */ - function subject($subject) + function subject(string $subject) { - $this->_subject = $subject; + $this->_subject = $subject; } /** * Текст письма */ - function setContent($text) + function setContent(string $text) { $this->content = $text; } - + /** * Кодировка текста в письме */ - function setEncoding($encoding) + function setEncoding(string $encoding) { $this->encoding = $encoding; } @@ -88,15 +91,13 @@ class Mail /** * Добавление вложения из файла */ - function addAttachment($filename, $name = false) + function addAttachment(string $filename, $name = false) { - assert(is_string($filename)); - - if(file_exists($filename)) { // assert ?? + if (file_exists($filename)) { // assert ?? $file = fopen($filename, "rb"); if (is_resource($file)) { $data = fread($file, filesize($filename)); - $this->attachment [] = ($name) ? [$data, $name] : [$data, basename($filename)]; + $this->attachment[] = ($name) ? [$data, $name] : [$data, basename($filename)]; } } } @@ -109,11 +110,9 @@ class Mail /** * Добавление вложения из строки с указанием имени файла */ - function addAttachmentRaw($data, $name) + function addAttachmentRaw($data, string $name) { - assert(is_string($name)); - - $this->attachment [] = [$data, $name]; + $this->attachment[] = [$data, $name]; } function quote($var, $val) @@ -127,33 +126,33 @@ class Mail */ function mimeTag($name, $value, array $args = []) { - assert (is_string($name)); - assert (is_string($value)); + assert(is_string($name)); + assert(is_string($value)); - return $name . ": " . $value . implode("", array_map([$this, 'quote'], array_keys($args), array_values($args))) . PHP_EOL; + return $name . ": " . $value . implode("", array_map([$this, 'quote'], array_keys($args), array_values($args))) . PHP_EOL; } /** - * - * @see http://tools.ietf.org/html/rfc2047 + * + * @see http://tools.ietf.org/html/rfc2047 */ - function encodedWord($text, $encoding = 'B') + function encodedWord(string $text, string $encoding = 'B') { - return "=?{$this->encoding}?$encoding?".base64_encode($text)."?="; + return "=?{$this->encoding}?$encoding?" . base64_encode($text) . "?="; } /** * Тело сообщения */ - function getMessage() + function getMessage(): string { - $message = "--".$this->uniqid . PHP_EOL; + $message = "--" . $this->uniqid . PHP_EOL; $message .= $this->mimeTag("Content-Type", $this->type, ['charset' => $this->encoding]); $message .= $this->mimeTag("Content-Transfer-Encoding", "8bit"); $message .= PHP_EOL . $this->content . PHP_EOL . PHP_EOL; /* - * Вложения + * Вложения * http://tools.ietf.org/html/rfc2046#section-5.1.3 */ foreach ($this->attachment as $value) { @@ -171,7 +170,7 @@ class Mail /** * Заголовок сообщения */ - function getHeader() + function getHeader(): string { $head = $this->mimeTag("MIME-Version", "1.0"); $head .= $this->mimeTag("From", $this->_from); @@ -188,7 +187,7 @@ class Mail return $head; } - function getSubject() + function getSubject(): string { return $this->encodedWord($this->_subject); } @@ -196,27 +195,28 @@ class Mail /** * Вывод строки для сохранения в формате .eml */ - function eml() + function eml(): string { return "To: " . $this->_to . PHP_EOL . "Subject: {$this->getSubject()}" . PHP_EOL . $this->getHeader() . $this->getMessage(); } /** - * Отправка почты + * Отправка почты */ - function send() + function send(): bool { $result = mail($this->_to, $this->getSubject(), $this->getMessage(), $this->getHeader()); - if(! $result) { + if (! $result) { file_put_contents(Path::resolveFile("send.eml"), $this->eml()); - throw new Exception('Невозможно отправить почту'); + throw new Exception('Невозможно отправить почту'); } return $result; } - function clear() { + function clear(): void + { foreach ($this->attachment as $key => &$value) { - unset($this->attachment[$key]); + unset($this->attachment[$key]); } $this->attachment = []; } diff --git a/src/MailAlt.php b/src/MailAlt.php index 2775553..1a08d50 100644 --- a/src/MailAlt.php +++ b/src/MailAlt.php @@ -62,12 +62,12 @@ class MailAlt * Текст письма * @param string $text */ - function setContent($text) + function setContent(string $text) { $this->mailer->Body = $text; } - function setType($text) + function setType(string $text) { $this->mailer->isHTML($text == 'text/html'); } @@ -75,7 +75,7 @@ class MailAlt /** * Кодировка текста в письме */ - function setEncoding($encoding) + function setEncoding(string $encoding) { $this->encoding = $encoding; } @@ -83,7 +83,7 @@ class MailAlt /** * Добавление вложения из файла */ - function addAttachment(string $filename, $name = null) + function addAttachment(string $filename, ?string $name = null) { $this->mailer->addAttachment($filename, $name); } diff --git a/src/Path.php b/src/Path.php index aee6a87..2cdbee5 100644 --- a/src/Path.php +++ b/src/Path.php @@ -12,14 +12,13 @@ class Path { const SEPARATOR = "/"; - protected $path = array(); - protected $url = array(); - protected $absolute = false; + protected array $path = []; + protected array $url = []; + protected bool $absolute = false; - public function __construct($path = '') + public function __construct(string $path = '') { - //assert(is_string($path)); - $this->url = parse_url($path ?? ''); + $this->url = parse_url($path); if (isset($this->url['path'])) { $path = $this->url['path']; @@ -36,16 +35,16 @@ class Path } } - static function factory($path) { + static function factory(string $path): Path { return new Path($path); } - public function getParts() + public function getParts(): array { return $this->path; } - public static function normalize($pathName) + public static function normalize(string $pathName): string { $path = new Path($pathName); return $path->__toString(); @@ -84,15 +83,9 @@ class Path /** * Полное имя файла без расширения - * - * @param string $fileName Имя файла - * - * @return string */ - static function skipExtension($fileName) + static function skipExtension(string $fileName): string { - assert(is_string($fileName)); - $path = pathinfo($fileName); if ($path['dirname'] === ".") { return $path['filename']; @@ -108,10 +101,8 @@ class Path * * @return string */ - static function getFileName($fileName) + static function getFileName(string $fileName) { - assert(is_string($fileName)); - return pathinfo($fileName, PATHINFO_FILENAME); } @@ -121,15 +112,10 @@ class Path * Преобразует строку пути в массив * * @param string $path Путь - * - * @return array */ - public static function listFromString($path) + public static function listFromString(string $path): array { - assert(is_string($path)); - $list = preg_split('#\\\\|/#s', $path); - return $list; } @@ -154,11 +140,10 @@ class Path return $result; } - // Сравнение двух путей на равентство /** - * @param Path $path + * Сравнение двух путей на равентство */ - public function equal($path) + public function equal(Path $path): bool { $count = count($this->path); if ($count == count($path->path)) { @@ -172,7 +157,7 @@ class Path return false; } - public static function makeUrl($path) + public static function makeUrl($path): string { $slash = (isset($path['host']) && (strlen($path['path']) > 0) && ($path['path'][0] != '/')) ? '/' : ''; @@ -189,10 +174,8 @@ class Path /** * Преобразует путь в строку - * - * @return string */ - public function __toString() + public function __toString(): string { $result = (($this->absolute) ? '/' : '') . implode(self::SEPARATOR, $this->path); $this->url['path'] = $result; @@ -203,10 +186,8 @@ class Path * Проверяет является ли папка родительской для другой папки * * @param Path $path - * - * @return boolean */ - public function isParent($path) + public function isParent($path): bool { if (isset($this->url['host']) && isset($path->url['host']) && ($this->url['host'] != $path->url['host'])) return false; @@ -223,7 +204,7 @@ class Path return false; } - public static function _isParent($path1, $path2) + public static function _isParent(string $path1, string $path2): bool { $path = new Path($path1); return $path->isParent(new Path($path2)); @@ -421,7 +402,7 @@ class Path return $result; } - protected static function fileListAll(&$result, $base, &$allow, &$ignore) + protected static function fileListAll(array &$result, string $base, ?array &$allow, array &$ignore) { $files = self::fileList($base, $allow, $ignore); foreach ($files as $name) { diff --git a/src/SortRecord.php b/src/SortRecord.php index f1ed4e6..855c85b 100644 --- a/src/SortRecord.php +++ b/src/SortRecord.php @@ -13,6 +13,11 @@ class SortRecord $this->order = ((boolean)($order) === false) ? 1 : -1; } + /** + * @template T + * @param array $a + * @param array $b + */ function compare(array $a, array $b): int { if($a[$this->key] == $b[$this->key]) { @@ -29,12 +34,12 @@ class SortRecord return ($a->{$this->key} > $b->{$this->key}) ? $this->order : -$this->order; } - function sort(array &$list) + function sort(array &$list): bool { return usort($list, [$this, 'compare']); } - function sortKeys(array &$list) + function sortKeys(array &$list): bool { return usort($list, [$this, 'compareKeys']); } diff --git a/src/Tales.php b/src/Tales.php index 21b15cb..400bf24 100644 --- a/src/Tales.php +++ b/src/Tales.php @@ -13,12 +13,12 @@ use PHPTAL_Php_TalesInternal, class Tales_DateTime implements PHPTAL_Tales { - static public function date($expression, $nothrow = false): string + static public function date(string $expression, $nothrow = false): string { return "ctiso\\Tales::phptal_date(".PHPTAL_Php_TalesInternal::path($expression).")"; } - static public function time($expression, $nothrow = false): string + static public function time(string $expression, $nothrow = false): string { return "ctiso\\Tales::phptal_time(".PHPTAL_Php_TalesInternal::path($expression).")"; } @@ -26,7 +26,7 @@ class Tales_DateTime implements PHPTAL_Tales class Tales_Component implements PHPTAL_Tales { - static public function component($expression, $nothrow = false): string + static public function component(string $expression, $nothrow = false): string { $s = PHPTAL_Php_TalesInternal::string($expression); return "ctiso\\Tales::phptal_component(" . $s . ")"; @@ -35,7 +35,7 @@ class Tales_Component implements PHPTAL_Tales class Tales_Assets implements PHPTAL_Tales { - static public function assets($expression, $nothrow = false): string + static public function assets(string $expression, $nothrow = false): string { $s = PHPTAL_Php_TalesInternal::string($expression); return "ctiso\\Tales::phptal_asset(" . $s . ")"; @@ -43,18 +43,18 @@ class Tales_Assets implements PHPTAL_Tales } class Tales { - /** @var SiteInterface */ + /** @var ?SiteInterface */ static $site; - static function phptal_date ($e): string { + static function phptal_date (int $e): string { return date("d.m.Y", $e); } - static function phptal_time ($e): string { + static function phptal_time (int $e): string { return date("H:i", $e); } - static function phptal_asset($s): string { + static function phptal_asset(string $s): string { self::$site->addStyleSheet($s); return ""; } @@ -74,7 +74,7 @@ class Tales { } - static function register($site) { + static function register(?SiteInterface $site) { self::$site = $site; /* Регистрация нового префикса для подключения компонента */ diff --git a/src/Tools/TemplateImage.php b/src/Tools/TemplateImage.php index 6854bb6..161c68a 100644 --- a/src/Tools/TemplateImage.php +++ b/src/Tools/TemplateImage.php @@ -5,6 +5,7 @@ */ namespace ctiso\Tools; use ctiso\Tools\Drawing; +use GdImage; class TemplateImage { @@ -35,11 +36,11 @@ class TemplateImage protected array $context = []; protected array $data = []; protected string $base = "c:\\windows\\fonts\\"; - protected $image; + protected GdImage $image; protected $_prepare = true; public $debug = false; - public $resource; - public $filename; + public string $resource; + public string $filename; function __construct (?array $template = null) { @@ -66,10 +67,10 @@ class TemplateImage function set(string $name, $value) { - $this->context['['.$name.']'] = $this->encode($value); + $this->context['['.$name.']'] = $value; } - function setImage($name): void + function setImage(string $name): void { $this->filename = $name; $this->image = $this->imagefromfile($name); @@ -140,11 +141,11 @@ class TemplateImage /** * Перекодировка текста - * @deprecated + * @deprecated Можно заменить encode($x) -> $x */ function encode(string $text): string { - return $text; //iconv("WINDOWS-1251", "UTF-8", $text); + return $text; } function setSize(int $new_width, int $new_height): void diff --git a/src/Url.php b/src/Url.php index 67e145a..56b8ddb 100644 --- a/src/Url.php +++ b/src/Url.php @@ -11,7 +11,10 @@ class Url { $this->parent = $parent; } - function setQuery($parts): void { + /** + * @param string[] $parts + */ + function setQuery(array $parts): void { $this->parts = $parts; } diff --git a/src/Validator/Rule/AbstractRule.php b/src/Validator/Rule/AbstractRule.php index 6a7ebef..52619ea 100644 --- a/src/Validator/Rule/AbstractRule.php +++ b/src/Validator/Rule/AbstractRule.php @@ -6,10 +6,10 @@ use ctiso\Collection; abstract class AbstractRule { public string $field; - protected $errorMsg; + protected ?string $errorMsg; protected $ctx; - public function __construct(string $field, $errorMsg = null) + public function __construct(string $field, ?string $errorMsg = null) { $this->field = $field; $this->errorMsg = $errorMsg; diff --git a/src/Validator/Rule/Count.php b/src/Validator/Rule/Count.php index 008e24b..7115587 100644 --- a/src/Validator/Rule/Count.php +++ b/src/Validator/Rule/Count.php @@ -9,15 +9,15 @@ use ctiso\Validator\Rule\AbstractRule, class Count extends AbstractRule { - public $size = 1; - public $max = null; + public int $size = 1; + public ?int $max = null; public function getErrorMsg(): string { return "Количество записей должно быть не менне {$this->size} и не более {$this->max}"; } - function not_empty($s) { + function notEmpty($s): bool { return $s != ""; } @@ -28,7 +28,7 @@ class Count extends AbstractRule $this->max = $this->size; } $count = count(array_filter(array_map('trim', - explode(";", $container->get($this->field))), [$this, 'not_empty'])); + explode(";", $container->get($this->field))), [$this, 'notEmpty'])); return $count >= $this->size && $count <= ((int)$this->max); } diff --git a/src/Validator/Rule/Time.php b/src/Validator/Rule/Time.php index 658e3e8..34534b9 100644 --- a/src/Validator/Rule/Time.php +++ b/src/Validator/Rule/Time.php @@ -9,7 +9,7 @@ use ctiso\Validator\Rule\AbstractRule, class Time extends AbstractRule { - private $split = ":"; + private string $split = ":"; public function getErrorMsg(): string { diff --git a/src/Validator/Validator.php b/src/Validator/Validator.php index 8abd7dd..2b8bd32 100644 --- a/src/Validator/Validator.php +++ b/src/Validator/Validator.php @@ -17,7 +17,7 @@ class Validator /** * Поля по умолчанию - * @var array> + * @var array> */ protected $type = [ 'date' => Rule\Date::class, @@ -43,9 +43,9 @@ class Validator /** * Добавление правила в список * @param string $name - * @param class-string $className + * @param class-string $className */ - function addRuleType(string $name, string $className) { + function addRuleType(string $name, string $className): void { $this->type[$name] = $className; } @@ -99,7 +99,7 @@ class Validator /** * @param AbstractRule $rule */ - public function skip($rule, Collection $container) // -> Rule_Abstract + public function skip($rule, Collection $container): bool { if ($rule->skipEmpty()) { $data = $container->get($rule->field); @@ -111,7 +111,7 @@ class Validator return false; } - function reset() { + function reset(): void { $this->errorMsg = []; } @@ -121,7 +121,7 @@ class Validator if ($rule) { $this->chain = $rule; } - // $this->errorMsg = []; + foreach ($this->chain as $rule) { //echo $key; if (!in_array($rule->field, $fields) && !$this->skip($rule, $container) && !$rule->isValid($container, $status)) { @@ -133,17 +133,17 @@ class Validator return $this->isValid(); } - public function addError($name, $message) + public function addError(string $name, string $message) { $this->errorMsg[$name] = $message; } - public function isError() + public function isError(): bool { return !empty($this->errorMsg); } - public function isValid() + public function isValid(): bool { return empty($this->errorMsg); } diff --git a/src/View/Composite.php b/src/View/Composite.php index bc2101b..5261ba4 100644 --- a/src/View/Composite.php +++ b/src/View/Composite.php @@ -6,10 +6,10 @@ use ctiso\View\View, class Composite extends View { - private $tal; + private PHPTAL $tal; public $config; - function __construct($file) + function __construct(string $file) { parent::__construct(); @@ -29,17 +29,17 @@ class Composite extends View $this->tal->set($key, $val); } - function __set($key, $val) { + function __set(string $key, mixed $val) { $this->tal->set($key, $val); } - function execute() + function execute(): string { $this->processChild(); return $this->tal->execute(); } - function setTranslator($t) { + function setTranslator($t): void { $this->tal->setTranslator($t); } } diff --git a/src/View/View.php b/src/View/View.php index 70a565e..627d983 100644 --- a/src/View/View.php +++ b/src/View/View.php @@ -7,10 +7,15 @@ class View extends \stdClass { protected array $_section = []; // Вложенные шаблоны // Блоки + /** @var string[] $_stylesheet */ protected array $_stylesheet = []; // Массив стилей текущего шаблона + /** @var string[] $_script */ protected array $_script = []; // Массив скриптов текущего шаблона + /** @var string[] $_scriptstring */ public array $_scriptstring = []; + /** @var string[] $_startup */ protected array $_startup = []; + protected array $_values = []; protected ?string $_title = null; // Заголовок текущего шаблона @@ -94,7 +99,7 @@ class View extends \stdClass * @param string $list Имя свойства * @param bool $flatten */ - protected function doTree($list, bool $flatten = true) + protected function doTree($list, bool $flatten = true): array { $result = ($flatten == true) ? $this->$list : [$this->$list]; foreach ($this->_section as $value) { @@ -131,7 +136,7 @@ class View extends \stdClass $this->_title = $title; } - protected function isNotNull($title): bool + protected function isNotNull(?string $title): bool { return $title !== null; }