diff --git a/src/Annotations.php b/src/Annotations.php index a2c4f5f..fdbe5a4 100644 --- a/src/Annotations.php +++ b/src/Annotations.php @@ -27,17 +27,15 @@ class Annotations /** @internal identifier */ public const RE_IDENTIFIER = '[_a-zA-Z\x7F-\xFF][_a-zA-Z0-9\x7F-\xFF-\\\]*'; - /** @var bool */ - public static $useReflection; + public static ?bool $useReflection = null; - /** @var bool */ - public static $autoRefresh = true; + public static bool $autoRefresh = true; /** @var string[] */ - public static $inherited = ['description', 'param', 'return']; + public static array $inherited = ['description', 'param', 'return']; /** @var array>>> */ - private static $cache; + private static array $cache; /** * @param ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionFunction $r @@ -49,9 +47,8 @@ public static function hasAnnotation(Reflector $r, string $name): bool /** * @param ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionFunction $r - * @return mixed */ - public static function getAnnotation(Reflector $r, string $name) + public static function getAnnotation(Reflector $r, string $name): mixed { $res = self::getAnnotations($r); @@ -93,14 +90,12 @@ public static function getAnnotations(Reflector $r): array return self::$cache[$type][$member]; } - if (self::$useReflection) { - $annotations = self::parseComment((string) $r->getDocComment()); - } else { - $annotations = []; - } + $annotations = self::$useReflection ? self::parseComment((string) $r->getDocComment()) : []; - // @phpstan-ignore-next-line - if ($r instanceof ReflectionMethod && !$r->isPrivate() && (!$r->isConstructor() || !empty($annotations['inheritdoc'][0])) + if ( + $r instanceof ReflectionMethod && !$r->isPrivate() + && (!$r->isConstructor() || !empty($annotations['inheritdoc'][0])) // @phpstan-ignore-line + && $type !== null ) { try { $inherited = self::getAnnotations(new ReflectionMethod((string) get_parent_class($type), $member)); diff --git a/src/Csv.php b/src/Csv.php index 8ed02e5..7ac8a3d 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -75,11 +75,10 @@ public static function structural(array $scheme, string $file, string $delimiter } /** - * @param mixed $value * @param mixed[][] $liner * @param string[] $keys */ - protected static function matchValue($value, array &$liner, array $keys): void + protected static function matchValue(mixed $value, array &$liner, array $keys): void { if (count($keys) > 1) { $tmp = array_shift($keys); diff --git a/src/DateTime.php b/src/DateTime.php index f16af49..6da2c9a 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -6,6 +6,9 @@ use DateTimeZone; use Nette\Utils\DateTime as NetteDateTime; +/** + * @phpstan-consistent-constructor + */ class DateTime extends NetteDateTime { @@ -14,77 +17,12 @@ public function __construct(string $time = 'now', ?DateTimeZone $timezone = null parent::__construct($time, $timezone); } - /** - * @param string|int|DateTimeInterface $time - * @return static - */ - public static function from($time): self + public static function from(string|int|DateTimeInterface|null $time): static { return parent::from($time); } - /** - * @return static - */ - public function modifyClone(string $modify = ''): self - { - return parent::modifyClone($modify); - } - - /** - * Set time to current time - * - * @return static - */ - public function setCurrentTime(): self - { - return $this->modifyClone()->setTime((int) date('H'), (int) date('i'), (int) date('s')); - } - - /** - * Reset current time (00:00:00) - * - * @return static - */ - public function resetTime(): self - { - return $this->modifyClone()->setTime(0, 0, 0); - } - - /** - * Reset current time (00:00:00) - * - * @return static - */ - public function setZeroTime(): self - { - return $this->resetTime(); - } - - /** - * Set time to midnight (23:59:59) - * - * @return static - */ - public function setMidnight(): self - { - return $this->modifyClone()->setTime(23, 59, 59); - } - - /** - * Set date to today - * - * @return static - */ - public function setToday(): self - { - return $this->modifyClone()->setDate((int) date('Y'), (int) date('m'), (int) date('d')); - } - - /** - * @return static - */ - public static function createBy(?int $year = null, ?int $month = null, ?int $day = null, ?int $hour = null, ?int $minute = null, ?int $second = null): self + public static function createBy(?int $year = null, ?int $month = null, ?int $day = null, ?int $hour = null, ?int $minute = null, ?int $second = null): static { return self::create([ 'year' => $year, @@ -98,9 +36,8 @@ public static function createBy(?int $year = null, ?int $month = null, ?int $day /** * @param string[]|int[]|null[] $args - * @return static */ - public static function create(array $args): self + public static function create(array $args): static { $date = new static(); @@ -134,55 +71,82 @@ public static function create(array $args): self return $date; } + public function modifyClone(string $modify = ''): static + { + return parent::modifyClone($modify); + } + /** - * @return static + * Set time to current time */ - public function getFirstDayOfWeek(): self + public function setCurrentTime(): static { - return $this->modifyClone('this week') - ->setZeroTime(); + return $this->modifyClone()->setTime((int) date('H'), (int) date('i'), (int) date('s')); } /** - * @return static + * Reset current time (00:00:00) */ - public function getLastDayOfWeek(): self + public function resetTime(): static { - return $this->modifyClone('this week +6 days') - ->setMidnight(); + return $this->modifyClone()->setTime(0, 0, 0); } /** - * @return static + * Reset current time (00:00:00) */ - public function getFirstDayOfMonth(): self + public function setZeroTime(): static { - return $this->modifyClone('first day of this month') - ->setZeroTime(); + return $this->resetTime(); } /** - * @return static + * Set time to midnight (23:59:59) */ - public function getLastDayOfMonth(): self + public function setMidnight(): static { - return $this->modifyClone('last day of this month') - ->setMidnight(); + return $this->modifyClone()->setTime(23, 59, 59); } /** - * @return static + * Set date to today */ - public function getFirstDayOfYear(): self + public function setToday(): static + { + return $this->modifyClone()->setDate((int) date('Y'), (int) date('m'), (int) date('d')); + } + + public function getFirstDayOfWeek(): static + { + return $this->modifyClone('this week') + ->setZeroTime(); + } + + public function getLastDayOfWeek(): static + { + return $this->modifyClone('this week +6 days') + ->setMidnight(); + } + + public function getFirstDayOfMonth(): static + { + return $this->modifyClone('first day of this month') + ->setZeroTime(); + } + + public function getLastDayOfMonth(): static + { + return $this->modifyClone('last day of this month') + ->setMidnight(); + } + + public function getFirstDayOfYear(): static { return $this->modifyClone(sprintf('first day of January %s', $this->format('Y'))) ->setZeroTime(); } - /** - * @return static - */ - public function getLastDayOfYear(): self + public function getLastDayOfYear(): static { return $this->modifyClone(sprintf('last day of December %s', $this->format('Y'))) ->setMidnight(); diff --git a/src/Deeper.php b/src/Deeper.php index 281fc8b..f5a788a 100644 --- a/src/Deeper.php +++ b/src/Deeper.php @@ -17,6 +17,7 @@ public static function has($key, array $arr, string $sep = '.'): bool { try { static::get($key, $arr, $sep); + return true; } catch (InvalidArgumentException $e) { return false; @@ -41,23 +42,16 @@ public static function get($key, array $arr, string $sep = '.', $default = null) } /** - * @param string|int|bool|null $key * @param non-empty-string $sep * @return string[] */ - public static function flat($key, string $sep = '.'): array + public static function flat(string|int|bool|null $key, string $sep = '.'): array { if ($key === '' || $key === null || $key === false) { return []; } - $res = explode($sep, (string) $key); - - if ($res === false) { - return []; - } - - return $res; + return explode($sep, (string) $key); } } diff --git a/src/Fields.php b/src/Fields.php index 5a215ea..48dd468 100644 --- a/src/Fields.php +++ b/src/Fields.php @@ -5,10 +5,7 @@ class Fields { - /** - * @return mixed - */ - public static function inn(string $s) + public static function inn(string $s): mixed { $s = Strings::spaceless($s); $s = Strings::dashless($s); @@ -17,10 +14,7 @@ public static function inn(string $s) return $s; } - /** - * @return mixed - */ - public static function tin(string $s) + public static function tin(string $s): mixed { $s = Strings::spaceless($s); $s = Strings::dashless($s); @@ -29,10 +23,7 @@ public static function tin(string $s) return $s; } - /** - * @return mixed - */ - public static function zip(string $s) + public static function zip(string $s): mixed { $s = Strings::spaceless($s); $s = Strings::dashless($s); @@ -40,10 +31,7 @@ public static function zip(string $s) return $s; } - /** - * @return mixed - */ - public static function phone(string $s) + public static function phone(string $s): mixed { $s = Strings::spaceless($s); $s = Strings::dashless($s); diff --git a/src/Http.php b/src/Http.php index 4689f80..040e043 100644 --- a/src/Http.php +++ b/src/Http.php @@ -10,8 +10,7 @@ class Http use StaticClass; - /** @var string */ - private static $metadataPattern = ' + private static string $metadataPattern = ' ~<\s*meta\s # using lookahead to capture type to $1 @@ -39,7 +38,7 @@ public static function metadata(string $content): array if (preg_match_all(self::$metadataPattern, $content, $matches) !== false) { $combine = array_combine($matches[1], $matches[2]); - if ($combine === false) { + if ($combine === []) { throw new LogicException('Matches count is not equal.'); } diff --git a/src/LazyCollection.php b/src/LazyCollection.php index 5321122..c72e78e 100644 --- a/src/LazyCollection.php +++ b/src/LazyCollection.php @@ -7,6 +7,7 @@ /** * @implements IteratorAggregate + * @phpstan-consistent-constructor */ class LazyCollection implements IteratorAggregate { @@ -15,7 +16,7 @@ class LazyCollection implements IteratorAggregate private $callback; /** @var mixed[] */ - private $data = null; + private ?array $data = null; private function __construct(callable $callback) { diff --git a/src/Merger.php b/src/Merger.php index 6ae56db..d85ec78 100644 --- a/src/Merger.php +++ b/src/Merger.php @@ -5,12 +5,7 @@ class Merger { - /** - * @param mixed $left - * @param mixed $right - * @return mixed - */ - public static function merge($left, $right) + public static function merge(mixed $left, mixed $right): mixed { if (is_array($left) && is_array($right)) { foreach ($left as $key => $val) { diff --git a/src/Monad/Optional.php b/src/Monad/Optional.php index 388e7ad..0e7dee3 100644 --- a/src/Monad/Optional.php +++ b/src/Monad/Optional.php @@ -5,31 +5,27 @@ use Nette\InvalidStateException; use Throwable; +/** + * @phpstan-consistent-constructor + */ class Optional { - /** @var mixed */ - private $value; + private mixed $value; - /** @var mixed */ - private $elseValue; + private mixed $elseValue = null; - /** @var Throwable */ - private $elseThrow; + private ?Throwable $elseThrow = null; - /** - * @param mixed $value - */ - protected function __construct($value) + protected function __construct(mixed $value) { $this->value = $value; } /** - * @param mixed $value * @return static */ - public static function of($value): self + public static function of(mixed $value): self { return new static($value); } @@ -48,10 +44,9 @@ public function isPresent(): bool } /** - * @param mixed $value * @return static */ - public function orElse($value): self + public function orElse(mixed $value): self { $this->elseValue = $value; @@ -68,10 +63,7 @@ public function orElseThrow(Throwable $e): self return $this; } - /** - * @return mixed - */ - public function get() + public function get(): mixed { if ($this->value !== null) { return $this->value; diff --git a/src/Urls.php b/src/Urls.php index eecd5cb..03ee74b 100644 --- a/src/Urls.php +++ b/src/Urls.php @@ -7,7 +7,7 @@ class Urls public static function hasFragment(string $url): bool { - return Strings::contains($url, '#'); + return str_contains($url, '#'); } } diff --git a/src/Values/Email.php b/src/Values/Email.php index 57f8951..c6cd6ca 100644 --- a/src/Values/Email.php +++ b/src/Values/Email.php @@ -9,11 +9,9 @@ class Email { - /** @var string */ - private $domainPart; + private string $domainPart; - /** @var string */ - private $localPart; + private string $localPart; public function __construct(string $value) {