Skip to content

Commit

Permalink
Code: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Nov 15, 2023
1 parent 632c776 commit 5b7cc53
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 166 deletions.
25 changes: 10 additions & 15 deletions src/Annotations.php
Expand Up @@ -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<string, array<string, array<string, array<mixed>>>> */
private static $cache;
private static array $cache;

/**
* @param ReflectionClass<object>|ReflectionMethod|ReflectionProperty|ReflectionFunction $r
Expand All @@ -49,9 +47,8 @@ public static function hasAnnotation(Reflector $r, string $name): bool

/**
* @param ReflectionClass<object>|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);

Expand Down Expand Up @@ -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));
Expand Down
3 changes: 1 addition & 2 deletions src/Csv.php
Expand Up @@ -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);
Expand Down
146 changes: 55 additions & 91 deletions src/DateTime.php
Expand Up @@ -6,6 +6,9 @@
use DateTimeZone;
use Nette\Utils\DateTime as NetteDateTime;

/**
* @phpstan-consistent-constructor
*/
class DateTime extends NetteDateTime
{

Expand All @@ -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,
Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand Down
12 changes: 3 additions & 9 deletions src/Deeper.php
Expand Up @@ -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;
Expand All @@ -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);
}

}
20 changes: 4 additions & 16 deletions src/Fields.php
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -29,21 +23,15 @@ 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);

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);
Expand Down
5 changes: 2 additions & 3 deletions src/Http.php
Expand Up @@ -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
Expand Down Expand Up @@ -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.');
}

Expand Down
3 changes: 2 additions & 1 deletion src/LazyCollection.php
Expand Up @@ -7,6 +7,7 @@

/**
* @implements IteratorAggregate<int, mixed>
* @phpstan-consistent-constructor
*/
class LazyCollection implements IteratorAggregate
{
Expand All @@ -15,7 +16,7 @@ class LazyCollection implements IteratorAggregate
private $callback;

/** @var mixed[] */
private $data = null;
private ?array $data = null;

private function __construct(callable $callback)
{
Expand Down

0 comments on commit 5b7cc53

Please sign in to comment.