Skip to content

Commit

Permalink
Less Psalm issues (#283)
Browse files Browse the repository at this point in the history
Psalm's "other issues found" down by 40 or so.
  • Loading branch information
spaze authored Mar 4, 2024
2 parents b867a6a + ce845c7 commit c91296c
Show file tree
Hide file tree
Showing 58 changed files with 1,099 additions and 196 deletions.
2 changes: 1 addition & 1 deletion site/app/Admin/Presenters/TrainingsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TrainingsPresenter extends BasePresenter
/** @var list<TrainingApplication> */
private array $applications = [];

/** @var int[] */
/** @var list<int> */
private array $applicationIdsAllowedFiles = [];

private ?TrainingApplication $application = null;
Expand Down
5 changes: 3 additions & 2 deletions site/app/Application/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function boot(): Container
{
return self::createConfigurator(
ServerEnv::tryGetString('ENVIRONMENT') === self::MODE_DEVELOPMENT,
self::SITE_DIR . '/config/extra-' . ServerEnv::tryGetString('SERVER_NAME') . '.neon',
self::SITE_DIR . '/config/extra-' . ServerEnv::getString('SERVER_NAME') . '.neon',
)->createContainer();
}

Expand Down Expand Up @@ -112,12 +112,13 @@ private static function getCliArgs(string $argsProvider): CliArgs
$args[] = self::DEBUG;
$args[] = self::COLORS;
$cliArgsParser = new Parser("\n " . implode("\n ", $args));
$cliArgsError = null;
try {
$cliArgsParsed = $cliArgsParser->parse();
} catch (Exception $e) {
$cliArgsError = $e->getMessage();
}
return new CliArgs($cliArgsParsed ?? [], $cliArgsError ?? null);
return new CliArgs($cliArgsParsed ?? [], $cliArgsError);
}

}
7 changes: 7 additions & 0 deletions site/app/Application/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace MichalSpacekCz\Application;

use MichalSpacekCz\ShouldNotHappenException;
use Nette\Application\BadRequestException;
use Nette\Application\Helpers;
use Nette\Application\Request;
Expand All @@ -27,6 +28,12 @@ public function response(Request $request): Response

if ($e instanceof BadRequestException) {
[$module, , $sep] = Helpers::splitName($request->getPresenterName());
if (!is_string($module)) {
throw new ShouldNotHappenException(sprintf('Module should be a string, %s provided', get_debug_type($module)));
}
if (!is_string($sep)) {
throw new ShouldNotHappenException(sprintf('Separator should be a string, %s provided', get_debug_type($sep)));
}
return new ForwardResponse($request->setPresenterName($module . $sep . 'Error'));
}

Expand Down
6 changes: 3 additions & 3 deletions site/app/Application/Locale/Locales.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MichalSpacekCz\Application\Locale;

use Nette\Database\Explorer;
use MichalSpacekCz\Database\TypedDatabase;

class Locales
{
Expand All @@ -13,7 +13,7 @@ class Locales


public function __construct(
private readonly Explorer $database,
private readonly TypedDatabase $database,
) {
}

Expand All @@ -24,7 +24,7 @@ public function __construct(
public function getAllLocales(): array
{
if ($this->locales === null) {
$this->locales = $this->database->fetchPairs('SELECT id_locale, locale FROM locales ORDER BY id_locale');
$this->locales = $this->database->fetchPairsIntString('SELECT id_locale, locale FROM locales ORDER BY id_locale');
}
return $this->locales;
}
Expand Down
11 changes: 4 additions & 7 deletions site/app/Articles/Articles.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use DateTime;
use MichalSpacekCz\Articles\Blog\BlogPost;
use MichalSpacekCz\Articles\Blog\BlogPostFactory;
use MichalSpacekCz\Database\TypedDatabase;
use MichalSpacekCz\DateTime\Exceptions\InvalidTimezoneException;
use MichalSpacekCz\Formatter\TexyFormatter;
use MichalSpacekCz\ShouldNotHappenException;
use MichalSpacekCz\Tags\Tags;
use MichalSpacekCz\Utils\Exceptions\JsonItemNotStringException;
use MichalSpacekCz\Utils\Exceptions\JsonItemsNotArrayException;
Expand All @@ -24,6 +24,7 @@ class Articles

public function __construct(
private readonly Explorer $database,
private readonly TypedDatabase $typedDatabase,
private readonly TexyFormatter $texyFormatter,
private readonly BlogPostFactory $blogPostFactory,
private readonly Tags $tags,
Expand Down Expand Up @@ -235,11 +236,9 @@ public function getNearestPublishDate(): ?DateTime
ORDER BY date
LIMIT 1';
$now = new DateTime();
$result = $this->database->fetchField($query, $now, $now, $this->translator->getDefaultLocale());
$result = $this->typedDatabase->fetchFieldDateTimeNullable($query, $now, $now, $this->translator->getDefaultLocale());
if (!$result) {
return null;
} elseif (!$result instanceof DateTime) {
throw new ShouldNotHappenException(sprintf("Nearest published date is a %s not a DateTime object", get_debug_type($result)));
}
return $result;
}
Expand All @@ -260,11 +259,9 @@ public function getNearestPublishDateByTags(array $tags): ?DateTime
AND l.locale = ?
ORDER BY bp.published
LIMIT 1';
$result = $this->database->fetchField($query, $this->tags->serialize($tags), new DateTime(), $this->translator->getDefaultLocale());
$result = $this->typedDatabase->fetchFieldDateTimeNullable($query, $this->tags->serialize($tags), new DateTime(), $this->translator->getDefaultLocale());
if (!$result) {
return null;
} elseif (!$result instanceof DateTime) {
throw new ShouldNotHappenException(sprintf("Nearest published date is a %s not a DateTime object", get_debug_type($result)));
}
return $result;
}
Expand Down
2 changes: 1 addition & 1 deletion site/app/CompanyInfo/CompanyRegisterRegisterUz.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getDetails(string $companyId): CompanyInfoDetails
IResponse::S200_OK,
'OK',
$unit->ico,
(isset($unit->dic) ? strtoupper(self::COUNTRY_CODE) . $unit->dic : ''),
isset($unit->dic) && is_string($unit->dic) ? strtoupper(self::COUNTRY_CODE) . $unit->dic : '',
$unit->nazovUJ,
$unit->ulica,
$unit->mesto,
Expand Down
10 changes: 10 additions & 0 deletions site/app/Database/Exceptions/TypedDatabaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\Database\Exceptions;

use Exception;

abstract class TypedDatabaseException extends Exception
{
}
16 changes: 16 additions & 0 deletions site/app/Database/Exceptions/TypedDatabaseTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\Database\Exceptions;

use Throwable;

class TypedDatabaseTypeException extends TypedDatabaseException
{

public function __construct(string $expectedType, mixed $value, ?Throwable $previous = null)
{
parent::__construct(sprintf('%s expected, %s given', $expectedType, get_debug_type($value)), previous: $previous);
}

}
167 changes: 167 additions & 0 deletions site/app/Database/TypedDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\Database;

use JetBrains\PhpStorm\Language;
use MichalSpacekCz\Database\Exceptions\TypedDatabaseTypeException;
use Nette\Database\Explorer;
use Nette\Utils\DateTime;

readonly class TypedDatabase
{

public function __construct(
private Explorer $database,
) {
}


/**
* @param literal-string $sql
* @param array<array-key, mixed> $params
* @return array<string, string>
*/
public function fetchPairsStringString(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): array
{
$values = [];
/**
* @var mixed $value
*/
foreach ($this->database->fetchPairs($sql, ...$params) as $key => $value) {
if (!is_string($key)) {
throw new TypedDatabaseTypeException('string', $key);
} elseif (!is_string($value)) {
throw new TypedDatabaseTypeException('string', $value);
}
$values[$key] = $value;
}
return $values;
}


/**
* @param literal-string $sql
* @param array<array-key, mixed> $params
* @return array<int, string>
*/
public function fetchPairsIntString(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): array
{
$values = [];
/**
* @var mixed $value
*/
foreach ($this->database->fetchPairs($sql, ...$params) as $key => $value) {
if (!is_int($key)) {
throw new TypedDatabaseTypeException('int', $key);
} elseif (!is_string($value)) {
throw new TypedDatabaseTypeException('string', $value);
}
$values[$key] = $value;
}
return $values;
}


/**
* @param literal-string $sql
* @param array<array-key, mixed> $params
* @return list<DateTime>
*/
public function fetchPairsListDateTime(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): array
{
$values = [];
foreach ($this->database->fetchPairs($sql, ...$params) as $value) {
if (!$value instanceof DateTime) {
throw new TypedDatabaseTypeException(DateTime::class, $value);
}
$values[] = $value;
}
return $values;
}


/**
* @param literal-string $sql
* @param array<array-key, mixed> $params
*/
public function fetchFieldString(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): string
{
$field = $this->database->fetchField($sql, ...$params);
if (!is_string($field)) {
throw new TypedDatabaseTypeException('string', $field);
}
return $field;
}


/**
* @param literal-string $sql
* @param array<array-key, mixed> $params
*/
public function fetchFieldStringNullable(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): ?string
{
$field = $this->database->fetchField($sql, ...$params);
if (!is_string($field) && !is_null($field)) {
throw new TypedDatabaseTypeException('string|null', $field);
}
return $field;
}


/**
* @param literal-string $sql
* @param array<array-key, mixed> $params
*/
public function fetchFieldInt(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): int
{
$field = $this->database->fetchField($sql, ...$params);
if (!is_int($field)) {
throw new TypedDatabaseTypeException('int', $field);
}
return $field;
}


/**
* @param literal-string $sql
* @param array<array-key, mixed> $params
*/
public function fetchFieldIntNullable(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): ?int
{
$field = $this->database->fetchField($sql, ...$params);
if (!is_int($field) && !is_null($field)) {
throw new TypedDatabaseTypeException('int|null', $field);
}
return $field;
}


/**
* @param literal-string $sql
* @param array<array-key, mixed> $params
*/
public function fetchFieldDateTime(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): DateTime
{
$field = $this->database->fetchField($sql, ...$params);
if (!$field instanceof DateTime) {
throw new TypedDatabaseTypeException(DateTime::class, $field);
}
return $field;
}


/**
* @param literal-string $sql
* @param array<array-key, mixed> $params
*/
public function fetchFieldDateTimeNullable(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): ?DateTime
{
$field = $this->database->fetchField($sql, ...$params);
if (!$field instanceof DateTime && !is_null($field)) {
throw new TypedDatabaseTypeException(DateTime::class . '|null', $field);
}
return $field;
}

}
5 changes: 5 additions & 0 deletions site/app/DateTime/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class DateTime
*/
public const string DATE_RFC3339_MICROSECONDS = 'Y-m-d\TH:i:s.uP';

/**
* Same as in \Nette\Database\Drivers\MySqlDriver::formatDateTime() but without the quotes.
*/
public const string DATE_MYSQL = 'Y-m-d H:i:s';


public function getDaysFromString(string $interval): int
{
Expand Down
2 changes: 1 addition & 1 deletion site/app/Form/TrainingFileFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(

/**
* @param callable(Html|string, string): void $onSuccess
* @param array<int, int> $applicationIdsAllowedFiles
* @param list<int> $applicationIdsAllowedFiles
*/
public function create(callable $onSuccess, DateTimeInterface $trainingStart, array $applicationIdsAllowedFiles): UiForm
{
Expand Down
11 changes: 4 additions & 7 deletions site/app/Http/Redirections.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,24 @@

namespace MichalSpacekCz\Http;

use MichalSpacekCz\Database\TypedDatabase;
use MichalSpacekCz\Http\Exceptions\HttpRedirectDestinationUrlMalformedException;
use MichalSpacekCz\ShouldNotHappenException;
use Nette\Database\Explorer;
use Nette\Http\UrlScript;

readonly class Redirections
{

public function __construct(
private Explorer $database,
private TypedDatabase $database,
) {
}


public function getDestination(UrlScript $sourceUrl): ?string
{
$destination = $this->database->fetchField('SELECT destination FROM redirections WHERE source = ?', $sourceUrl->getPath());
if (!$destination) {
$destination = $this->database->fetchFieldStringNullable('SELECT destination FROM redirections WHERE source = ?', $sourceUrl->getPath());
if ($destination === null) {
return null;
} elseif (!is_string($destination)) {
throw new ShouldNotHappenException(sprintf("Redirect destination for '%s' is a %s not a string", $sourceUrl->getPath(), get_debug_type($destination)));
}
$destinationUrl = parse_url($destination);
if ($destinationUrl === false) {
Expand Down
Loading

0 comments on commit c91296c

Please sign in to comment.